Docker Compose: Change COMPOSE_PROJECT_NAME without rebuilding the application

Summary :

I have an application X, I want to deploy multiple instances of the same application (port numbers will be handled by an .env) in the same OS without starting a build for each instance.

What I tried :

So I managed to dynamically (by the user changing .env file), change the container_name of a container. But then we cannot run 5 instances at the same time (even if the ports are different, docker just stops the first re-creates the container for second)

Next I came across COMPOSE_PROJECT_NAME that seems to work BUT starts a new build.

COMPOSE_PROJECT_NAME=hello-01

docker-compose up
Creating network "hello-01_default" with the default driver
Building test
Step 1/2 : FROM ubuntu:latest
 ---> 113a43faa138
Step 2/2 : RUN echo Hello
 ---> Using cache
 ---> ba846acc19e5
Successfully built ba846acc19e5
Successfully tagged hello-01_test:latest
WARNING: Image for service test was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating hello-01_test ... done
Attaching to hello-01_test
hello-01_test exited with code 0

COMPOSE_PROJECT_NAME=hello-2

docker-compose up
Creating network "hello-02_default" with the default driver
Building test
Step 1/2 : FROM ubuntu:latest
 ---> 113a43faa138
Step 2/2 : RUN echo Hello
 ---> Using cache
 ---> ba846acc19e5
Successfully built ba846acc19e5
Successfully tagged hello-02_test:latest
WARNING: Image for service test was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating hello-02_test ... done
Attaching to hello-02_test
hello-02_test exited with code 0

Source files

docker-compose.yml

version: '3'
services:
  test:
    container_name: "${COMPOSE_PROJECT_NAME}_test"
    build: .

.env

COMPOSE_PROJECT_NAME=hello-02

Dockerfile

FROM ubuntu:latest
RUN echo Hello
Ubuntu 18.04.1 LTS 
Docker version 18.06.0-ce, build 0ffa825
docker-compose version 1.21.2, build a133471

As a convention, the folder name will be used for what you call COMPOSE_PROJECT_NAME.
The convention is: {foldername}_{servicename}_${replic-number}.

Hence, if you create fhe subfolders hello-01, hello-02 and hello-03 and put you compose file and .env files inside, you can do ‘docker-compose up -d’ inside these folders and end up with names like hello-01_{service name}_1, hello-02_{service name}1, hello-03{service name}_1. The built image will be tagged as hello-01_{service name}, hello-02_{service name}, hello-03_{service name}. If you build the images within a compose file, the folder name will be part of the built image name and it’s tag.

I would strongly advise to build the images outside the compose file to have a defined image name and tag. This defined image name and tag can be reference in all you compose files.

With Docker Swarm, you even have to specify the stack name during deployments:

docker stack deploy [OPTIONS] STACK