Use multiple dockers with same name

778 Views Asked by At

I am working on building a high availability setup using keepalived, where each server will have its own set of dockers that will get handled appropriately depending on if it is in BACKUP or MASTER. However, for testing purposes, I don't have 2 boxes available that I can turn on and off for this. So, is there is a good (preferably lightweight) way I can setup multiple dockers with the same name on the same machine?

Essentially, it would like it to look something like this:

 Physical Server A
 ----------------------------------------- 
|  Virtual Server A                       |
|  -------------------------------------- |
| | keepalived - htmld - accessd - mysql  |
|  -------------------------------------- |
|      ^                                  |
|      |                                  |
|      v                                  |
|  Virtual Server B                       |
|  -------------------------------------- |
| | keepalived - htmld - accessd - mysql  |
|  -------------------------------------- |
 -----------------------------------------

Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

You cannot have multiple containers with the exact same name, but you can use docker-compose file to have several directories and containers with same name (but with some differences that I explain below).

You can read more about it in Docker Docs regarding my below explanation.

Let us suppose yours:

 Physical Server A
 ----------------------------------------- 
|  Virtual Server A                       |
|  -------------------------------------- |
| | keepalived - htmld - accessd - mysql  |
|  -------------------------------------- |
|      ^                                  |
|      |                                  |
|      v                                  |
|  Virtual Server B                       |
|  -------------------------------------- |
| | keepalived - htmld - accessd - mysql  |
|  -------------------------------------- |
 -----------------------------------------

In your case, I would create two directories: vsb and vsb. Now let's go into these two directories.

We have these one file (at least, but you can have more per your requirement):

 ----------------------------------------- 
|  /home/vsa/docker-compose.yml           |
|  /home/vsa/keepalived/Dockerfile        |
|  /home/vsa/htmld/Dockerfile             |
|  /home/vsa/accessd/Dockerfile           |
|  /home/vsa/mysql/Dockerfile             |
|  -------------------------------------- |
|      ^                                  |
|      |                                  |
|      v                                  |
|  /home/vsb/docker-compose.yml           |
|  /home/vsb/keepalived/Dockerfile        |
|  /home/vsb/htmld/Dockerfile             |
|  /home/vsb/accessd/Dockerfile           |
|  /home/vsb/mysql/Dockerfile             |
|  -------------------------------------- |
 -----------------------------------------

Note the file names exactly, as Dockerfile starts with capital D.

Let's watch docker-compose.yml:

version: '3.9'

services:
    keepalived:
        build: ./keepalived
        restart: always
    htmld:
        build: ./htmld
        restart: always
    accessd:
        build: ./accessd
        restart: always
    mysql:
        build: ./mysql
        restart: always

networks:
  default:
    external:
      name: some_network
volumes:
    some_name: {}

Let's dig into docker-compose.yml first:

Version part defines which version to use. Services part starts the services and containers you want to create and run.

I've used names like keepalived under services. You can use any name you want there, as it's your choice.

Under keepalived, the keyword build specifies in which path Dockerfile exists, so that as the path is called /home/vsa/keepalived, so we use . which means here and then it goes to keepalived directory, searching for Dockerfile (in docker-compose.yml for vsb, it searches for this file in /home/vsb/keepalived).

networks part specifies the external network these containers use, so that when all of our containers from docker-compose are running, then they're in the same docker network, so they can see and talk to each other. name part has the name some_network that you can choose any name you want that created before.

How to create a network called some_network is, if you're in Linux, you should run docker network create some_network before running docker-compose file.

volumes part specifies the name of volume of these services.

And here is an example in keepalived directory for a file called Dockerfile:

FROM ubuntu:latest # see [Dockerfile Docs][2] for more info
# after FROM command, you can use
# other available commands to reach
# your own goal

Now let's go to Dockerfile:

FROM command specifies which OS base to use. In this case, we want to use ubuntu for example, so that we create our image based on ubuntu.

There are other commands you can see them all in above link.

After having finished both Dockerfile and docker-compose.yml files with your own commands and keywords, you can run and create them by these commands:

docker-compose -f /home/vsa/docker-compose.yml up -d
docker-compose -f /home/vsb/docker-compose.yml up -d

Now we'll have eight containers calling these (docker automatically called them, otherwise you explicitly name them on your own):

vsa_keepalived
vsa_htmld
vsa_accessd
vsa_mysql
vsb_keepalived
vsb_htmld
vsb_accessd
vsb_mysql