使用 Docker-Compose 启动具有不同配置的容器的多个实例 [英] Using Docker-Compose to spin up multiple instances of a container with different configurations
问题描述
我知道您可以使用 docker-compose 和 scale 命令来启动多个容器.但是,它们都将使用相同的配置.
I understand that you can user docker-compose with the scale command to spin up multiple containers. However, they will all have the same configuration used.
是否可以在同一主机上以不同配置(不同的.yml
文件)在同一主机上启动容器?
Is it possible to launch a container on the same host with different configurations (different .yml
files) on the same host?
使用以下命令:
docker-compose -f dev.yml up -d
docker-compose -f qa.yml up -d
只有 qa.yml
容器会运行,这不是我想要的.
only the qa.yml
container will be running, which is not what I want.
-- 编辑 --
这是我尝试运行这两个命令时发生的情况.
Here's what happens when I try running both commands.
$ docker-compose -f compose/dev.yml up -d
compose_mydocker_1 is up-to-date
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
905912df6e48 compose_mydocker "/sbin/my_init" 2 days ago Up 2 days 0.0.0.0:1234->80/tcp compose_mydocker_1
$ docker-compose -f compose/qa.yml up -d
Recreating compose_mydocker_1...
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fc912201224 compose_mydocker "/sbin/my_init" 5 seconds ago Up 5 seconds 0.0.0.0:1235->80/tcp compose_mydocker_1
我的 qa.yml
和 dev.yml
看起来像这样:
My qa.yml
and dev.yml
look like this:
mydocker:
build: ..
ports:
- "1234:80" #for dev.yml
#- "1235:80" for qa.yml
environment:
- ENVIRONMENT=dev #and vice-versa for qa
volumes:
- ../assets/images:/var/www/assets
推荐答案
您需要做的是更改项目名称.默认情况下,compose 使用基于当前目录命名的项目.就您而言,您需要单独的环境,因此需要不同的项目名称.
What you need to do is change the project name. By default, compose uses a project named based on the current directory. In your case, you want separate environments, so you need different project names.
您可以在环境中使用 docker-compose -p
或设置 COMPOSE_PROJECT_NAME
.
You can use either docker-compose -p <project_name>
or set COMPOSE_PROJECT_NAME
in the environment.
还有一些关于提供一种持久化项目名称的方法的讨论:https://github.com/docker/compose/issues/745
There is also some discussion about providing a way to persist the project name: https://github.com/docker/compose/issues/745
这篇关于使用 Docker-Compose 启动具有不同配置的容器的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!