0
0
Dockerdevops~5 mins

Service scaling in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your app needs more power to handle many users at once. Service scaling lets you run more copies of your app easily to share the work and keep things fast.
When your website gets slow because too many people visit at the same time
When you want to test how your app behaves with multiple copies running
When you need to update your app without stopping all users by running new copies first
When you want to balance the work across several servers or containers
When you want to save resources by running fewer copies during quiet times
Commands
This command creates a new service named 'my-web' running the nginx web server with 3 copies (replicas). It also maps port 8080 on your machine to port 80 inside the containers so you can access the web server.
Terminal
docker service create --name my-web --replicas 3 -p 8080:80 nginx
Expected OutputExpected
q1w2e3r4t5y6z7a8b9c0d1e2f3g4h5i6j7k8l9m0n1o2p3q4r5s6t7u8v9w0x1y2z3
--name - Sets the name of the service
--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 so you can see your 'my-web' service and how many replicas it has.
Terminal
docker service ls
Expected OutputExpected
ID NAME MODE REPLICAS IMAGE PORTS q1w2e3r4t5y6 my-web replicated 3/3 nginx:latest *:8080->80/tcp
This command changes the number of copies of the 'my-web' service to 5, adding two more copies to handle more traffic.
Terminal
docker service scale my-web=5
Expected OutputExpected
my-web scaled to 5 overall progress: 5 out of 5 tasks verify: Service converged
This command shows the status of each copy (task) of the 'my-web' service so you can check that all 5 copies are running.
Terminal
docker service ps my-web
Expected OutputExpected
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS x1y2z3a4b5c6 my-web.1 nginx:latest node-1 Running Running 2 minutes ago x2y3z4a5b6c7 my-web.2 nginx:latest node-1 Running Running 2 minutes ago x3y4z5a6b7c8 my-web.3 nginx:latest node-1 Running Running 2 minutes ago x4y5z6a7b8c9 my-web.4 nginx:latest node-1 Running Running 1 minute ago x5y6z7a8b9c0 my-web.5 nginx:latest node-1 Running Running 1 minute ago
Key Concept

If you remember nothing else from this pattern, remember: scaling a service means running more copies to share the work and keep your app fast.

Common Mistakes
Trying to scale a service that was not created as a Docker service (e.g., a standalone container)
Docker service commands only work with services managed by Docker Swarm, not with individual containers.
Create the app as a Docker service first using 'docker service create' before scaling.
Not exposing the correct ports when creating the service, so the app is not reachable
Without port mapping, you cannot access the app from outside the container.
Use the '-p' flag to map host ports to container ports when creating the service.
Scaling to zero replicas accidentally
This stops all copies of the service, making the app unavailable.
Always scale to at least one replica to keep the service running.
Summary
Create a Docker service with a specific number of replicas to run multiple copies of your app.
Use 'docker service ls' to check running services and their replica counts.
Change the number of replicas anytime with 'docker service scale' to handle more or less traffic.
Verify each replica's status with 'docker service ps' to ensure all copies are running.