What's the Best Way to Update a Service in Docker Swarm Without Downtime?
Asked by
far-and-away
I'm currently using Docker Swarm on a single machine to perform no-downtime updates for my website. Here's my process:
1. I publish a new Docker image tagged as "latest" from my dev machine.
2. On the server, I run "docker pull myimage:latest" but notice that my running container's image changes from "latest" to the previous image's hash.
3. Then, I execute the command "docker service update --image myimage:latest myservicename" and the console shows:
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service myservicename converged
In Portainer, I see that my service has been updated to the latest version of the container, but Docker doesn't attempt to stop the old container or restart the new one. The old container remains running, and the latest image shows as "Unused." I expected Docker to start a new container and redirect traffic to it. What am I missing?
Answers
It looks like the issue stems from using the "latest" tag for your images. Docker Swarm doesn't recognize that there's an actual change in the service definition because both the old and new images are tagged as "latest." Ideally, you should use unique tags for your images, like version numbers or timestamps, which will help Docker realize that thereโs a new image to pull and deploy.
Reply to this answer
Another option to consider is using `docker stack deploy`, which will automatically fetch the correct image and restart the service if necessary, even when using the "latest" tag. It ensures that the deployment reflects the latest updates in your images.
Reply to this answer
If you want a quick workaround, you can use the `--force` option with your update command. This will force Docker to recreate the containers and, in doing so, switch them to the latest image.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions