Docker Compose:无需重新构建应用程序即可更改COMPOSE_PROJECT_NAME

时间:2018-12-20 14:38:57

标签: docker docker-compose

摘要

我有一个应用程序X,我想在同一操作系统中部署同一应用程序的多个实例(端口号将由.env处理),而不必为每个实例启动build

我尝试过的事情

因此,我设法动态地(通过用户更改.env文件)来更改容器的container_name。但是然后我们不能同时运行5个实例(即使端口不同,docker也只会停止第一个实例,然后重新创建容器)

接下来,我遇到了COMPOSE_PROJECT_NAME似乎可行,但开始了新的构建。


COMPOSE_PROJECT_NAME = hello-01

docker-compose up
Creating network "hello-01_default" with the default driver
Building test
Step 1/2 : FROM ubuntu:latest
 ---> 113a43faa138
Step 2/2 : RUN echo Hello
 ---> Using cache
 ---> ba846acc19e5
Successfully built ba846acc19e5
Successfully tagged hello-01_test:latest
WARNING: Image for service test was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating hello-01_test ... done
Attaching to hello-01_test
hello-01_test exited with code 0

COMPOSE_PROJECT_NAME = hello-2

docker-compose up
Creating network "hello-02_default" with the default driver
Building test
Step 1/2 : FROM ubuntu:latest
 ---> 113a43faa138
Step 2/2 : RUN echo Hello
 ---> Using cache
 ---> ba846acc19e5
Successfully built ba846acc19e5
Successfully tagged hello-02_test:latest
WARNING: Image for service test was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating hello-02_test ... done
Attaching to hello-02_test
hello-02_test exited with code 0

源文件

docker-compose.yml

version: '3'
services:
  test:
    container_name: "${COMPOSE_PROJECT_NAME}_test"
    build: .

.env

COMPOSE_PROJECT_NAME=hello-02

Dockerfile

FROM ubuntu:latest
RUN echo Hello

Ubuntu 18.04.1 LTS 
Docker version 18.06.0-ce, build 0ffa825
docker-compose version 1.21.2, build a133471

1 个答案:

答案 0 :(得分:2)

通过在不提供image:引用的情况下更改容器名称,可以使撰写文件不知道您已经构建了该映像。因此,如果您将该docker映像构建为某些本地映像example/image/local, you can add映像:example / image / local to your docker-compose file and do that to spawn docker-compose up -d多次,请在示例中使用环境变量更改名称。< / p>

但是,似乎您可能想研究使用副本,而不是在摆脱docker-compose的单行填充之外进行繁琐的手动操作。

https://docs.docker.com/compose/compose-file/#short-syntax