Reputation: 1175
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
Upvotes: 3
Views: 5070
Reputation: 159761
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:
COMPOSE_PROJECT_NAME
environment variable..env
file in the current directory, and set COMPOSE_PROJECT_NAME
there.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.
Upvotes: 5
Reputation: 48662
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.
Upvotes: 1