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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With