Technical skill

Docker swarm

Keep an application running across computers

Imagine a website is packaged in a container and needs to run on several computers. Someone must decide which computer runs each copy and replace a copy if it stops. Docker Swarm mode is the cluster-management feature built into Docker Engine that does this work across a group of Docker Engines. Each Engine in the group is called a node.

Manager nodes keep the cluster's state and assign work. Worker nodes receive and run assigned work. By default, a manager can also run application tasks, though it can be set to do management only. A service is the cluster-wide plan for an application: it specifies the image, command, number of copies, published ports, networks, and resource needs. The manager compares this plan with what is running and schedules tasks to make the observed state match.

A service, a task, and a container are different parts of the plan. A service is the cluster-level definition. The scheduler assigns tasks to nodes; each task runs a container and has its own lifecycle. A replicated service asks for a chosen number of tasks. A global service asks for one task on each eligible node. Constraints and resource needs can leave a task waiting if no node qualifies, so requesting several copies does not guarantee that they are all running.

After intentionally initializing a swarm and checking the active context, this command creates a three-replica Nginx service:

docker service create --name web --replicas 3 --publish published=8080,target=80 nginx

It requests three tasks from the nginx image and publishes container port 80 through port 8080. Swarm chooses which nodes run those tasks. The command does not configure application data, TLS, or external firewall rules; those are separate deployment decisions. Creating a service changes cluster state, so use it only where that change is intended.

To inspect membership, docker node ls lists nodes and roles. docker service ls shows services and replica counts, while docker service ps web shows placement and task status for web. A service with fewer running tasks than desired may be blocked by resource constraints, a node being unavailable, a failed image download, or another task error. Read task details as well as the service summary.

docker service uses Swarm's desired-state model; docker run starts a standalone container. Docker Compose can define applications too, but its target and deployment workflow depend on how it is used. Check the Docker Engine documentation for the version in use before relying on an option or behavior.

Interactive exercises

Materials

Skillsets
Skillset Web Backend Developer preview
image descriptionWeb Backend Developer
by Infinite Education
2092 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset DevOps engineer preview
image descriptionDevOps engineer
by Infinite Education
1211 users
Skillset DevOps preview
image descriptionDevOps
by Federico Cordone
3 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful