在码头群堆栈中重新启动一个服务

有没有人知道有没有一种方法可以让 Docker 群重新启动一个服务,这个服务是一个堆栈的一部分,而不需要重新启动整个堆栈?

89422 次浏览

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.

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.

Scale to 0 and back up:

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

remove it:

docker stack rm stack_name

redeploy it:

docker stack deploy -c docker-compose.yml stack_name

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

To restart a single service (with rolling restart to avoid downtime in case the service has multiple replicas) in already configured, existing stack, you can do:

docker service update --force stack_service_name

I don't recommend running the same command again (in another shell) until this one completes (because otherwise the rolling restart isn't guaranteed; it might restart all replicas of that service).

The docker service update command also checks for newer version of the image:tag you are trying to use. If your registry requires auth, also pass --with-registry-auth argument, like so:

docker service update --force --with-registry-auth stack_service_name

If you don't pass this argument, the service will still be restarted, but the check won't be made and the service will still use the old container image without pulling the new one first. Which might be what you want.

In case you want to also switch to different image tag (or completely different image), you can do it from here too, but remember to also change the tag in your docker-stack.yml, or your next docker stack deploy will revert it back to the verison defined there:

docker service update --with-registry-auth --force --image nginx:edge stack_service_name