3

for some reason, I need to create the container with the same image, But when I started the second one, It just restarted the fist one's container

the first yml file:

version: "3.1"
services:

  php:
    image:php:php73-fpm
    restart: always
    ports:
      - "9000:9000"
      - "9501:9501"
    volumes:
      - $PWD/../:/var/www/html/
    networks:
      - app_net
    container_name: php
networks:
  app_net:
    driver: bridge

the second yml file:

version: "3.1"
services:

  php:
    image:php:php73-fpm
    restart: always
    ports:
      - "19000:19000"
      - "19501:19501"
    volumes:
      - $PWD/../:/var/www/html/
    networks:
      - app_net2
    container_name: php73
networks:
  app_net2:
    driver: bridge

when I run docker-compose up -d to start the first one:

$ cd ~/Document/php/work/docker/
$ docker-compose up -d
Creating network "docker_app_net" with driver "bridge"
Creating php ... done

then I switch the directory to the second yml file

$ cd ../../private/docker/
$ docker-compose up -d
Recreating php ... done

2 Answers 2

5

Compose has a notion of a project name. By default the project name is the basename of the directory containing the docker-compose.yml file. In your example both directories are named docker (even if they're in different parent directories) so Compose looks for a project named docker and a container named php, and finds a match.

There are four ways to override this:

  1. Rename one of the directories.
  2. Set the COMPOSE_PROJECT_NAME environment variable.
  3. Create a .env file in the current directory, and set COMPOSE_PROJECT_NAME there.
  4. Use the docker-compose -p option (on every docker-compose command).

Within your docker-compose.yml file, the second part of ports: needs to match what the container is listening on; this is allowed to be different from the first part. So use the same 9500/9501 in both files.

Another consequence of the Compose project naming is that the standard names of containers, volumes, and networks that Compose creates will be prefixed with the project name. If the project name (current directory name) is docker2, and you reduce the Compose file to

version: "3.1"
services:
  php:
    build: .
    restart: always
    ports:
      - "19000:9000"
      - "19501:9501"
    # no manual container_name: or networks:

The container will be named docker2_php_1, and it will be attached to a network named docker2_default; these will be different from the container/network created in the docker1 project/directory.

Sign up to request clarification or add additional context in comments.

1 Comment

excellent!, Your answer perfectly told me why and how, I tried your suggest and It work perfectly.
1

You can't have two containers with the same name. Since both names are just php, Docker thought they were settings that were supposed to be merged for the same container. Rename one of them.

2 Comments

great, It works, But I still have a question, actually, I have others docker-compose.yml, For simple, I named them A.yml and B.yml. Both of them started three container, and one of them was the same image: redis:5.0.5, Service's name is the same:redis. but when I started B.yml, It did nothing like : creating xxx done... creating xxx done... restarting redis-A done... (redis-A is container name in A.yml, B.yml's container name is redis-B)
@afraid.jpg Having two services with the same name will also end badly. Don't do that either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.