Restart one service in docker swarm stack

DockerDocker ComposeDocker Swarm

Docker Problem Overview


Does anyone know if there is a way to have docker swarm restart one service that is part of a stack without restarting the whole stack?

Docker Solutions


Solution 1 - Docker

Doing docker stack deploy again for me is the way to go to update services. As Francois' Answer, and also in my own experience, doing so updates only services that need to be updated.

But sometimes, it seems easier when testing stuff to only restart a single service. In my case, I had to clear the volume and update the service to start it like it was fresh. I'm not sure if there is downside to the method I will describe. I tested it on my development stack and it worked great for me.

Get the service id you want to tear down then use docker service update --force <id> to force the update of the service which effectively re-deploy it

$ docker stack services <stack_name>
ID                  NAME              ...
3xrdy2c7pfm3        stack-name_api    ...

$ docker service update --force 3xrdy2c7pfm3

The --force flag will force the service to update causing it to restart.

Solution 2 - Docker

Scale to 0 and back up:

docker service scale myservice=0
docker service scale myservice=10

Solution 3 - Docker

Looking at the docker stack documentation:

> Extended description > > Create and update a stack from a compose or a dab file on the swarm

From this blog article: docker stack works in a similar way as docker compose. It’s idempotent. If the stack is already deployed, docker stack deploy will restart only those services which has the digest or tag that is updated:

docker stack process

From my experience, when I deploy the same stack again with one service changing, only the updated service will be restarted.

BUT... there seems to be some limitations to changes that are taken into account (some report bugs with image tags), so give it a try and see if works as expected.

You can also use service update if you want to be sure that only targeted service if updated with your changes.

You can also refer to this similar SO QA.

Solution 4 - Docker

As per the example in the documentation for rolling updates:

> $ docker service update --image redis:3.0.7 redis

However, that only works if your image is already on the local machines. If not then you need to use --with-registry-auth to send registry authentication details to the swarm agents. See details in the docker service update documentation.

> $ docker service update --with-registry-auth --image redis:3.0.7 redis

Solution 5 - Docker

remove it:

docker stack rm stack_name

redeploy it:

docker stack deploy -c docker-compose.yml stack_name

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontweeks200View Question on Stackoverflow
Solution 1 - DockerMattView Answer on Stackoverflow
Solution 2 - DockerLuke WView Answer on Stackoverflow
Solution 3 - DockerFrançois MaturelView Answer on Stackoverflow
Solution 4 - DockerRobert WasmannView Answer on Stackoverflow
Solution 5 - DockerXiaorong LiaoView Answer on Stackoverflow