The software described in this documentation is either no longer supported or is in extended support.
Oracle recommends that you upgrade to a current supported release.

6.2.2 Adding or removing container instances

It is possible to add replica container instances to a service, or to remove them, at any point to scale the service to meet demand. This is achieved using the docker service scale command on any manager node:

# docker service scale SERVICE=REPLICAS

Substitute SERVICE with the name or ID of the service to be scaled and substitute REPLICAS with the number of replica container instances that you wish to run in the service. For example:

# docker service scale hello=12
hello scaled to 12
# docker service ps hello
ID                NAME      SERVICE  IMAGE             LAST STATE          DESIRED STATE  NODE
5u710ts...weir0v  hello.1   hello    nginxdemos/hello  Running 17 minutes  Running        ol1.example.com
b1nqbkp...65qj3f  hello.2   hello    nginxdemos/hello  Running 17 minutes  Running        ol3.example.com
00hmy0p...b4kgsp  hello.3   hello    nginxdemos/hello  Running 17 minutes  Running        ol4.example.com
8pv15jy...u75oz2  hello.4   hello    nginxdemos/hello  Running 3 seconds   Running        ol2.example.com
cqyffbs...3w0m54  hello.5   hello    nginxdemos/hello  Running 4 seconds   Running        ol1.example.com
4pggall...b0yek3  hello.6   hello    nginxdemos/hello  Running 4 seconds   Running        ol4.example.com
9kdsm5q...ybanef  hello.7   hello    nginxdemos/hello  Running 4 seconds   Running        ol3.example.com
44rebto...dygv6e  hello.8   hello    nginxdemos/hello  Running 4 seconds   Running        ol3.example.com
8hoiml0...42eouu  hello.9   hello    nginxdemos/hello  Running 4 seconds   Running        ol4.example.com
43ab93b...jyi194  hello.10  hello    nginxdemos/hello  Running 3 seconds   Running        ol4.example.com
a7kwo7y...60gxn5  hello.11  hello    nginxdemos/hello  Running 3 seconds   Running        ol1.example.com
cgg9snb...aersh4  hello.12  hello    nginxdemos/hello  Running 2 seconds   Running        ol2.example.com

In the example, the hello service is scaled to run 12 replica containers. When the tasks running in the hello service are listed, it is apparent that many new containers have been spawned as these have only been running for several seconds, while three original containers have already been running for significantly longer.

In these examples the nginxdemos/hello image is useful as it can be used to demonstrate the load-balancing functionality when the service is accessed on any particular node. The nginxdemos/hello image contains a web application that returns the hostname and IP address for the container where it is running. By simply pointing a web browser at any of the nodes in the swarm and repeatedly refreshing, it should become apparent that the hostname returned by the application changes using a round-robin algorithm. This can also be illustrated on the command line using curl, for example:

# curl --silent "http://ol1.example.com"|grep 'hostname'
<h2>My hostname is 2a6c91061981</h2>
# curl --silent "http://ol1.example.com"|grep 'hostname'
<h2>My hostname is e163d77affa0</h2>
# curl --silent "http://ol1.example.com"|grep 'hostname'
<h2>My hostname is f4dde4ea921f</h2>
# curl --silent "http://ol1.example.com"|grep 'hostname'
<h2>My hostname is 2a6c91061981</h2>

To remove container instances from the service, scale down to a number lower than the number of instances currently running. To reduce the number of instances in the example, the hello service is scaled down to two instances:

# docker service scale hello=2
hello scaled to 2