0
0
Dockerdevops~10 mins

Deploying services in Swarm in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Deploying services in Swarm
Initialize Docker Swarm
Create Service Definition
Run 'docker service create'
Swarm Scheduler Assigns Tasks
Tasks Run on Worker Nodes
Service is Running & Scaled
Monitor & Update Service
This flow shows how a Docker Swarm service is deployed: starting Swarm, creating a service, scheduling tasks, running containers, and managing the service.
Execution Sample
Docker
docker swarm init

docker service create --name webserver -p 80:80 nginx

docker service ls

docker service ps webserver
This code initializes a swarm, deploys an nginx service named 'webserver' exposing port 80, then lists services and their tasks.
Process Table
StepCommandActionResult/Output
1docker swarm initInitialize swarm mode on manager nodeSwarm initialized: current node is now a manager
2docker service create --name webserver -p 80:80 nginxCreate and deploy 'webserver' service with nginx imageService webserver created with 1 replica
3docker service lsList all running servicesID NAME MODE REPLICAS IMAGE xyz webserver replicated 1/1 nginx
4docker service ps webserverShow tasks for 'webserver' serviceID NAME NODE DESIRED STATE CURRENT STATE abc webserver.1 manager Running Running 5 seconds ago
5docker service scale webserver=3Scale service to 3 replicaswebserver scaled to 3
6docker service ps webserverShow updated tasks3 tasks listed, all Running on nodes
7docker service rm webserverRemove the servicewebserver removed
8-No service runningNo services listed
9-End of deployment lifecycleDeployment stopped
💡 Service removed and no tasks remain, deployment lifecycle ends.
Status Tracker
VariableStartAfter Step 2After Step 5After Step 7
Swarm ModeDisabledEnabledEnabledEnabled
Service 'webserver' Replicas0130
Service StatusNoneCreatedScaledRemoved
Key Moments - 3 Insights
Why does the service have only 1 replica after creation?
By default, 'docker service create' deploys 1 replica unless scaled later, as shown in execution_table step 2 and step 5.
What happens when you run 'docker swarm init' on a node?
It enables swarm mode on that node, making it a manager able to deploy services, as seen in execution_table step 1.
How do tasks relate to service replicas?
Each replica corresponds to a task running a container; scaling changes the number of tasks, shown in steps 4 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, after which step does the service run 3 replicas?
AStep 2
BStep 5
CStep 3
DStep 7
💡 Hint
Check the 'Service Replicas' variable in variable_tracker after Step 5.
At which step is the swarm mode enabled on the node?
AStep 1
BStep 2
CStep 4
DStep 7
💡 Hint
Look at execution_table step 1 and variable_tracker 'Swarm Mode' variable.
If you remove the service at step 7, what is the number of replicas after that?
A3
B1
C0
DUndefined
💡 Hint
Refer to variable_tracker 'Service Replicas' after Step 7.
Concept Snapshot
Docker Swarm deploys services as tasks running containers.
Use 'docker swarm init' to start swarm mode.
Create services with 'docker service create'.
Scale replicas with 'docker service scale'.
Monitor with 'docker service ls' and 'docker service ps'.
Remove services with 'docker service rm'.
Full Transcript
This visual execution shows how to deploy services in Docker Swarm. First, you initialize swarm mode on a node using 'docker swarm init'. Then, you create a service with 'docker service create', which deploys one replica by default. You can check running services with 'docker service ls' and see their tasks with 'docker service ps'. To increase the number of replicas, use 'docker service scale'. Finally, you can remove the service with 'docker service rm'. Each step changes the swarm state and service replicas, which is tracked in the variable tracker. This helps beginners understand how services run and scale in a swarm cluster.