本文介绍了Docker compose 在另一个目录中会影响其他容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我在一个项目中使用了我的 docker-compose 文件.然后我将它复制到另一个目录以运行另一个容器.但是每当我这样做时,它都会重新创建现有的容器,或者如果我使用 down 命令,它还会破坏另一个目录中的容器,这有什么问题?

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?

这是我的配置.

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: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: rootme
    volumes:
      - pgadmin:/root/.pgadmin
    ports:
      - "8440:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
  postgres:
  pgadmin:

例如,当我从另一个目录运行 docker-compose up -d 时,它会重新创建容器

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

有什么问题?

推荐答案

Docker Compose 为其创建的所有内容附加名称前缀,但默认前缀仅基于当前目录的基本名称.如果你有这样的布局

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

那么两个 docker-compose 实例都会认为项目名称只是 docker(包含 docker-compose.yml 的目录的名称)) 并创建容器名称,例如 docker_postgres_1.

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.

您可以通过使用 docker-compose -p 选项,或 设置COMPOSE_PROJECT_NAME 环境变量.在项目的顶级目录中看到 docker-compose.yml 文件并不罕见,这可能有助于消除歧义.

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.

这篇关于Docker compose 在另一个目录中会影响其他容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:04