Using Docker-Compose to spin up multiple instances

2019-03-14 10:40发布

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条回答
孤傲高冷的网名
2楼-- · 2019-03-14 11:08

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

查看更多
登录 后发表回答