Scale Service

Once you have deployed a service to a swarm, and then you want to scale up or scale down the number of containers in the service, you can use the docker service scale command.

Run the following command to scale the service -

docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>

For example:

Let's first create 2 services with 2 replicas and 3 replicas respectively -

root@master:~# docker service create -d --replicas 2 alpine ping 192.168.0.123
z9oapr0bkhnyos6de9qc6d2ym
root@master:~# docker service create -d --replicas 3 alpine ping 192.168.0.123
ie3oamnpb5ivxuwune58k7tn2
root@master:~# docker service ls
ID             NAME                  MODE         REPLICAS   IMAGE           PORTS
ie3oamnpb5iv   bold_chatelet         replicated   3/3        alpine:latest
z9oapr0bkhny   unruffled_blackburn   replicated   2/2        alpine:latest

To scale the service starting z9, use below command -

root@master:~# docker service scale z9=7
z9 scaled to 7
overall progress: 7 out of 7 tasks
1/7: running   [==================================================>]
2/7: running   [==================================================>]
3/7: running   [==================================================>]
4/7: running   [==================================================>]
5/7: running   [==================================================>]
6/7: running   [==================================================>]
7/7: running   [==================================================>]
verify: Service converged

To scale both the service at a time, use below command -

root@master:~# docker service scale z9=9 ie=5
z9 scaled to 9
ie scaled to 5
overall progress: 9 out of 9 tasks
1/9: running   [==================================================>]
2/9: running   [==================================================>]
3/9: running   [==================================================>]
4/9: running   [==================================================>]
5/9: running   [==================================================>]
6/9: running   [==================================================>]
7/9: running   [==================================================>]
8/9: running   [==================================================>]
9/9: running   [==================================================>]
verify: Service converged
overall progress: 5 out of 5 tasks
1/5: running   [==================================================>]
2/5: running   [==================================================>]
3/5: running   [==================================================>]
4/5: running   [==================================================>]
5/5: running   [==================================================>]
verify: Service converged

Now check the list of services to check the final number of replicas -

root@master:~# docker service ls
ID             NAME                  MODE         REPLICAS   IMAGE           PORTS
ie3oamnpb5iv   bold_chatelet         replicated   5/5        alpine:latest
z9oapr0bkhny   unruffled_blackburn   replicated   9/9        alpine:latest
root@master:~#