Bird
Raised Fist0
Dockerdevops~5 mins

Deploying services in Swarm in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you want to run your app across multiple computers easily, Docker Swarm helps by managing many containers as one group. Deploying services in Swarm means starting your app so it runs reliably and can handle more users by spreading work across machines.
When you want your app to keep running even if one computer fails
When you need to run multiple copies of your app to handle more users
When you want to update your app without stopping it completely
When you want to manage many containers from one place
When you want to balance work evenly across several servers
Commands
This command starts Docker Swarm mode on your current machine, making it the manager that controls the cluster.
Terminal
docker swarm init
Expected OutputExpected
Swarm initialized: current node (abcdef123456) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-0abcdef1234567890abcdef1234567890abcdef1234567890abcdef123456 192.168.1.100:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
This command deploys a service named 'my-web' running the nginx web server with 3 copies (replicas). It also maps port 8080 on your machine to port 80 in the containers so you can access the web server.
Terminal
docker service create --name my-web --replicas 3 -p 8080:80 nginx:1.23
Expected OutputExpected
q1w2e3r4t5y6z7x8y9a0b1c2d3e4f5g6h7i8j9k0l1m2n3o4p5q6r7s8t9u0v1w2
→
--name - Sets the name of the service for easy identification
→
--replicas - Specifies how many copies of the service to run
→
-p - Maps a port on the host to a port in the container
This command lists all running services in the swarm so you can check if your service is active and how many replicas are running.
Terminal
docker service ls
Expected OutputExpected
ID NAME MODE REPLICAS IMAGE PORTS q1w2e3r4t5y6 my-web replicated 3/3 nginx:1.23 *:8080->80/tcp
This command shows detailed information about the tasks (containers) running for the 'my-web' service, including their current state and which node they run on.
Terminal
docker service ps my-web
Expected OutputExpected
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS x1y2z3a4b5c6 my-web.1 nginx:1.23 node-1 Running Running 2 minutes ago x7y8z9a0b1c2 my-web.2 nginx:1.23 node-2 Running Running 2 minutes ago x3y4z5a6b7c8 my-web.3 nginx:1.23 node-3 Running Running 2 minutes ago
Key Concept

If you remember nothing else from this pattern, remember: Docker Swarm lets you run and manage many copies of your app easily across multiple machines with simple commands.

Common Mistakes
Trying to deploy a service before running 'docker swarm init'
The swarm mode is not active, so Docker cannot manage services across nodes.
Always run 'docker swarm init' first to enable swarm mode on your machine.
Not specifying the number of replicas when creating a service
Docker will create only one replica by default, which may not provide the desired availability or load distribution.
Use the '--replicas' flag to set how many copies of the service you want.
Forgetting to map ports with '-p' when deploying a web service
Without port mapping, you cannot access the service from outside the swarm nodes.
Use '-p host_port:container_port' to expose the service ports properly.
Summary
Initialize swarm mode with 'docker swarm init' to start managing services.
Deploy services using 'docker service create' with options for replicas and port mapping.
Check running services with 'docker service ls' and inspect tasks with 'docker service ps'.

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