📖 Scenario: You are managing a simple web application using Docker Compose. To handle more users, you want to run multiple copies of the web service.
🎯 Goal: Learn how to scale a Docker Compose service by increasing the number of running containers.
📋 What You'll Learn
Create a Docker Compose file with a web service
Add a scale variable to control the number of containers
Use the Docker Compose command to scale the service
Display the number of running containers after scaling
💡 Why This Matters
🌍 Real World
Scaling services with Docker Compose helps handle more users by running multiple copies of the same service.
💼 Career
Understanding service scaling is essential for DevOps roles to manage application availability and load.
Progress0 / 4 steps
1
Create a Docker Compose file with a web service
Create a file named docker-compose.yml with a service called web that uses the image nginx:latest.
Docker
Hint
Use YAML syntax to define the service under services with the name web and specify the image nginx:latest.
2
Add a scale variable to control the number of containers
Create a variable called scale_count and set it to 3 to represent how many containers you want to run.
Docker
Hint
Define a variable named scale_count and assign it the value 3.
3
Use the Docker Compose command to scale the service
Write a shell command string called scale_command that uses docker-compose up -d --scale web=3 to start 3 containers of the web service using the scale_count variable.
Docker
Hint
Use an f-string to insert scale_count into the command string.
4
Display the number of running containers after scaling
Write a print statement that outputs exactly: Running 3 containers of web service using the scale_count variable.
Docker
Hint
Use a print statement with an f-string to show the number of containers running.
Practice
(1/5)
1. What does the command docker service scale myservice=3 do?
easy
A. It stops the service named 'myservice'.
B. It runs 3 copies of the service named 'myservice'.
C. It updates the service 'myservice' to version 3.
D. It removes the service named 'myservice'.
Solution
Step 1: Understand the command structure
The command docker service scale SERVICE_NAME=NUMBER sets the number of replicas for a service.
Step 2: Apply to the given command
Here, myservice=3 means run 3 replicas of the service named 'myservice'.
Final Answer:
It runs 3 copies of the service named 'myservice'. -> Option B
Quick Check:
Scaling sets replicas = 3 [OK]
Hint: Scale sets number of service copies with SERVICE=NUMBER [OK]
Common Mistakes:
Confusing scaling with stopping or removing services
Thinking scale changes service version
Using wrong syntax without '=' sign
2. Which of these is the correct syntax to scale a Docker service named 'webapp' to 5 replicas?
easy
A. docker service scale webapp 5
B. docker service scale -replicas=5 webapp
C. docker service scale webapp=5
D. docker scale service webapp=5
Solution
Step 1: Recall correct scale command syntax
The correct syntax is docker service scale SERVICE_NAME=NUMBER with an equals sign.
Step 2: Match options to syntax
docker service scale webapp=5 uses webapp=5 which matches the correct syntax.
Final Answer:
docker service scale webapp=5 -> Option C
Quick Check:
Correct syntax uses '=' sign [OK]
Hint: Use '=' between service name and replica count [OK]
Common Mistakes:
Omitting '=' sign between service and number
Using wrong command order
Confusing 'docker scale' with 'docker service scale'
3. Given the command docker service scale backend=2 was run on a service with 1 replica, what will be the output of docker service ls for the 'backend' service's REPLICAS column?
medium
A. 2/2
B. 1/2
C. 2/1
D. 1/1
Solution
Step 1: Understand scaling effect on replicas
Scaling to 2 means the service should have 2 running replicas out of 2 desired.
Step 2: Interpret REPLICAS column format
The format is running/desired. After scaling, both running and desired should be 2.
Final Answer:
2/2 -> Option A
Quick Check:
Replicas show running/desired = 2/2 [OK]
Hint: Replicas show running/desired count after scaling [OK]
Common Mistakes:
Mixing running and desired counts order
Assuming replicas don't update immediately
Confusing scale number with current replicas
4. You run docker service scale frontend=abc but get an error. What is the likely cause?
medium
A. The replica count must be a number, not 'abc'.
B. The service name 'frontend' is invalid.
C. You need to use a space instead of '='.
D. Scaling requires the service to be stopped first.
Solution
Step 1: Check the replica count format
The replica count must be a positive integer number, not a string like 'abc'.
Step 2: Validate error cause
Using 'abc' causes a syntax or validation error because replicas must be numeric.
Final Answer:
The replica count must be a number, not 'abc'. -> Option A
Quick Check:
Replica count must be numeric [OK]
Hint: Replica count must be a number, not letters [OK]
Common Mistakes:
Using letters instead of numbers for replicas
Thinking service must be stopped to scale
Using space instead of '=' sign
5. You have a service 'api' currently running 4 replicas. You want to handle a sudden traffic spike by doubling the replicas temporarily. Which command correctly doubles the replicas?
hard
A. docker service scale api=4
B. docker service scale api=16
C. docker service scale api=2
D. docker service scale api=8
Solution
Step 1: Identify current replica count
The service 'api' currently has 4 replicas running.
Step 2: Calculate double replicas
Doubling 4 replicas means setting replicas to 8.
Step 3: Choose correct scale command
The command docker service scale api=8 sets replicas to 8, doubling the current count.
Final Answer:
docker service scale api=8 -> Option D
Quick Check:
Double 4 replicas = 8 [OK]
Hint: Double replicas by setting scale to twice current count [OK]
Common Mistakes:
Setting replicas to current count instead of double
Setting replicas to half or quadruple by mistake
Using wrong syntax for scaling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.