Everything with Docker Swarm!

Mahedi Hasan Jisan
4 min readNov 11, 2021
https://labs.play-with-docker.com/

Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines.

Before you start the practice on docker swarm, read about the swarm, nodes, services and tasks, and load balancing.

Exercise: play with docker!

Step 1: Create machine instances and cluster, in: https://labs.play-with-docker.com/

For example, let’s choose node3 as our manager node where we will initialize the docker swarm cluster.
Command:

docker swarm init --advertise-addr eth0

Therefore, node3 will be the manager and we can add the other two-node or machine instance that we created as workers. Those workers will join this cluster.

How do we do that?

When you create a swarm cluster such as in the above example, you will be able to see a command like this:

docker swarm join --token SWMTKN-1-2.... {ip}:2377

Run that command in the other two machines and that will make those two nodes as workers. Let’s verify that from the manager node (node3):

Now, we can verify that node3 is the leader whereas node1 and node2 are the workers. Read Documentation to understand the difference between managers and workers. However, in a short version:

A swarm consists of multiple Docker hosts which run in swarm mode and act as managers (to manage membership and delegation) and workers (which run swarm services). A given Docker host can be a manager, a worker, or perform both roles. When you create a service, you define its optimal state (number of replicas, network and storage resources available to it, ports the service exposes to the outside world, and more). Docker works to maintain that desired state. For instance, if a worker node becomes unavailable, Docker schedules that node’s tasks on other nodes. A task is a running container which is part of a swarm service and managed by a swarm manager, as opposed to a standalone container.

We can see the cluster visually. To make that activate, run the docker visualizer container in the manager node.
docker service create --name=viz \
-p=8081:8080 \
--constraint=node.role==manager \
--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
dockersamples/visualizer:stable

Now open port 8081 from the manager node. You will be able to see something like this:

Cluster Visualization!

To delete the visualization service:

$ docker service rm viz

Now, let’s re-deploy that visualization service using the docker-compose file. Create a file name “docker-visualize.yml” and edit the following inside it.

Click on the “editor” to go to that page! And I am pretty sure you guys know how to write the docker-compose file.

To re-deploy, follow these steps:

What is stack deploy?

When running Docker Engine in swarm mode, you can use docker stack deploy it to deploy a complete application stack to the swarm. The deploy command accepts a stack description in the form of a Compose file. The docker stack deploy command supports any Compose file of version “3.0” or above. Read here for details.

Create a complete Docker Swarm stack

We are going to look into an existing app to see it in effect in the cluster.

App descriptions:

  • A Python web app that lets you vote between two options
  • A Redis queue that collects new votes
  • A .NET worker which consumes votes and stores them in…
  • A PostgreSQL database backed by a Docker volume
  • A Node.js web app that shows the results of the voting in real-time

docker-compose.yml:

Now, go ahead and do the stack deployment.

Let’s check the visualizer to see if the worker is working properly:

Yes, they are working! You can see all five containers in the visualizer and that they turn green. You should also check that you can see the two frontends at ports 5000 and 5001.

That’s pretty much it about docker swarm. However, there are some shortcomings on the docker swarm where docker Kubernetes comes in. Both of them work well together. Maybe, next article will be about Kubernetes. That’s it for today. Cheers! 👌

--

--