How to run a few Docker Compose with similar names of images?

1.3k Views Asked by At

I have >10 projects with similar architecture, but not identical. Names of images in docker-compose are the same. So I need to run all of them on one droplet. How can I run it with out conflicts?

2

There are 2 best solutions below

2
On BEST ANSWER

Docker compose uses the current directoy as "project name", for example:

cd foo
docker-compose up

This will create every service declared as foo_<servicename>_1.

You can specify a parameter on docker-compose changing the default project name for each compose file:

docker-compose --file mycompose.yml --project-name anyname up

--file can be -f and --project-name can be -p

0
On

Since your containers cannot have exact the same name, but you can use directory prefixing names using by default.

Let us imagine you have this directory structure:

/home/your_project_directory/1/
/home/your_project_directory/2/
/home/your_project_directory/3/
/home/your_project_directory/4/

In directory 1 you have a docker-compose.yml file with the below contents:

version: '3.9'

services:
    httpd:
        image: nginx
        restart: always
volumes:
    nginx: {}

Suppose you are in directory /home/your_project_directory/ .

Now let's cd into each of these four directories and run this command:

ln -s /home/your_project_directory/2/docker-compose.yml $PWD/2

This will create a soft link from docker-compose.yml file in directory 1 into 2.

And repeat this command for the two other directories 3 and 4.

When you have done all of them, you can verify each of them by this command:

find /home/your_project_directory/ -name 'docker-compose.yml' ls -lh {} \;

This command finds in directory /home/your_project_directory/ any file called docker-compose.yml and then runs ls -lh THAT_FILE, as below:

-rw-r--r-- 1 root root 105 Apr 23 04:20 /home/your_project_directory/1/docker-compose.yml
lrwxrwxrwx 1 root root 29 Apr 23 04:11 /home/your_project_directory/2/docker-compose.yml -> /home/your_project_directory/1/docker-compose.yml
lrwxrwxrwx 1 root root 29 Apr 23 04:11 /home/your_project_directory/3/docker-compose.yml -> /home/your_project_directory/1/docker-compose.yml
lrwxrwxrwx 1 root root 29 Apr 23 04:12 /home/your_project_directory/4/docker-compose.yml -> /home/your_project_directory/1/docker-compose.yml

Good, now let's make them all up by another find command:

find . -name "docker-compose.yml" -exec docker-compose -f {} up -d \;

Again, this file finds any file named docker-compose.yml and then runs docker-compose -f THAT_FILE up -d command.

So, these four command would run:

docker-compose -f 1/docker-compose.yml up -d
docker-compose -f 2/docker-compose.yml up -d
docker-compose -f 3/docker-compose.yml up -d
docker-compose -f 4/docker-compose.yml up -d

And if you run docker ps, you should see similar output:

CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS          PORTS     NAMES
4dac5dfba581   nginx              "/docker-entrypoint.…"   12 minutes ago   Up 12 minutes   80/tcp    1_httpd_1
d517a156f6a1   nginx              "/docker-entrypoint.…"   12 minutes ago   Up 12 minutes   80/tcp    2_httpd_1
ed6223ca5411   nginx              "/docker-entrypoint.…"   12 minutes ago   Up 12 minutes   80/tcp    3_httpd_1
0fa78f2ba8bf   nginx              "/docker-entrypoint.…"   12 minutes ago   Up 12 minutes   80/tcp    4_httpd_1

But per your needs and your requirements, you can add any details to your docker-compose.yml file.

Note that you cannot use the same port mapping in this file, as you get port already allocated error.

The other approach is to cp one file into other three directories and use port mapping with different ports, like in 1/docker-compose.yml:

ports:
    - "8080:80"

And 2/docker-compose.yml:

ports:
    - "8081:80"

etc.