Open In App
Related Articles

Introduction to Docker Swarm Mode

Like Article
Save Article
Report issue

Docker swarm is a container orchestration tool. Swarm Mode in Docker was introduced in version 1.12 which enables the ability to deploy multiple containers on multiple Docker hosts. For this Docker use an overlay network for the service discovery and with a built-in load balancer for scaling the services. One of the key advantages of docker swarm is that the configuration of the docker services such as volume and network can be modified without the need to manually restart the service Docker will update the configuration, stop the service tasks with the out-of-date configuration, and create new ones matching the desired configuration.

How Does Docker Swarm Work?

When you want to deploy a container in the swarm first, you have to launch services. Service consists of multiple containers of the same image. These services are deployed inside a node so to deploy a swarm at least one node has to be deployed. As you see below diagram the manager node is responsible for the allocation of the task, dispatch the tasks, and schedule the tasks. API in the manager is the medium between the manager node and the worker node to communicate with each other by using the HTTP protocol. The service of one cluster can be used by the other. All the execution of the task is performed by the worker node.

Docker Swarm Init

To initialize the docker swarm cluster we use the command called “docker swarm init”. For converting the docker engine to the swarm manager we will use this command after converting into the swarm mode then you will able the manager and the worker nodes then the swarm will distribute the work across them.

After Initializing The Swarm Mode

  • After initializing the docker swarm init it will bring the swarm into existence.
  • After bringing the swarm into existence know it will turn the current node into a manager node. And also it will generate one token.
  • The token that is generated will be used for the further joining of the worker nodes and master nodes.

Docker Swarm Architecture

There are two types of nodes in Docker Swarm:

  1. Manager node: Carries out and oversees cluster-level duties. 
  2. Worker node: Receives and completes the tasks set by the manager node.
Docker Swarm Architecture

A single manager node can be created but the worker node can not be created without a manager node. The ideal number for the count of the manager node is seven. Increasing the number of the manager node does not mean that the scalability will increase.

Features of Docker Swarm

  1. Cluster management:- To create Swarm you can use the Docker engine CLI where you can deploy the applications. Additional orchestration software is not required to manage a swarm.
  2. Multi-host networking:- Swarm can contain multiple overlay networks so while deploying the service you can specify the network on which you want to deploy your service. The swarm manager automatically assigns addresses to the containers on the overlay network when it initializes or updates the application.
  3. Load balancing:- While deploying any service on a particular port the swarm automatically balances the load of these ports.
  4. Scaling:- When you scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state.

What is Docker Swarm Used For?

Docker swarm is a container orchestration tool that is used to Docker containers and scale them. Instead of a single host with the help of Docker Swarm, we can manage multiple nodes which are called clusters where we can deploy and maintain our containers in multiple hosts.

  1. Docker Swarm assures that our application is always available even if the one of nodes fails by creating the container in another node which is available
  2. Based on the incoming traffic we can scale the containers up and down by adding to the multiple nodes.
  3. If there are multiple containers the incoming load will be balanced automatically by the Docker Swarm.
  4. Docker Swarm has a number of security features, including traffic encryption between nodes and mutual TLS authentication.
  5. Docker Swarm will automatically take care of failed containers and nodes. By this, we can maintain high availability.

Simply Docker Swarm is mainly used to deploy, scale, and manage the containers and nodes which are available in the cluster.

Different Modes of Docker Swarm

Docker Swarm mainly consists of two modes they are:

  1. Global Mode: In this mode, Docker Swarm will maintain containers in all the slave nodes and master nodes. It will maintain the replicas of containers in all the nodes which are available in the cluster.
  2. Replicated Mode: In this mode, Docker Swarm will deploy the containers based on the no.of replicas required for you. If you required 3 replicas it will deploy the containers based on the node availability.

Getting Started With Swarm Mode

Follow the steps mentioned below to get familiar with the docker swarm mode.

Step 1: In the first step we need to initialize the docker engines of swarm mode.

Step 2: After instilizing the swarm mode know add the nodes into the docker swarm cluster. When you intilize the swarm mode it will generates two tokens one is for the manger node and another is for the worker node by using following command you can join the nodes according to the requirement.

docker swarm init <Token>

Step 3: Know you can start deploying the you application in the form of containers in docker swarm. Docker swarm will take care of deployment of application an scaling of the application across the worker nodes.

Step 4: WIth the help od docker swarm cli you can manage the swarm like adding or removing the worker nodes scaling the services and inspecting the swarm state.

What is Stack in Docker Swarm?

A stack is nothing but a collection of one or more services deployed as a single unit. The stack is deployed by using compose file in which you can mention the service of the stack and all the required configurations to deploy the stack.

With the help of stack, it is very easy to deploy and maintain complex containers like multi-containers in the Docker swarm. We can deploy with the help of a single file called Docker Compose. yaml we can define all the necessary configurations. You can assure that they deployed and scaled together.

If we deploy the new version of the application and the end users find any bugs while using it you can roll back to the previous version of the application by using Docker Swarm.

Note: Stack is mainly used to deploy the multi-container application with ease.

Swarm Mode Key Concepts

  • Node: A Node is an instance of a Docker engine that connects to the Swarm. You can run one or more nodes on a single physical computer or cloud server. Nodes can be either a manager or workers. The manager node dispatches units of work called tasks to worker nodes. Worker nodes receive and execute tasks dispatched from manager nodes.
  • Services: A service is a high-level concept relating to a collection of tasks to be executed by workers. An example of a service is an HTTP Server running as a Docker Container on three nodes.
  • Load Balancing: Docker includes a load balancer to process requests across all containers in the service.

Docker Swarm Filters

  • Constraints: Based on conditions, users are restricted from creating containers on particular Docker hosts.
  • Drain Node: If we applied Drain Nodes on any node then Docker swarm will not assign any replicas to that node. 
  • Port: Avoids the port conflicts between the application by deploying the same port applications in two different nodes.

Docker Swarm Mode CLI Commands

docker swarm init: This command is used to initialize the swarm.      

docker swarm init [OPTIONS]

docker swarm join: By using this command you can join a node to a swarm. The node joins as a manager node or worker node based on the token you pass with the –token flag.

docker swarm join [OPTIONS] HOST:PORT

docker service creates: This is a cluster management command, and must be executed on a Swarm manager node.

docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]

docker service inspects: This command is used to inspect the particular service and all the details will display in JSON format.

docker service inspect [OPTIONS] SERVICE [SERVICE...]

docker service ls: This command is used to see the complete list of all the services in that network.

docker service ls [OPTIONS]

docker service rm: This command is used to remove the specific service you want to remove.

docker service rm SERVICE [SERVICE...]

Docker Container vs Docker Swarm

Docker ContainerDocker Swarm

Docker Container is an executable package that consists of all the code and dependencies.

Docker Swarm is the container orchestration tool that manages all the containers available in the Swarm cluster.

The container can be run on any OS as Docker is an underlying runtime.Docker Swarm will manage the cluster which consists of Docker nodes.

A single, isolated, and self-contained unit called a “Docker container” is capable of running an application.

You may manage many Docker containers across various servers in a cluster using the technology known as Docker Swarm.

Docker containers are more suitable for microservices applications than monolithic applications.

Docker Swarm will scale, deploy and balance the containers.

Docker Swarm vs Kubernetes 


   Docker Swarm 

Kubernetes is an open-source platform used for maintaining and deploying a group of containersDocker Swarm is designed to be a simple and lightweight container orchestration.
In practice, Kubernetes is most commonly used alongside Docker for better control and implantation of containerized applications.With Docker swarm, multiple containers run on the same hardware much more efficiently than the VM environment & productivity of Docker is extremely high. 
Applications are deployed as a combination of pods, Deployment, and services. Apps are deployed in the form of services.
It supports auto-scaling of the container in a cluster. With more efficient Docker Swarm does support auto-scaling but is not as efficient as docker swarm.
The health check is of two kinds: liveness and readiness.Health checks are limited to service.
Hard to set up and configure.Docker swarm setup and installation are easy.

It does not have extensive documentation but is quite less than Docker Swarm. But it does include everything from installation to deployment. 

Docker swarm documentation is more effective, more extensive, and has even more capabilities & it includes everything from installation to deployment & quick-start instructions as well as a more detailed tutorial. 

Kubernetes installation is provided to be quite difficult than Docker swarm and even the command for Kubernetes is quite more complex than Docker swarm.

Docker swarm installation is quite easier, by using fewer commands you can install Docker in your virtual machine or even on the cloud.

Azure, buffer, intel, Evernote, and Shopify Using Kubernetes. Citizens Bank, and MetLife companies using Docker swarm. 

FAQs On Docker Swarm

1. Is Docker Swarm Used in Production?

Yes Docker Swarm is used in the production environments but it is not much used as the kubernetes in the production environment. Kubernetes had more advantages then the docker swarm.

2. Is Docker Swarm Free?

Yes Docker swarm is free to use. You can run the docker swarm on as many as docker engine instances as you want without incurring any licensing costs.

3. What Ports are Used by Docker Swarm?

  • TCP Port for the manager node: 2377
  • Worker node TCP port: 2376

4. Is Docker Swarm a Load Balancer?

Docker swarm itself isn’t a full-fledged load balancer but you can achieve this by using two mechanisms.

  • Overlay routing mesh
  • Ingress

    Last Updated : 08 Dec, 2023
    Like Article
    Save Article
    Share your thoughts in the comments
    Similar Reads