Using Docker-Compose to spin up multiple instances of a container with different configurations

女生的网名这么多〃 提交于 2019-12-20 10:24:11

问题


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.

Is it possible to launch a container on the same host with different configurations (different .yml files) on the same host?

Using the following commands:

docker-compose -f dev.yml up -d
docker-compose -f qa.yml up -d

only the qa.yml container will be running, which is not what I want.

-- edit --

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

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

回答1:


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.

You can use either docker-compose -p <project_name> or set COMPOSE_PROJECT_NAME in the environment.

There is also some discussion about providing a way to persist the project name: https://github.com/docker/compose/issues/745



来源:https://stackoverflow.com/questions/32939319/using-docker-compose-to-spin-up-multiple-instances-of-a-container-with-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!