Bird
Raised Fist0
Dockerdevops~20 mins

Deploying services in Swarm in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swarm Service Deployment Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Service replicas count after deployment

You deploy a service in Docker Swarm with the command:

docker service create --name webapp --replicas 3 nginx

What will be the output of docker service ls regarding the replicas column immediately after deployment?

Awebapp nginx replicated 0/3 running
Bwebapp nginx global 3/3 running
Cwebapp nginx replicated 3/0 running
Dwebapp nginx replicated 3/3 running
Attempts:
2 left
💡 Hint

Think about how quickly replicas start after service creation.

❓ Configuration
intermediate
2:00remaining
Correct syntax for updating service replicas

You want to update an existing service named api to have 5 replicas. Which command is correct?

Adocker service update --replicas=5 api
Bdocker service update api --replicas 5
Cdocker service scale api=5
Ddocker service update --replica 5 api
Attempts:
2 left
💡 Hint

Check the correct order and spelling of flags in docker service update.

❓ Troubleshoot
advanced
2:00remaining
Service fails to deploy with port conflict

You try to deploy a service with this command:

docker service create --name web --publish 80:80 nginx

But it fails with an error about port binding. What is the most likely cause?

APort 80 is already in use on one or more swarm nodes
BThe nginx image does not expose port 80
CThe service name 'web' is already used by another service
DDocker Swarm does not support publishing ports
Attempts:
2 left
💡 Hint

Consider what prevents binding to a port on the host.

🔀 Workflow
advanced
3:00remaining
Order of commands to deploy and scale a service

Arrange the steps in the correct order to deploy a service named db with 2 replicas and then scale it to 4 replicas.

A1,4,2,3
B2,1,3,4
C1,3,2,4
D1,2,4,3
Attempts:
2 left
💡 Hint

Think about deploying first, then checking status, then scaling.

✅ Best Practice
expert
3:00remaining
Ensuring zero downtime during service update

You want to update a service's image without downtime. Which option ensures rolling updates with minimal disruption?

Adocker service update --image newimage:latest --force myservice
Bdocker service update --image newimage:latest --rollback myservice
Cdocker service update --image newimage:latest --update-parallelism 1 --update-delay 10s myservice
Ddocker service update --image newimage:latest --replicas 0 myservice
Attempts:
2 left
💡 Hint

Look for options controlling update speed and parallelism.

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