Bird
Raised Fist0
Dockerdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the docker service create command do in Docker Swarm?
easy
A. It deploys a new service across the Swarm cluster.
B. It removes an existing service from the Swarm.
C. It lists all running containers on a single node.
D. It initializes a new Swarm cluster.

Solution

  1. Step 1: Understand the purpose of docker service create

    This command is used to deploy a new service that runs containers across the Swarm cluster nodes.
  2. Step 2: Differentiate from other commands

    Removing services uses docker service rm, listing containers uses docker ps, and initializing Swarm uses docker swarm init.
  3. Final Answer:

    It deploys a new service across the Swarm cluster. -> Option A
  4. Quick Check:

    docker service create = deploy service [OK]
Hint: Create services to deploy apps on many nodes [OK]
Common Mistakes:
  • Confusing service creation with Swarm initialization
  • Mixing service removal with creation
  • Thinking it lists containers instead of services
2. Which of the following is the correct syntax to create a service with 3 replicas and publish port 80 to 8080?
easy
A. docker service create --replicas 3 --publish 8080:80 myimage
B. docker service create --publish 3 --replicas 80:8080 myimage
C. docker service create --replicas=3 --publish=80:8080 myimage
D. docker service create --replicas 3 --publish 80:8080 myimage

Solution

  1. Step 1: Check correct option format for replicas and publish

    The correct syntax uses --replicas 3 and --publish 8080:80 to map port 8080 on the host to port 80 in the container.
  2. Step 2: Validate port mapping order and syntax

    Port mapping is hostPort:containerPort, so 8080:80 is correct. Options B, C, and D have wrong order or syntax.
  3. Final Answer:

    docker service create --replicas 3 --publish 8080:80 myimage -> Option A
  4. Quick Check:

    Replicas and publish flags syntax = docker service create --replicas 3 --publish 8080:80 myimage [OK]
Hint: Use --replicas N and --publish hostPort:containerPort [OK]
Common Mistakes:
  • Reversing port numbers in --publish
  • Using equal signs incorrectly
  • Mixing replicas and publish values
3. What will be the output of the command docker service ls after running docker service create --name webapp --replicas 2 --publish 80:80 myimage:latest?
medium
A. Error: service not found
B. No services found
C. ID NAME MODE REPLICAS IMAGE PORTS abc123 webapp replicated 2/2 myimage:latest *:80->80/tcp
D. ID NAME MODE REPLICAS IMAGE PORTS abc123 webapp global 1/2 myimage:latest *:80->80/tcp

Solution

  1. Step 1: Understand docker service ls output format

    This command lists services with columns: ID, NAME, MODE, REPLICAS, IMAGE, PORTS.
  2. Step 2: Match service details with created service

    Since the service webapp has 2 replicas in replicated mode, the REPLICAS column shows 2/2. The ports show mapping like *:80->80/tcp.
  3. Final Answer:

    ID NAME MODE REPLICAS IMAGE PORTS abc123 webapp replicated 2/2 myimage:latest *:80->80/tcp -> Option C
  4. Quick Check:

    Service list shows replicas count correctly [OK]
Hint: Replicas show as running/desired in service list [OK]
Common Mistakes:
  • Expecting 'global' mode instead of 'replicated'
  • Thinking no services appear after creation
  • Confusing error messages with normal output
4. You ran docker service create --replicas 3 --publish 80:80 myimage but only 1 replica is running. What is the likely cause?
medium
A. The image name is incorrect causing failure.
B. The command syntax is invalid and service was not created.
C. The port mapping syntax is wrong and blocks replicas.
D. Swarm has only one node available to run the replicas.

Solution

  1. Step 1: Check cluster node availability

    If only one node is in the Swarm, it can run only one replica at a time if resources are limited or constraints exist.
  2. Step 2: Verify command correctness and image

    The command syntax and image name are valid, so the issue is not syntax or image error.
  3. Final Answer:

    Swarm has only one node available to run the replicas. -> Option D
  4. Quick Check:

    Replica count depends on node availability [OK]
Hint: Check number of nodes to match replica count [OK]
Common Mistakes:
  • Assuming port syntax blocks replicas
  • Blaming image name without error messages
  • Thinking syntax error stops service creation
5. You want to deploy a service named api with 4 replicas, publish port 5000, and ensure it restarts automatically on failure. Which command is correct?
hard
A. docker service create --name api --replicas 4 --publish 5000:5000 myapiimage --restart-condition on-failure
B. docker service create --name api --replicas 4 --publish 5000:5000 --restart-condition on-failure myapiimage
C. docker service create --name api --replicas 4 --publish 5000:5000 --restart-condition failure myapiimage
D. docker service create --name api --replicas 4 --publish 5000:5000 --restart on-failure myapiimage

Solution

  1. Step 1: Identify correct restart flag for services

    Docker Swarm uses --restart-condition to specify restart behavior, such as on-failure.
  2. Step 2: Check option placement and syntax

    Options must be before the image name. docker service create --name api --replicas 4 --publish 5000:5000 --restart-condition on-failure myapiimage correctly places --restart-condition on-failure before the image name. Options B and C use invalid flags or values. docker service create --name api --replicas 4 --publish 5000:5000 myapiimage --restart-condition on-failure places the flag after the image name, which is invalid.
  3. Final Answer:

    docker service create --name api --replicas 4 --publish 5000:5000 --restart-condition on-failure myapiimage -> Option B
  4. Quick Check:

    Use --restart-condition on-failure before image [OK]
Hint: Use --restart-condition on-failure before image name [OK]
Common Mistakes:
  • Using --restart instead of --restart-condition
  • Placing flags after image name
  • Using invalid restart condition values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard