# How to Scale Docker Swarm Horizontally in Production

Link: [https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/)

<div class="dark bg-primary-hero" id="bkmrk-auto-scalingdockerhi" style="text-align: justify;"><div class="mx-auto pt-4 pb-8 px-7 max-w-[1130px]"><div class="pt-10 md:pt-16 lg:pt-20"><div class="mt-4 flex justify-center text-base flex-wrap">[Auto Scaling](https://betterstack.com/tag/auto-scaling?utm_content&utm_medium=guides&utm_source=community&utm_term=horizontally-scaling-swarm)[Docker](https://betterstack.com/tag/docker?utm_content&utm_medium=guides&utm_source=community&utm_term=horizontally-scaling-swarm)[High Availability](https://betterstack.com/tag/high-availability?utm_content&utm_medium=guides&utm_source=community&utm_term=horizontally-scaling-swarm)</div><div class="mt-10 md:mt-16 mt-20 text-center"><div class="text-white">Cristovao Cordeiro</div><div class="mt-1 uppercase whitespace-nowrap text-xs font-medium text-neutral-300 tracking-[1.56px]">Updated on January 9, 2024</div></div></div></div></div>###### Contents

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-prerequisites-gettin" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="sticky hidden xl:block mr-12 shrink-0 w-[190px]" data-toc-target="toc"><div class="text-base text-secondary sticky scroll-smooth overflow-y-auto pretty-scrollbars max-h-screen top-[100px] pb-40"><div class="table-of-contents flat">- [Prerequisites](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#prerequisites)
- [Getting started](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#getting-started)
- [Step 1 — Checking the current distribution of tasks](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#step-1-checking-the-current-distribution-of-tasks)
- [Step 2 — Scaling out the service to 5 replicas](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#step-2-scaling-out-the-service-to-5-replicas)
- [Step 3 — Undoing a scaling operation](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#step-3-undoing-a-scaling-operation)
- [Step 4 — Scaling in the service to 3 replicas](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#step-4-scaling-in-the-service-to-3-replicas)
- [Step 5 — Defining constraints while scaling a service](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#step-5-defining-constraints-while-scaling-a-service)
- [Auto scaling Docker services (optional)](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#auto-scaling-docker-services-optional)
- [Conclusion and next steps](https://betterstack.com/community/guides/scaling-docker/horizontally-scaling-swarm/#conclusion-and-next-steps)

</div></div></div><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content">  
</div></div></div></div>With any software service deployment, there might come a time when you need to think about scaling your service due to meet changing demands. Such an action may be triggered by multiple factors including the following:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-your-service-is-unde" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content">- Your service is under heavy load which can ultimately impact its performance. Therefore, you'll want to add more instances of the service throughout the cluster, such that the incoming load can be re-distributed evenly.
- Your service does not have enough instances running throughout multiple availability zones of your cluster. This means it might be subject to a single point of failure which compromises its availability if one or more nodes in the cluster go down. Therefore, you'll want to add more instances to your service, distributed more equally throughout the cluster to increase its redundancy.
- Your service is experiencing a low demand from your users, which means you might be hosting and maintaining more instances of your service than what is needed to fulfill the demand. Therefore, you'll want to decrease the number of active service instances to reduce your maintenance efforts and compute costs.

</div></div></div></div>In this tutorial, you will learn how to horizontally scale in/out an existing Docker service, and you'll also experiment with scalability constraints and auto scaling solutions. For future reference, when we talk about *Scaling In* and *Scaling Out*, we mean the following:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-scaling-in%3A-removing" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content">- **Scaling In**: removing instances from an existing service.
- **Scaling Out**: adding more instances in parallel to spread out the service load.

</div></div></div></div><div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-centralize-%26-visuali" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content">Centralize &amp; visualize your logs. Query everything with SQL. <div>[<button class="block px-3 py-2 bg-neutral-700 rounded font-medium text-white leading-none whitespace-nowrap">Explore more</button>](https://betterstack.com/telemetry?utm_content=upsell&utm_medium=guides&utm_source=community&utm_term=horizontally-scaling-swarm)</div></div></div></div></div>## Prerequisites

Before proceeding with this article, ensure that you've read and performed all the steps from the previous tutorial on [Setting up Docker Swarm High Availability in Production](https://betterstack.com/community/guides/scaling-docker/ha-docker-swarm/). This means you should have four replicas of an NGINX service running on a 5-node cluster with three managers and two workers where all managers have been drained:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-2" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker node ls

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID                            HOSTNAME    STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
9r83zto8qpqiazt6slxfkjypq     manager-1   Ready     Drain          Reachable        20.10.18
uspt9qwqnzqwl78gbxc7omja7 *   manager-2   Ready     Drain          Leader           20.10.18
txrdxwuwjpg5jjfer3bcmtc5r     manager-3   Ready     Drain          Reachable        20.10.18
kaq8r9gec4t58yc9oh3dc0r2d     worker-1    Ready     Active                          20.10.18
vk1224zd81xcihgm1iis2703z     worker-2    Ready     Active                          20.10.18

```

We are currently working on the `manager-2` node, which is the leader and is not schedulable (as it was drained in the previous tutorial). Go ahead and execute the following command to see the active services on the cluster:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-3" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service ls

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID             NAME          MODE         REPLICAS   IMAGE          PORTS
euqfhji2qpco   nginx_nginx   replicated   4/4        nginx:latest   *:8088->80/tcp

```

Currently, there is only one service with four replicas in the entire cluster. For now, ensure that you are connected to the Leader node (`manager-2` in this example). If you do not have access to a Docker cluster as required to follow this tutorial, we recommend you use the [online Docker playground<span class="whitespace-nowrap"><svg class="relative -top-[1px] ml-1  w-4 h-4 inline"></svg></span>](https://labs.play-with-docker.com/).Getting started

In this tutorial, we'll mainly use the `docker service` management command to perform all the service inspections and scalability actions. Knowledge of this command is crucial when scaling your services since it allows you to perform the following actions through various subcommands:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-inspect%C2%A0all-the-info" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content">- `inspect` all the information about your service, including its previous and current states.
- `list` (or `ls`) all the services running in your cluster.
- list (`ps`) all the tasks within a service. Each task will correspond to a replica of your service, so this information is critical when scaling in/out.
- `update` a service's specification (i.e., its placement policies, number of replicas, etc.).
- `rollback` a service to its previous specification.
- `scale` one or more services concurrently, in, or out, which is pretty much the same as running a service `update` to change the number of service replicas.

</div></div></div></div>To see these subcommands in action, let's take on a 5-step scenario where we'll analyze our current cluster and services, and then scale in and out a service while also applying scalability constraints.

## Step 1 — Checking the current distribution of tasks

Docker services are composed of tasks which are scheduled into the cluster nodes based on the provided placement [preferences<span class="whitespace-nowrap"><svg class="relative -top-[1px] ml-1  w-4 h-4 inline"></svg></span>](https://docs.docker.com/engine/swarm/services/#placement-preferences) and [constraints<span class="whitespace-nowrap"><svg class="relative -top-[1px] ml-1  w-4 h-4 inline"></svg></span>](https://docs.docker.com/engine/swarm/services/#placement-constraints). In the previous tutorial, our NGINX service was deployed via a Docker Stack, which was declared with the `nginx.yaml` compose file. Let's confirm this through the command below:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-4" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker stack ls

```

You should observe just one stack which was deployed from our `nginx.yaml` compose file:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-2" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
NAME      SERVICES   ORCHESTRATOR
nginx     1          Swarm

```

Next, list all the services in the `nginx` stack using the command below:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-5" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker stack services nginx

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-3" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID             NAME          MODE         REPLICAS   IMAGE          PORTS
euqfhji2qpco   nginx_nginx   replicated   4/4        nginx:latest   *:8088->80/tcp

```

The above command produces all the services defined in the `nginx.yaml` file and deployed as part of the `nginx` stack above. In this case, it's the same as running `docker service ls` since there are no other stacks or services in the cluster.

It is easy to read through our `nginx.yaml` file to verify that we haven't defined any [placement policies<span class="whitespace-nowrap"><svg class="relative -top-[1px] ml-1  w-4 h-4 inline"></svg></span>](https://docs.docker.com/compose/compose-file/compose-file-v3/#placement) for this service. Take a look at the `deploy` section of the file:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-nginx.yaml" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small">nginx.yaml</span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```yaml
version: '3.7'

networks:
  nginx:
    external: false

services:

  # --- NGINX ---
  nginx:
      image: nginx:latest
      ports:
        - '8088:80'

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-deploy%3A" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight top"><span class="token key atrule">deploy</span><span class="token punctuation">:</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-replicas%3A-4" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">replicas</span><span class="token punctuation">:</span> <span class="token number">4</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-update_config%3A" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">update\_config</span><span class="token punctuation">:</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-parallelism%3A-2" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">parallelism</span><span class="token punctuation">:</span> <span class="token number">2</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-order%3A-start-first" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">order</span><span class="token punctuation">:</span> start<span class="token punctuation">-</span>first</div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-failure_action%3A-roll" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">failure\_action</span><span class="token punctuation">:</span> rollback</div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-delay%3A-10s" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">delay</span><span class="token punctuation">:</span> 10s</div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-rollback_config%3A" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">rollback\_config</span><span class="token punctuation">:</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-parallelism%3A-0" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">parallelism</span><span class="token punctuation">:</span> <span class="token number">0</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-order%3A-stop-first" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">order</span><span class="token punctuation">:</span> stop<span class="token punctuation">-</span>first</div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-restart_policy%3A" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">restart\_policy</span><span class="token punctuation">:</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-condition%3A-any" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">condition</span><span class="token punctuation">:</span> any</div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-delay%3A-5s" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">delay</span><span class="token punctuation">:</span> 5s</div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-max_attempts%3A-3" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight"><span class="token key atrule">max\_attempts</span><span class="token punctuation">:</span> <span class="token number">3</span></div></div></div></div></div></div></div>```yaml


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-window%3A-120s" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded yaml" data-clipboard-target="content" data-controller="code"><div class="code-highlight bottom"><span class="token key atrule">window</span><span class="token punctuation">:</span> 120s</div></div></div></div></div></div></div>```yaml

      healthcheck:
        test: ["CMD", "service", "nginx", "status"]
      networks:
        - nginx

```

We can also confirm this by using `docker service inspect` to get detailed information about the NGINX service. Because this inspection can return an exhaustive amount of information, we can format the output using a Go template. In our case, we know that the service's placement policies are detailed under the service's `Spec->TaskTemplate->Placement` field, so we can format our inspection output by running the following:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-6" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service inspect nginx_nginx --format '{{json .Spec.TaskTemplate.Placement}}'

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-4" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
{"Platforms":[{"Architecture":"amd64","OS":"linux"},{"OS":"linux"},{"OS":"linux"},{"Architecture":"arm64","OS":"linux"},{"Architecture":"386","OS":"linux"},{"Architecture":"mips64le","OS":"linux"},{"Architecture":"ppc64le","OS":"linux"},{"Architecture":"s390x","OS":"linux"}]}

```

As you can see, the service does not have any placement policies. We should expect that Docker is smart enough to distribute the four replicas of this service throughout all schedulable nodes of the cluster as evenly as possible. We have four schedulable nodes in our cluster, so in theory, we should have two NGINX replicas per worker node (since all three manager nodes are drained). Let's confirm this:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-7" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service ps nginx_nginx # list the tasks of the nginx_nginx service

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-5" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID             NAME                IMAGE          NODE        DESIRED STATE   CURRENT STATE           ERROR     PORTS
ikjo7su8ooo6   nginx_nginx.1       nginx:latest   worker-2    Running         Running 27 hours ago
inel921owq9b    \_ nginx_nginx.1   nginx:latest   manager-2   Shutdown        Shutdown 28 hours ago
y0y5mvj5n44r   nginx_nginx.2       nginx:latest   worker-1    Running         Running 27 hours ago
uk18jo6wwlq6    \_ nginx_nginx.2   nginx:latest   manager-3   Shutdown        Shutdown 28 hours ago
ijbalagf7isy   nginx_nginx.3       nginx:latest   worker-1    Running         Running 27 hours ago
drgu007baw8z   nginx_nginx.4       nginx:latest   worker-2    Running         Running 27 hours ago

```

As you can see, both worker nodes `worker-1` and `worker-2` are running two replicas of the NGINX service each. You can also see that the replicas that were running on `manager-2` and `manager-3` were shut down and reassigned to `worker-2` and `worker-1` respectively when both manager nodes were drained in step 8 of the [previous tutorial](https://betterstack.com/community/guides/scaling-docker/ha-docker-swarm/).

## Step 2 — Scaling out the service to 5 replicas

Let's begin this section by assuming our NGINX service is under a high load. We'll need to add another NGINX instance (aka replica) to cope with the increased demand. We can use the `docker service scale` command for this purpose. We only need to state the service name and the number of desired replicas.

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-8" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service scale nginx_nginx=5

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-6" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
nginx_nginx scaled to 5
overall progress: 4 out of 5 tasks
1/5: running   [==================================================>]
2/5: starting  [============================================>      ]
3/5: running   [==================================================>]
4/5: running   [==================================================>]
5/5: running   [==================================================>]

```

Once the service is stable, we'll find a new replica in our service:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-9" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service ps nginx_nginx

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-7" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID             NAME                IMAGE          NODE        DESIRED STATE   CURRENT STATE            ERROR     PORTS
ikjo7su8ooo6   nginx_nginx.1       nginx:latest   worker-2    Running         Running 28 hours ago
inel921owq9b    \_ nginx_nginx.1   nginx:latest   manager-2   Shutdown        Shutdown 29 hours ago
y0y5mvj5n44r   nginx_nginx.2       nginx:latest   worker-1    Running         Running 28 hours ago
uk18jo6wwlq6    \_ nginx_nginx.2   nginx:latest   manager-3   Shutdown        Shutdown 29 hours ago
ijbalagf7isy   nginx_nginx.3       nginx:latest   worker-1    Running         Running 28 hours ago
drgu007baw8z   nginx_nginx.4       nginx:latest   worker-2    Running         Running 28 hours ago

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-2cd89o4cybmn-nginx_n" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight top bottom">2cd89o4cybmn nginx_nginx.5 nginx:latest worker-2 Running Running 11 seconds ago</div></div></div></div></div></div></div>```text


```

This new `nginx_nginx.5` task is now running alongside `nginx_nginx.1` and `nginx_nginx.4` in `worker-2`.

We could've also added a new NGINX replica by running:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-10" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service update nginx_nginx --replicas 5

```

The outcome would've been the same as:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-11" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service scale nginx_nginx=5

```

Nonetheless, we recommend always using the `docker service scale` command as it can scale multiple services simultaneously. For example:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-12" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service scale my_service=3 my_other_service=4 ...

```

On the other hand, `docker service update` can only update one service at a time.

## Step 3 — Undoing a scaling operation

In this step, we will assume that we scaled our service by accident in the previous section and we'd like to revert our changes. As mentioned earlier, the `docker service scale` command is pretty much the same as a `docker service update` that only targets the number of service replicas. What happens under the hood is that the service is taking in a new specification where the number of replicas differs from the current one.

So how can we return our service to its previous state? Do we need to remember its entire previous specification by heart? Gladly we don't! Docker is smart enough to keep the service's previous specification within its definition. By running `docker service inspect`, we can examine a service's current and previous states:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-13" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service inspect nginx_nginx

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-8" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
[
    {
        "ID": "euqfhji2qpco",
        . . .

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22spec%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight top">"Spec": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22name%22%3A-%22nginx_nginx" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Name": "nginx_nginx",</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-." style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22tasktemplate%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"TaskTemplate": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22containerspec%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"ContainerSpec": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22image%22%3A-%22nginx%3Alate" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Image": "nginx:latest@sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767",</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22labels%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Labels": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22com.docker.stack.na" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"com.docker.stack.namespace": "nginx"</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-2" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-2" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22mode%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Mode": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22replicated%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Replicated": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22replicas%22%3A-5" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Replicas": 5</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">}</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-3" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-3" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22rollbackconfig%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"RollbackConfig": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22parallelism%22%3A-0%2C" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Parallelism": 0,</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22failureaction%22%3A-%22pa" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"FailureAction": "pause",</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22monitor%22%3A-500000000" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Monitor": 5000000000,</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22maxfailureratio%22%3A-0" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"MaxFailureRatio": 0,</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22order%22%3A-%22stop-first" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Order": "stop-first"</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-4" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-4" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-5" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22previousspec%22%3A-%7B" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"PreviousSpec": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22name%22%3A-%22nginx_nginx-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Name": "nginx_nginx",</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-5" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22tasktemplate%22%3A-%7B-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"TaskTemplate": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22containerspec%22%3A-%7B-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"ContainerSpec": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22image%22%3A-%22nginx%3Alate-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Image": "nginx:latest@sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767",</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22labels%22%3A-%7B-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Labels": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22com.docker.stack.na-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"com.docker.stack.namespace": "nginx"</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-6" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-6" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-7" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-7" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-8" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22mode%22%3A-%7B-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Mode": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22replicated%22%3A-%7B-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Replicated": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22replicas%22%3A-4" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Replicas": 4</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">}</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-9" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-8" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22rollbackconfig%22%3A-%7B-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"RollbackConfig": {</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22parallelism%22%3A-0%2C-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Parallelism": 0,</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22failureaction%22%3A-%22pa-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"FailureAction": "pause",</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22maxfailureratio%22%3A-0-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"MaxFailureRatio": 0,</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%22order%22%3A-%22stop-first-1" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">"Order": "stop-first"</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-10" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">},</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-.-.-.-9" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight">. . .</div></div></div></div></div></div></div>```text


```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%7D%2C-11" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight bottom">},</div></div></div></div></div></div></div>```text

        . . .
    }
]

```

The above command results in a large output (thus truncated), but we can clearly see two top-level keys, `Spec` and `PreviousSpec`, which look identical at first, but if you look closely, `PreviousSpec` defines a `Replicated` service with `4` replicas, while `Spec` sets this number to `5` (as expected, given the scale out we've performed in the previous step).

Since we have this `PreviousSpec` information, we can determine what our service will look like once we revert to the previous state. But the question now is: how do we undo the last scaling operation?

Once again, Docker comes to the rescue! The `docker service` management command has a `rollback` subcommand for these situations. There's not much to know about it, you simply run `docker service rollback` on the desired service, and Docker will automatically update it to its `PreviousSpec` configuration. Go ahead and try it out as shown below:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-14" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service rollback nginx_nginx

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-9" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
nginx_nginx
rollback: manually requested rollback
overall progress: rolling back update: 4 out of 4 tasks
1/4: running   [>                                                  ]
2/4: running   [>                                                  ]
3/4: running   [>                                                  ]
4/4: running   [>                                                  ]
verify: Service converged
rollback: rollback completed

```

If the rollback is successful, we should now see our initial four replicas instead of the five we scaled out to in the previous step:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-15" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service ps nginx_nginx

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-10" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID             NAME                IMAGE          NODE        DESIRED STATE   CURRENT STATE           ERROR     PORTS
ikjo7su8ooo6   nginx_nginx.1       nginx:latest   worker-2    Running         Running 29 hours ago
inel921owq9b    \_ nginx_nginx.1   nginx:latest   manager-2   Shutdown        Shutdown 30 hours ago
y0y5mvj5n44r   nginx_nginx.2       nginx:latest   worker-1    Running         Running 29 hours ago
uk18jo6wwlq6    \_ nginx_nginx.2   nginx:latest   manager-3   Shutdown        Shutdown 30 hours ago
ijbalagf7isy   nginx_nginx.3       nginx:latest   worker-1    Running         Running 29 hours ago
drgu007baw8z   nginx_nginx.4       nginx:latest   worker-2    Running         Running 29 hours ago

```

And there we go, our service is back to where it was in Step 1, running with four replicas.

## Step 4 — Scaling in the service to 3 replicas

Let's now assume we've been able to absorb the high service loads with our previous scale out and are now experiencing low demand for our service. To free up some space and potentially reduce our compute costs, let's shrink our service from four to three replicas:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-16" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service scale nginx_nginx=3

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-11" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
nginx_nginx scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged

```

The command is pretty much the same as in step 2, except for the desired number of replicas. Docker will take care of shutting down one of the replicas, leaving us with the desired number, evenly distributed throughout the cluster once again:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-17" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service ps nginx_nginx

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-12" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID             NAME                IMAGE          NODE        DESIRED STATE   CURRENT STATE           ERROR     PORTS
ikjo7su8ooo6   nginx_nginx.1       nginx:latest   worker-2    Running         Running 29 hours ago
inel921owq9b    \_ nginx_nginx.1   nginx:latest   manager-2   Shutdown        Shutdown 30 hours ago
y0y5mvj5n44r   nginx_nginx.2       nginx:latest   worker-1    Running         Running 29 hours ago
uk18jo6wwlq6    \_ nginx_nginx.2   nginx:latest   manager-3   Shutdown        Shutdown 30 hours ago
ijbalagf7isy   nginx_nginx.3       nginx:latest   worker-1    Running         Running 29 hours ago

```

Notice that other replicas weren't disturbed by this action — they are still running, uninterruptedly, for 29 hours.

## Step 5 — Defining constraints while scaling a service

Finally, sometimes you might want to ensure that a certain condition is met, even after your service is already running. For example, let's assume we now need to scale out to five NGINX replicas again, but we discovered that our worker nodes are not capable of hosting more than two NGINX replicas concurrently without a performance degradation.

In such scenario, we can no longer use `docker service scale` because it does not allow us to specify other configurations besides the number of replicas. This is where the `docker service update` command comes in, as it offers a wide range of options for configuring the new state of our service.

We need a new number of replicas, plus one condition (no more than two replicas per node), so we can use the following options:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-18" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service update --help

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-13" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
. . .
--constraint-add list                Add or update a placement constraint
. . .
--replicas uint                      Number of tasks
--replicas-max-per-node uint         Maximum number of tasks per node (default 0 = unlimited)
. . .

```

Go ahead and run the following service update command to scale out to five replicas once more:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-19" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service update nginx_nginx --replicas 5 --replicas-max-per-node 2

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-14" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
nginx_nginx
overall progress: 2 out of 5 tasks
1/5: starting  [============================================>      ]
2/5: running   [==================================================>]
3/5: starting  [============================================>      ]
4/5: running   [==================================================>]

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-5%2F5%3A-no-suitable-nod" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight top bottom">5/5: no suitable node (max replicas per node limit exceed; scheduling constrain…</div></div></div></div></div></div></div>```text


```

You'll observe that Docker will rightfully complain about your decision, because you've restricted each worker to only two replicas which means the fifth one will have no where to go. Since the above command was executed in the foreground, Docker will keep indefinitely, trying to fix this situation. You'll see something like this:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-15" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
nginx_nginx
overall progress: 4 out of 5 tasks
1/5: running   [==================================================>]
2/5: running   [==================================================>]
3/5: running   [==================================================>]
4/5: running   [==================================================>]
5/5: no suitable node (max replicas per node limit exceed; scheduling constrain…

```

We can `Ctrl+c` to interrupt the process (the service update will continue in the background), and then verify that this new task is pending, waiting for a suitable node to show up:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-20" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
docker service ps nginx_nginx --no-trunc # use --no-trunc to see the whole output

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0output-16" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"><svg alt="" class="m-0 -mt-1 inline fill-neutral-200" height="20" width="20"></svg> Output</span></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
ID                          NAME                IMAGE                                                                                  NODE        DESIRED STATE   CURRENT STATE           ERROR                                                                                          PORTS
ikjo7su8ooo6b7wmh4r9uj1lq   nginx_nginx.1       nginx:latest@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1   worker-2    Running         Running 3 days ago
inel921owq9bjq4zotkz590dz    \_ nginx_nginx.1   nginx:latest@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1   manager-2   Shutdown        Shutdown 3 days ago
y0y5mvj5n44r23b5y548hrvsm   nginx_nginx.2       nginx:latest@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1   worker-1    Running         Running 3 days ago
uk18jo6wwlq6qfrv2lr6zk1xz    \_ nginx_nginx.2   nginx:latest@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1   manager-3   Shutdown        Shutdown 3 days ago
ijbalagf7isyu1drhm0hu8z95   nginx_nginx.3       nginx:latest@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1   worker-1    Running         Running 3 days ago
lqa493b94iu8lpmc8yqtu4jd1   nginx_nginx.4       nginx:latest@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1   worker-2    Running         Running 4 minutes ago

```

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-txc8rjd3xpojvl7ckz8z" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"><div class="code-highlight top bottom">txc8rjd3xpojvl7ckz8zgohx9 nginx_nginx.5 nginx:latest@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1 Running Pending 5 minutes ago "no suitable node (3 nodes not available for new tasks; max replicas per node limit exceed)"</div></div></div></div></div></div></div>```text


```

To move the highlighted `Pending` task above to `Running`, you can take either of the following actions:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-redefine-the-schedul" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content">1. Redefine the scheduling constraints by allowing the cluster to run more than two replicas per node (as defined in the `docker service update` command above).
2. Horizontally scaling the cluster by provisioning additional worker nodes (this will be covered in the next tutorial).

</div></div></div></div>## Auto scaling Docker services (optional)

When talking about scaling container services, "auto scaling" often comes up. Now, the reason why this is a bonus (and optional) step, is because **Docker doesn't really offer any auto scaling mechanisms** (unlike [Kubernetes<span class="whitespace-nowrap"><svg class="relative -top-[1px] ml-1  w-4 h-4 inline"></svg></span>](https://kubernetes.io/)).

Nonetheless, you might be wondering how to introduce some level of automation to help scale in and out Docker services. Let's face it, there is no right answer for this, nor there is any "one size fits all" solution that can help you. Because we cannot auto scale directly with Docker, we must resort to workarounds that might involve third-party tools, custom scripts, etc.

There are some tools out there (such as [Orbiter<span class="whitespace-nowrap"><svg class="relative -top-[1px] ml-1  w-4 h-4 inline"></svg></span>](https://github.com/gianarb/orbiter)) that are designed specifically to fill this gap in Docker. But depending on the use case and auto scaling rules, you could build a custom Docker auto scaler with just a few lines of Bash and a [cronjob](https://betterstack.com/community/guides/linux/cron-jobs-getting-started/)!

Our NGINX service from above currently has five replicas. Regardless of their state and placement policies, let's say we simply want to **add** and **delete** service replicas every time a worker joins or leaves the Docker Swarm cluster, such that we always have two replicas per worker.

Start by building a script to monitor the cluster size and scale the service when necessary. As a superuser, create a new file called "/opt/autoscaler.sh" and open it with your favorite text editor:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-21" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
sudo nano /opt/autoscaler.sh

```

Paste the following text into the file:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%2Fopt%2Fautoscaler.sh" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small">/opt/autoscaler.sh</span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
#!/bin/sh

service_name=nginx_nginx

# 1
## let's see how many workers we have, by listing and counting all
## node IDs corresponding to the workers in the cluster
num_workers=$(docker node ls --filter role=worker -q | wc -l)

# 2
## then, let's infer the current number of replicas our service has
num_replicas=$(docker service inspect $service_name --format '{{json .Spec.Mode.Replicated.Replicas}}')

# 3
## let's then assess how many replicas we should have (2 per node)
let "target_replicas = 2*$num_workers"

# 4
## and finally, let's compare the target replicas
## with the ones we currently have, and if they are different
## we scale the service to the number of target_replicas
if [[ $num_replicas -ne $target_replicas ]]
then
  echo "Autoscaling Docker service ${service_name} to ${target_replicas} replicas"
  docker service scale $service_name=$target_replicas -d
fi

## else, there's nothing to do since we already have
## the desired amount of replicas

```

The above file defines a simplistic auto scaling rule, but applicable nonetheless. Next, you need to run this script periodically via a cronjob (say once every hour). To do that, execute the following command to open the crontab config file in your text editor:

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-22" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded command" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```command
crontab -e

```

Add the following line at the end of the file, then save and close it.

<div class="container mx-auto py-8 lg:py-16 max-w-[1130px]" id="bkmrk-%C2%A0-23" style="text-align: justify;"><div class="lg:flex" data-controller="toc"><div class="grow xl:flex"><div class="grow shrink-0 prose max-w-[616px]" data-controller="body" data-toc-target="content"><div class="relative mb-5 bg-neutral-30 dark:bg-[#171824] border border-neutral-50 dark:border-neutral-800 rounded-md text-[15px] shadow-2xs" data-controller="clipboard"><div class="px-4 py-[6px] bg-white dark:bg-[#0B0C14] flex items-center rounded-t-md border-b border-neutral-50 dark:border-neutral-800"><div class="text-[15px] font-medium inline align-middle grow"><span class="text-app-small"> </span></div><div class="copy-button hidden sm:flex sm:items-center" data-clipboard-target="confirmation"><button class="text-sm p-1" data-action="click->clipboard#copy"><svg alt="copy code to clipboard" class="m-0 text-neutral-200" height="18" width="18"></svg></button></div></div><div class="code-wrapper overflow-x-auto pretty-scrollbars p-2 rounded text" data-clipboard-target="content" data-controller="code"></div></div></div></div></div></div>```text
0 * * * * sh /opt/autoscaler.sh

```

With this setup in place, the `/opt/autoscaler.sh` script will be executed once per hour by the system's `cron` daemon. While this autoscaling infrastructure isn't production quality, it is enough to bootstrap your understanding of how to work around the autoscaling limitations in Docker.

## Conclusion and next steps

In this tutorial, you learned about scaling a Docker Swarm service in and out, and how to undo a scaling operation. You also learned how to inspect a service so that you can make educated decisions about your scaling constraints. Finally, we touched base on auto scaling, giving you some tips on how to automate the scaling operations for your service.

In a future tutorial, we'll learn about horizontally scaling the cluster itself, such that a suitable node shows up to serve the pending task from step five.

*This article was contributed by guest author [Cristovao Cordeiro<span class="whitespace-nowrap"><svg class="relative -top-[1px] ml-1  w-4 h-4 inline"></svg></span>](https://twitter.com/@cristovaojdc), a Docker certified Engineering Manager at Canonical. He's an expert in Containers and ex-CERN engineer with 9+ years of experience in Cloud and Edge computing.*