Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 728
Nepoxx Avatar asked Oct 04 '15 23:10

Nepoxx


People also ask

Can you run multiple instances of a Docker container?

Yes. You can run multiple containers on one server, and it is not restricted to the number of CPUs. Your command creates and starts exactly 1 container that has access to at most 16 CPUs (and in the 2nd example only precisely CPUs 0-15).

Can we execute multiple instances of Docker image on same server?

If you're expecting it to behave the same way, as when running a single container, it won't happen. If there's no specific reason for using version: '3' you may use version:'2' instead. Or you can let it create its own network, which it does with your current docker-compose file.

How do I run multiple copies of a compose file on the same host?

🔗 Compose uses the project name to create unique identifiers for all of a project's containers and other resources. To run multiple copies of a project, set a custom project name using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.

Can we spin up a single instance of a Docker container using a Docker compose file?

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.


1 Answers

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

like image 106
dnephin Avatar answered Sep 23 '22 21:09

dnephin