Bird
Raised Fist0
Dockerdevops~30 mins

Deploying services in Swarm in Docker - Mini Project: Build & Apply

Choose your learning style10 modes available
Deploying services in Swarm
📖 Scenario: You are managing a small web application that needs to run on multiple Docker nodes using Docker Swarm. You want to deploy a simple web service that can scale easily and be managed centrally.
🎯 Goal: Learn how to create a Docker Swarm service, configure its replicas, and verify the deployment status.
📋 What You'll Learn
Initialize Docker Swarm mode on your machine
Create a Docker service running the nginx image
Set the number of replicas for the service
Check the status of the deployed service
💡 Why This Matters
🌍 Real World
Docker Swarm is used to manage containerized applications across multiple machines, making it easier to deploy, scale, and maintain services.
💼 Career
Understanding how to deploy and manage services in Docker Swarm is essential for DevOps roles that involve container orchestration and cloud-native application management.
Progress0 / 4 steps
1
Initialize Docker Swarm
Run the command docker swarm init to initialize Docker Swarm mode on your machine.
Docker
Hint

Use the docker swarm init command to start Swarm mode on your local Docker engine.

2
Create a Docker service
Create a Docker service named webserver using the nginx:latest image with 3 replicas by running the command docker service create --name webserver --replicas 3 nginx:latest.
Docker
Hint

Use docker service create with the --name and --replicas options to deploy the service.

3
Check the service status
Run the command docker service ls to list all running services and verify that the webserver service is running with 3/3 replicas.
Docker
Hint

Use docker service ls to see the list of services and their replica counts.

4
Display the service details
Run the command docker service ps webserver to display detailed information about the tasks of the webserver service.
Docker
Hint

Use docker service ps webserver to see the status of each replica task.

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