HI All, this document deals with how to create docker swarm. I hope you read my previous blog introduction to docker swarm. If not, read the article to get to a clear understanding of docker swarm and microservices.
Setting up and managing docker swarm is easy. It not like any other orchestration tools like Kubernates etc.


* install docker engine in all the nodes.
* take 2 nodes for the cluster, where one will be the manager and another one is worker node. You can add as many nodes as per your requirement
* Docker image to run. Here I’m using a nodejs application. if you want the same image to run, check this document.


Now let’s create docker swarm and deploy containers to the clusters


Out of 2 servers, I’m choosing one server to act as the manager/leader node. In my case, is manager node. So let’s initialize the docker swarm. Execute the below command.

Initialize Docker swarm

docker swarm init --advertise-addr
Swarm initialized: current node (o2zdohabxmjvovsf17oa5bmwd) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-54ntcuitlh51wycml1tznq70garostngofbo5g0g2qhwuedjps-55zr77l0rxcn7wd0xp3nzoipt

To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.

The above command initializes docker swarm. Parameter –advertise-addr advertise the address of the manager address.

So once you initialize the docker swarm you can see the output “Swarm initialized”

Now Join your other node(worker node) to manager node, you can do this by executing the below command, where you have the token and address to join the manager node.


docker swarm join --token SWMTKN-1-54ntcuitlh51wycml1tznq70garostngofbo5g0g2qhwuedjps-55zr77l0rxcn7wd0xp3nzoipt
This node joined a swarm as a worker.

Now Run the below command to see the number of the nodes joined to docker swarm, here Manager Status with “leader” is the manager node, Status shows the status of each node.

NOTE: You can execute the below command only manager node.

docker node ls
89hd8fgq49ssxqajpqatnymqx localhost.localdomain Ready Active 18.09.1
o2zdohabxmjvovsf17oa5bmwd * proxmox Ready Active Leader 18.09.1

Now we are done the Docker swarm setup with two nodes, let’s create a service in docker swarm

Here I have docker image of nodejs application in my private Docker registry. Check the prerequisites section to create a docker image.

Execute the below command create a service of nodejs application.

docker service create –replicas 2 –name nodejs dockerreg:5000/swarm:latest
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged

– Create, create the docker swarm service
– replicas assign the docker manager to maintain 2 replicas(containers ) of nodejs application
– name, the name of the service
– dockerreg:5000/swarm:latest, my nodejs application docker image stored in docker private registry.

If you check in two docker nodes, using “docker ps” command you can check number for containers running on each node.

For example here the containers running on Manager node. Don’t get confused with registry container. It is my private Docker registry


root@proxmox:/home/manage# docker ps
0baf5564a5c5 dockerreg:5000/swarm:latest "/bin/sh -c 'node /t…" 26 seconds ago Up 23 seconds 8081/tcp nodejs.1.ja2wx4r0tgzskzse463t07p49
1131a0cc911d registry "/entrypoint.sh /etc…" 10 days ago Up 30 minutes>5000/tcp musing_beaver

For example here the containers running on the worker node.

[root@localhost ~]# docker ps
3a5a1e630e48 dockerreg:5000/swarm:latest "/bin/sh -c 'node /t…" About an hour ago Up About an hour 8081/tcp nodejs.2.y2jj7ko73ba64kzgzvg13mbv7

So now you will have a question, what will happen when the worker node dies?
Whenever the manager node detects worker node is unresponsive, new containers will get created in the existing worker node/manager node in order to maintain the replicas.

Increase the scale of your service

Now you can increase the replicas of containers without disturbing the running service, execute the below command to perform this


docker service scale nodejs=5
nodejs scaled to 5
overall progress: 5 out of 5 tasks 
1/5: running 
2/5: running 
3/5: running 
4/5: running 
5/5: running 
verify: Service converged

Now check the number of containers in each node

Manager Node

docker ps
038a7a4d5d9b dockerreg:5000/swarm:latest "/bin/sh -c 'node /t…" 36 seconds ago Up 35 seconds 8081/tcp nodejs.5.n7omgfrych1tz8qog0q4qetre
efedd0e6af7f dockerreg:5000/swarm:latest "/bin/sh -c 'node /t…" 36 seconds ago Up 35 seconds 8081/tcp nodejs.4.ikt5eo7qkismejnr8bnjcnrxk
3a5a1e630e48 dockerreg:5000/swarm:latest "/bin/sh -c 'node /t…" About an hour ago Up About an hour 8081/tcp nodejs.2.y2jj7ko73ba64kzgzvg13mbv7

Worker Node

root@proxmox:/home/manage# docker ps
bf46989e5063 dockerreg:5000/swarm:latest "/bin/sh -c 'node /t…" 32 seconds ago Up 31 seconds 8081/tcp nodejs.3.lk0l4kmvu9itdt0veh04y0c7l
0baf5564a5c5 dockerreg:5000/swarm:latest "/bin/sh -c 'node /t…" About an hour ago Up About an hour 8081/tcp nodejs.1.ja2wx4r0tgzskzse463t07p49
1131a0cc911d registry "/entrypoint.sh /etc…" 11 days ago Up 2 hours>5000/tcp musing_beaver

Remove a service
To remove a service from docker swarm, execute the below command with the service name.

docker service rm nodejs

Publish port of the service in order to access from outside

In my example, my docker nodejs application is running on port number 8081. In order to access my application from the outside bind host port to the Docker container.
Once we do this application can be accessed from any node using host bound port number.

docker service create --replicas 10 --name nodejs --publish published=8081,target=8081 dockerreg:5000/swarm:latest

Here is the diagram of docker swarm cluster which gives an idea about swarm load balancer and how it containers are accessed from outside using the published port.
Containers in the swarm can be accessed using any nodes hosts published port.

You can use any external load balancer such as Nginx, Haproxy to route the requests to the docker swarm service. Nginx/Haproxy can have any type of load balancer or failover to connect the worker nodes.
Here is the sample diagram when configuring an external load balancer.

I hope you understood about docker swarm, subscribe our newsletters for more articles.

happy learning 🙂

© 2019, Techrunnr. All rights reserved.

Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!


Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.