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.