Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose in another directory affects other containers

I have an issue. I used my docker-compose file for one project. Then I copied it to another directory in order to run another containers. But whenever I do that it recreates existing containers or in case I use the down command it also destroys containers from another directory, what could be wrong?

Here is my configuration.

version: '3.5'

services:
  postgres:
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - "5440:5432"
    networks:
      - postgres
    restart: unless-stopped

  pgadmin:
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: rootme
    volumes:
      - pgadmin:/root/.pgadmin
    ports:
      - "8440:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
  postgres:
  pgadmin:

For example when I run docker-compose up -d from another directory it recreates containers

Recreating docker_postgres_1 ... done
Recreating docker_pgadmin_1  ... done

What is the issue?

like image 976
Anthony B. Rhoden Avatar asked Sep 14 '25 01:09

Anthony B. Rhoden


1 Answers

Docker Compose attaches a name prefix to everything it creates, but the default prefix is just based on the basename of the current directory. If you have a layout like

projectA
+-- docker
|   \-- docker-compose.yml
projectB
\-- docker
    \-- docker-compose.yml

then both docker-compose instances will think the project name is just docker (the name of the directory containing docker-compose.yml) and create container names like, for example, docker_postgres_1.

You can get around this by either renaming one of the directories, using the docker-compose -p option, or setting a COMPOSE_PROJECT_NAME environment variable. It wouldn't be unusual to see a docker-compose.yml file in the top-level directory of a project and that might help disambiguate things.

like image 163
David Maze Avatar answered Sep 16 '25 22:09

David Maze