How to use docker service scale command to scale services

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage the number of running instances (replicas) of your services within a Docker Swarm. We will begin by creating a replicated service, establishing a baseline with multiple running copies.

Following the creation, you will explore how to dynamically adjust the service's scale. This includes scaling a single service up to handle increased demand, scaling it down, even to zero replicas, and finally, scaling multiple services simultaneously to manage complex deployments. Through hands-on commands, you will gain practical experience in using the docker service scale command to achieve high availability and resource optimization for your containerized applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/ls -.-> lab-555231{{"How to use docker service scale command to scale services"}} docker/ps -.-> lab-555231{{"How to use docker service scale command to scale services"}} docker/rm -.-> lab-555231{{"How to use docker service scale command to scale services"}} docker/inspect -.-> lab-555231{{"How to use docker service scale command to scale services"}} docker/pull -.-> lab-555231{{"How to use docker service scale command to scale services"}} end

Create a replicated service

In this step, we will learn how to create a replicated service using Docker. A replicated service means that multiple identical copies (replicas) of your service are running simultaneously. This provides high availability and allows you to handle more traffic.

First, let's pull the necessary Docker image. We will use the alpine image for this example.

docker pull alpine

You should see output indicating that the image is being pulled and downloaded.

Now, let's create a replicated service named my-alpine-service with 3 replicas. We will use the alpine image and the command ping localhost. This command will run inside each container and continuously ping the localhost.

docker service create --name my-alpine-service --replicas 3 alpine ping localhost

After running this command, Docker Swarm will create the service and start the specified number of replicas. You should see output confirming the creation of the service.

To verify that the service has been created and the replicas are running, you can list the services.

docker service ls

You should see my-alpine-service listed with 3 replicas.

You can also inspect the service to get more details.

docker service inspect my-alpine-service

This command will output a large JSON object containing detailed information about the service, including the number of replicas.

Finally, let's check the tasks associated with the service to see the individual containers running.

docker service ps my-alpine-service

This command will show the status of each replica (task) of the service. You should see 3 tasks listed, each in a running state.

Scale a single service up

In this step, we will learn how to scale a single service up. Scaling up means increasing the number of replicas for a service. This is useful when you need to handle increased load or traffic.

In the previous step, we created a service named my-alpine-service with 3 replicas. Now, let's scale this service up to 5 replicas. We can use the docker service scale command for this.

docker service scale my-alpine-service=5

After running this command, Docker Swarm will add 2 new replicas to the service. You should see output indicating that the service is being updated.

To verify that the service has been scaled up, you can list the services again.

docker service ls

You should now see my-alpine-service listed with 5 replicas.

You can also check the tasks associated with the service to see the newly created containers.

docker service ps my-alpine-service

This command will show the status of all 5 replicas (tasks) of the service. You should see 5 tasks listed, with the new ones transitioning to a running state.

Scale a single service down to zero

In this step, we will learn how to scale a single service down to zero replicas. Scaling down to zero effectively stops all instances of the service. This can be useful for temporarily shutting down a service without removing its configuration.

In the previous step, we scaled the my-alpine-service to 5 replicas. Now, let's scale this service down to 0 replicas. We will use the docker service scale command again.

docker service scale my-alpine-service=0

After running this command, Docker Swarm will stop and remove all the running replicas of the service. You should see output indicating that the service is being updated.

To verify that the service has been scaled down to zero, you can list the services.

docker service ls

You should now see my-alpine-service listed with 0 replicas.

You can also check the tasks associated with the service.

docker service ps my-alpine-service

This command will show the status of the tasks. Since we scaled down to zero, there should be no running tasks listed. You might see tasks in a "Shutdown" state, indicating they were stopped.

Scale multiple services

In this step, we will learn how to scale multiple services simultaneously. This is useful when you have several services that need to be scaled together, for example, different components of an application.

First, let's create another service. We will create a service named my-nginx-service using the nginx image with 1 replica. We need to pull the nginx image first.

docker pull nginx

You should see output indicating that the nginx image is being pulled.

Now, create the my-nginx-service.

docker service create --name my-nginx-service --replicas 1 nginx

You should see output confirming the creation of the my-nginx-service.

Now we have two services: my-alpine-service (currently scaled to 0) and my-nginx-service (scaled to 1). Let's scale both services at once. We will scale my-alpine-service to 2 replicas and my-nginx-service to 3 replicas.

docker service scale my-alpine-service=2 my-nginx-service=3

After running this command, Docker Swarm will update both services according to the specified replica counts. You should see output indicating that both services are being updated.

To verify that both services have been scaled, you can list the services.

docker service ls

You should now see my-alpine-service listed with 2 replicas and my-nginx-service listed with 3 replicas.

You can also check the tasks for each service individually to see the running containers.

docker service ps my-alpine-service

This will show the 2 running tasks for my-alpine-service.

docker service ps my-nginx-service

This will show the 3 running tasks for my-nginx-service.

Finally, let's clean up the services we created. We can remove both services using the docker service rm command.

docker service rm my-alpine-service my-nginx-service

You should see output confirming the removal of both services.

To verify that the services are removed, list the services again.

docker service ls

There should be no services listed.

Summary

In this lab, we learned how to manage the scaling of Docker services. We began by creating a replicated service with a specified number of replicas, demonstrating the initial deployment of a fault-tolerant application. We then explored how to dynamically adjust the number of running instances for a single service, first by scaling it up to handle increased demand and subsequently scaling it down, including reducing the replicas to zero to effectively stop the service.

Finally, we learned how to scale multiple services simultaneously, showcasing the ability to manage the resource allocation for several applications within a Docker Swarm environment with a single command. These steps provided practical experience in using the docker service scale command to efficiently manage the lifecycle and resource utilization of services.