Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run the same docker-compose.yml several times on same docker daemon with different names?

My situation. Trying to run a docker-compose structure several times on the same box. This is my docker-compose.yml:

version: '3'
services:
  code:
    image: organization:java-maven
    links:
      - mysql:mysql
    volumes:
      - "${PWD}:/home/ubuntu/src"
  mysql:
    image: organization:mysql

Running this twice with docker-compose run code mvn clean test creates two containers of code and one container of mysql.

Now, I want one code to be linked to one mysql, and another code linked to another mysql.

How do I accomplish this? This is supposed to run on jenkins slaves and the maven executions cannot share mysql.

I've miserably failed trying with the "-e KEY=VALUE" option for docker-compose run together with container_namein the docker compose file.

Not sure how to approach this, please help, thank you.

like image 643
Wrench Avatar asked Jan 24 '17 15:01

Wrench


People also ask

How do I run multiple copies of 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.

How do you handle multiple docker compose?

Running Multiple Copies of a Single Compose Project For times when you need multiple copies of environments with the same composition (or docker-compose. yml file), simply run docker-compose up -p new_project_name .

Can I execute multiple instances of a docker image on the 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.

Can you use multiple docker compose files?

Using Multiple Docker Compose Files The use of multiple Docker Compose files allows you to change your application for different environments (e.g. staging, dev, and production) and helps you run admin tasks or tests against your application. Docker Compose reads two files by default, a docker-compose.


1 Answers

So, I focused too much on using directives to alter container names manually. The solution was much easier.

docker-compose -p anything run code mvn clean test

docker-compose -p anything_else run code mvn clean test

So, this is the project name solution. Docker compose will use the value given with the option -p as a prefix when creating container names. That means no collision.

Very handy!

For more reading: documentation around project-name option

like image 194
Wrench Avatar answered Oct 14 '22 02:10

Wrench