Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose up keep replace existing container

I got the code from two branches with following config:

docker-compose.yml:

version: '3'
services:
  server:
    build: .
    restart: always
    image: XXXXX
    entrypoint: ["./run.sh"]
    container_name: XXXX
    ports:
      - 127.0.0.1:8000:8000
    volumes:
      - .:/app

    depends_on:
      - redis
  redis:
    container_name: XXXXX
    image: redis:4-alpine

When I docker compose first branch, it works well, but when I compose up the second branch, the original container become the new branch container, which I want the two branches containers exist at the same time.

When I compose up the second branch code, the following message shows:

Recreating XXXXX_branch2 ... done
Attaching to XXXXX_branch1
like image 244
Gary Ng Avatar asked Feb 01 '18 07:02

Gary Ng


1 Answers

Docker compose associates the container with the project name (default directory name) and the service name or container_name if specified. Thus in case both branches have the compose file under the same directory name, and thus the compose files will be interpreted as refering to the same container, which will lead to the container being recreated.

To avoid this situation, you can the --project-name option to override the default one (directory name).

docker-compose --project-name branch1 up -d
docker-compose --project-name branch2 up -d

In this case both containers will be created.

But note that if both compose files have the same container_name set, there will be a conflict and the second container creation will fail. To avoid that, either use different container names, or remove the container_name property, to get the default container name which is <project_name>_<service_name>_1

like image 126
yamenk Avatar answered Oct 09 '22 20:10

yamenk