简体   繁体   English

尽管docker-compose.yml中的container_name不同,但docker-compose仍会重新创建运行中的容器

[英]docker-compose recreates a running container despite having a different container_name in the docker-compose.yml

I have a simple docker-compose.yml file: 我有一个简单的docker-compose.yml文件:

services:
  app: 
    build: .
    image: team/repo:$VERSION
    ports:
      - "$PORT:3000" 
    container_name: myname-$VERSION
    volumes: 
      - $DB_PATH:/db
      - ./tmp:/tmp
    environment:
      DB_PATH: /db
volumes:
  db:
  tmp:

After I define eg VERSION=1.0.0 and PORT=80 , I start the container with: 在定义了VERSION=1.0.0PORT=80 ,我使用以下命令启动容器:

docker-compose up -d

And it creates a container "myname-1.0.0". 并创建一个容器“ myname-1.0.0”。 Afterwards, if I re-export my env. var 之后,如果我重新导出环境env. var env. var . env. var eg VERSION=1.0.1 and PORT=8080 and rerun the command, it stops the running container and launches the new one "byname-1.0.1". 例如VERSION=1.0.1PORT=8080并重新运行命令,它将停止正在运行的容器并启动新的“ byname-1.0.1”。 But the message is: 但是消息是:

Recreating myname-1.0.0 ... done 重新创建myname-1.0.0 ...完成

"docker ps" shows only the new container "myname-1.0.1", but I would expect to see both containers running on ports 80 and 8080 , respectively; “ docker ps”仅显示新的容器“ myname-1.0.1”,但是我希望看到这两个容器分别在端口808080上运行; and from 2 different images "team/repo:1.0.0" and "team/repo:1.0.1", respectively. 并分别来自2个不同的图像“ team / repo:1.0.0”和“ team / repo:1.0.1”。

What are the correct steps to have the 2 containers running side by side? 使2个容器并排运行的正确步骤是什么? I'd appreciate any pointers, thanks! 我将不胜感激任何指示,谢谢!

Docker Compose uses labels on containers to remember which container goes with which compose file. Docker Compose在容器上使用标签来记住哪个容器与哪个撰写文件一起使用。 The container names as such aren't used for this. 这样的容器名称不用于此目的。 (In early versions of fig this wasn't true, but since 2015 it has been.) (在fig早期版本中并非如此,但自2015年以来一直是。)

Compose has the notion of a project name . Compose具有项目名称的概念。 This has a couple of impacts. 这有几个影响。 One is that the project name gets put in the container labels, so versions of the same docker-compose.yml deployed under different project names are considered different projects. 一种是将项目名称放入容器标签中,因此以不同项目名称部署的同一docker-compose.yml版本被视为不同项目。 Another is that there is a default for container_name: based on the project and service name. 另一个是container_name:默认名称container_name:基于项目和服务名称。

So, this should work for you if you: 因此,如果您:

  1. Delete the container_name: and let Docker Compose construct a non-conflicting default for you; 删除container_name:并让Docker Compose为您构造一个无冲突的默认值; and

  2. Use the docker-compose -p option or $COMPOSE_PROJECT_NAME environment variable to explicitly specify the project name. 使用$COMPOSE_PROJECT_NAME docker-compose -p选项$COMPOSE_PROJECT_NAME环境变量来显式指定项目名称。

     VERSION=1.0.0 PORT=80 COMPOSE_PROJECT_NAME=project-100 \\ docker-compose up VERSION=1.0.1 PORT=8080 COMPOSE_PROJECT_NAME=project-101 \\ docker-compose up 

you need to specify the arg build in your docker-compose like this: 您需要在docker-compose中指定arg构建,如下所示:

services:
  app: 
    build:
      context: .
      args:
        - VERSION
    image: team/repo:$VERSION
    ports:
      - "$PORT:3000" 
    container_name: myname-$VERSION
    volumes: 
      - $DB_PATH:/db
      - ./tmp:/tmp
    environment:
      DB_PATH: /db
volumes:
  db:
  tmp:

and start it like this : 然后像这样启动它:

docker-compose up -d --build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM