Docker Compose: Change COMPOSE_PROJECT_NAME without rebuilding the application

Viewed 3985

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
1 Answers

By changing the container name without providing an image: reference the compose file has no idea that you've already built that image. So if you build that docker image as some local image example/image/local, you can addimage: example/image/localto your docker-compose file and do that to spawndocker-compose up -d` many times by changing the name with an environment variable in your example.

However, it appears that you might want to look into using replicas instead of making this a horrifically manual effort outside of on the one-line full up that you'd get out of docker-compose.

https://docs.docker.com/compose/compose-file/#short-syntax

Related