How to update docker stack without restarting all services
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Updating a Docker stack without restarting all services is a crucial skill for anyone managing production environments. This process ensures that your deployments remain seamless and your services continue to operate smoothly without downtime. Let's delve into the nuances of achieving this with Docker Swarm, using rolling updates, and understanding the related components.
Understanding Docker Swarm and Stack
Docker Swarm is Docker's native clustering and orchestration tool which allows multiple Docker engines to work together as a swarm cluster. It uses services and stacks to manage sets of containers.
- Service: Represents a deployed application running in the swarm. It's the core operational unit in Swarm.
- Stack: A collection of services that make up an application. The stack is defined in a Docker Compose YAML file and managed as a single unit.
Rolling Updates
Rolling updates are the primary mechanism for updating a service within a Docker stack without downtime. This feature allows you to update one task (container) at a time so that the rest of the services remain unaffected and running during the update process.
Key Components of Rolling Updates:
- Update Configuration: Control how a service update is executed. You can specify the number of tasks to update simultaneously, delay between updates, and failure action.
- Image Update: Replace running tasks with new tasks based on a newly built Docker image.
Example `docker-compose.yml` Configuration
Below is an excerpt of a stack file illustrating how rolling updates work:
- `parallelism: 2`: Updates at most two tasks at any given time.
- `delay: 10s`: Introduces a 10-second delay before updating the next set of tasks.
- `order: start-first`: Ensures the new task starts before the old task is stopped, reducing downtime.
- `restart_policy`: Configures the restart policy for failing services to enhance reliability during updates.
- source: my-config
- Use Health Checks: Incorporate health checks to ensure newly deployed tasks are healthy before terminating old tasks.
- Parallelism Limitations: Consider your infrastructure's capacity to prevent resource exhaustion during rolling updates.
- Rollback and Redeploy: Define a strategy for rollback using the `docker service rollback` command if an update fails.
Related reading
- How to update /etc/hosts file in Docker image during docker build
- How to update existing images with docker-compose?
- How to upgrade docker container after its image changed
- How to use --volume option with Docker Toolbox on Windows?
- How to update nginx-ingress controller so that latest ingress paths are used?
- How to upload and deploy zip file to AWS elastic beanstalk via CLI?
- How to update element priorities in a heap for Prim's Algorithm?
- How to update elements within a heap? priority queue

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.