Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose not prepending project name to the container names when running multiple instances

I have a set of docker that I run using docker-compose up -d pretty basic so far

I want to run multiple instances of my project and I have read this Run multiple docker compose

Now when running docker-compose up -p PRNAME -d compose is not prepending project name to containers like prname_container1 and I'm getting the following error :

ERROR: for container1  Cannot create container for service service1: Conflict. The container name "/container1" is already in use by container "f7aeb2ef782556ae5b0". You have to remove (or rename) that container to be able to reuse that name.

I might be missing something out here.

A part of my docker-compose.yml looks like

services:
  service1:
    image: "${PROJECT_REPO}:image"
    container_name: "container1"
    ports:
      - 443:443
      - 8000:80
    networks:
      - db
      - proxy
      - oauth
    depends_on:
      - db
like image 681
Mousse Avatar asked Jan 27 '23 06:01

Mousse


1 Answers

When you explicitly set container_name: in your docker-compose.yml file, the container name will be exactly what you specify; Docker Compose won't add its per-directory prefix to it.

Usually this doesn't matter to you at all, and it's safe to remove container_name:. You will still be able to reach other containers using their service name in docker-compose.yml as hostnames, and the docker-compose CLI provides wrappers for management commands like docker-compose stop that will act on the correct container.

You will also hit trouble with ports: and there is less of a clear solution here. Only one container or process can bind to a specific port on the host. You can leave off the first port number in ports:, and Docker will pick a port for you

ports:
  - '80'
  - '443'

but then you need to manually look up the corresponding host port number

docker-compose port service1 80
like image 88
David Maze Avatar answered Jan 31 '23 09:01

David Maze