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
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:
In your case, I would create two directories:
vsb
andvsb
. Now let's go into these two directories.We have these one file (at least, but you can have more per your requirement):
Note the file names exactly, as
Dockerfile
starts with capital D.Let's watch
docker-compose.yml
: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
underservices
. You can use any name you want there, as it's your choice.Under
keepalived
, the keywordbuild
specifies in which pathDockerfile
exists, so that as the path is called/home/vsa/keepalived
, so we use.
which means here and then it goes tokeepalived
directory, searching forDockerfile
(indocker-compose.yml
forvsb
, it searches for this file in/home/vsb/keepalived
).networks
part specifies theexternal
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 namesome_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 rundocker 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 calledDockerfile
:Now let's go to
Dockerfile
:FROM
command specifies which OS base to use. In this case, we want to useubuntu
for example, so that we create our image based onubuntu
.There are other commands you can see them all in above link.
After having finished both
Dockerfile
anddocker-compose.yml
files with your own commands and keywords, you can run and create them by these commands:Now we'll have eight containers calling these (docker automatically called them, otherwise you explicitly name them on your own):