Docker Swarm Mode (original) (raw)

Last Updated : 23 Jul, 2025

Docker swarm is a container orchestration tool. Swarm Mode in Docker was introduced in version 1.12, enabling the ability to deploy multiple containers on multiple Docker hosts. For this Docker uses 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.

Tasks and Services in Docker Swarm

In Docker Swarm, a **Service is a higher-level abstraction used to define how containers should be deployed, managed, and scaled across a swarm of Docker nodes. It includes specifications like the number of replicas, networking, and load balancing.

A **Task is an individual instance of a container that is created and managed by a service. Each task represents a single unit of work and is scheduled to run on one of the nodes in the swarm. Tasks are the actual running containers that fulfill the requirements specified by the service.

Working of Docker Swarm

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

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

The following are the features of Docker Swarm:

Use Case of Docker Swarm

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.

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:

Install and Setup Docker Swarm on AWS

**Step 1: Login to AWS Console

Console-Home

**Step 2: Launch the EC2 Instances

Launching Ec2 Instances

Choosing-Ami-and-instance-types

**Step 3: Specify the inbound Security rules

Specifying-the-inbound-security-rule

**Step 4: Specify the Metadata to instances to automate the docker setup process to all the nodes at once.

Specifying-the-metadata-in-aws-instance

( or )

#!/bin/bash

Update package lists and install dependencies

sudo apt-get update -y && sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
software-properties-common

Add Docker’s official GPG key

curl -fsSL https://download.docker.com/linux/ubuntu//gpg | sudo apt-key add -

Set up the stable repository for Docker

sudo add-apt-repository -y \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu/ \
$(lsb_release -cs) \
stable"

Update the package database with Docker packages from the newly added repo

sudo apt-get update -y

Install Docker

sudo apt-get install -y docker-ce

Start Docker service

sudo systemctl start docker

Enable Docker to start on boot

sudo systemctl enable docker

Install Docksal

sudo curl -fsSL ger.docksal.io | bash

Add the current user to the docker group

sudo usermod -aG docker "${USER}"

**Step 5: Specify the names to the instances after once they created as shown in the below figure:

Specifying-names-to-the-instances

Setup Docker Swarm Mode

**Step 6: Start Docker Swarm

docker swarm init

docker-swarm-init

**Step 7: Joining Docker Swarm Nodes

Joining Docker Swarm Node 1

Joining-to-Docker-Swarm-Node-2-

Create a Task in Docker Swarm Cluster

**Step 8: Create a Task in Docker Swarm

docker service create --name myjavawebapp -p 8080:8080 bnvschaitanya/java-web-app

Creating-Task-in-Cluster

**Step 9: Create a Replica to Task

docker service scale myjavawebapp=4

Scaling-the-Task

**Step 10: Access the application through browser using any of your cluster instance ip with 8080 port as follows:

http://54.163.46.161:8080

Accessing-the-server

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 initializing the swarm mode know add the nodes into the docker swarm cluster. When you initialize 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

**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.

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 be assured 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.

Docker Swarm Mode Key Concepts

The following are the docker swarm mode key concepts:

**Docker Swarm Filters

The following are the docker swarm filters:

Docker Swarm Mode CLI Commands

The following are the docker swarm mode CLI commands:

docker swarm init [OPTIONS]

docker swarm join [OPTIONS] HOST:PORT

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

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

docker service ls [OPTIONS]

docker service rm SERVICE [SERVICE...]

Docker Container vs Docker Swarm

The following are the difference between Docker Container and Docker Swarm:

Docker Container Docker 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

The following are the docker swarm and Kubernetes:

Kubernetes Docker Swarm
Kubernetes is an open-source platform used for maintaining and deploying a group of containers Docker 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.

Docker Service vs Docker Task

The following are the differences between docker service and docker task:

Aspect Docker Service Docker Task
**Definition A higher-level abstraction that defines how to run containers as a group An individual instance of a container created by a service
**Purpose Manages the deployment and scaling of a group of containers Executes a single unit of work within a service
**Management Handles orchestration, load balancing, and scaling Represents the actual work being performed by a container
**Scalability Easily scaled up or down to manage load Cannot be individually scaled; scaling is done at the service level
**State Desired state defined (e.g., number of replicas) Current state of a specific container instance
**Failure Handling Automatically reschedules tasks if a container fails Does not manage failures; relies on service for rescheduling

Benefits of Docker Swarm

The following are the benefits of Docker Swarm: