Deploying services in Swarm in Docker - Time & Space Complexity
When deploying services in Docker Swarm, it is important to understand how the deployment time changes as the number of services or replicas grows.
We want to know how the work needed to deploy scales with the size of the deployment.
Analyze the time complexity of the following Docker Swarm service deployment command.
docker service create \
--name my_web \
--replicas 5 \
--publish published=80,target=80 \
nginx:latest
This command creates a service named my_web with 5 replicas running the nginx image, publishing port 80.
Look for repeated actions during deployment.
- Primary operation: Creating and starting each replica container.
- How many times: Equal to the number of replicas specified (here 5 times).
The deployment time grows roughly in proportion to the number of replicas.
| Input Size (replicas) | Approx. Operations |
|---|---|
| 10 | 10 container starts |
| 100 | 100 container starts |
| 1000 | 1000 container starts |
Pattern observation: Doubling replicas roughly doubles the deployment work.
Time Complexity: O(n)
This means the deployment time grows linearly with the number of replicas you want to run.
[X] Wrong: "Deploying more replicas happens instantly regardless of number."
[OK] Correct: Each replica requires starting a container, which takes time. More replicas mean more work and longer deployment.
Understanding how deployment time scales helps you design efficient services and explain your choices clearly in discussions.
What if we changed the deployment to update existing replicas instead of creating new ones? How would the time complexity change?
Practice
docker service create command do in Docker Swarm?Solution
Step 1: Understand the purpose of
This command is used to deploy a new service that runs containers across the Swarm cluster nodes.docker service createStep 2: Differentiate from other commands
Removing services usesdocker service rm, listing containers usesdocker ps, and initializing Swarm usesdocker swarm init.Final Answer:
It deploys a new service across the Swarm cluster. -> Option AQuick Check:
docker service create= deploy service [OK]
- Confusing service creation with Swarm initialization
- Mixing service removal with creation
- Thinking it lists containers instead of services
Solution
Step 1: Check correct option format for replicas and publish
The correct syntax uses--replicas 3and--publish 8080:80to map port 8080 on the host to port 80 in the container.Step 2: Validate port mapping order and syntax
Port mapping ishostPort:containerPort, so8080:80is correct. Options B, C, and D have wrong order or syntax.Final Answer:
docker service create --replicas 3 --publish 8080:80 myimage -> Option AQuick Check:
Replicas and publish flags syntax = docker service create --replicas 3 --publish 8080:80 myimage [OK]
- Reversing port numbers in --publish
- Using equal signs incorrectly
- Mixing replicas and publish values
docker service ls after running docker service create --name webapp --replicas 2 --publish 80:80 myimage:latest?Solution
Step 1: Understand
This command lists services with columns: ID, NAME, MODE, REPLICAS, IMAGE, PORTS.docker service lsoutput formatStep 2: Match service details with created service
Since the servicewebapphas 2 replicas in replicated mode, the REPLICAS column shows 2/2. The ports show mapping like *:80->80/tcp.Final Answer:
ID NAME MODE REPLICAS IMAGE PORTS abc123 webapp replicated 2/2 myimage:latest *:80->80/tcp -> Option CQuick Check:
Service list shows replicas count correctly [OK]
- Expecting 'global' mode instead of 'replicated'
- Thinking no services appear after creation
- Confusing error messages with normal output
docker service create --replicas 3 --publish 80:80 myimage but only 1 replica is running. What is the likely cause?Solution
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.Step 2: Verify command correctness and image
The command syntax and image name are valid, so the issue is not syntax or image error.Final Answer:
Swarm has only one node available to run the replicas. -> Option DQuick Check:
Replica count depends on node availability [OK]
- Assuming port syntax blocks replicas
- Blaming image name without error messages
- Thinking syntax error stops service creation
api with 4 replicas, publish port 5000, and ensure it restarts automatically on failure. Which command is correct?Solution
Step 1: Identify correct restart flag for services
Docker Swarm uses--restart-conditionto specify restart behavior, such ason-failure.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-failurebefore 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.Final Answer:
docker service create --name api --replicas 4 --publish 5000:5000 --restart-condition on-failure myapiimage -> Option BQuick Check:
Use --restart-condition on-failure before image [OK]
- Using --restart instead of --restart-condition
- Placing flags after image name
- Using invalid restart condition values
Ready to prove it?
Pass the test and this pattern moves unknown
