WittCode💻

Ensuring Minimal Downtime with Docker Compose

By

Learn how to minimize the amount of downtime when updating Docker containers with Docker Compose. This includes pulling new images, rolling updates, and removing orphan containers.

Table of Contents 📖

Minimal Downtime

When updating your Docker containers, you want to ensure that there is minimal downtime during the transition from the old containers stopping and the new containers starting. Docker Compose makes this process extremely simple. The order is essentially:

  • Pull the updated images.
  • Update all services or a specific service.
  • Remove unused containers.

Pulling the New Images

The first step, after updating your docker-compose.yaml file, is to pull the new images for your containers. This is done with the following command:

docker compose pull

Pulling the images first ensures that the latest images are available for each service before restarting the service with new containers.

Rolling Update

Next, if you want to update or start a specific service, I would recommend the following command:

docker compose up -d --no-deps <service_name>

ERROR: If multiple services are dependent, recreate them sequentially.

The --no-deps flag prevents Docker compose from recreating or starting dependent services when you update or start a specific service. By default, when you use docker compose up, Docker Compose checks if the specified service has dependencies defined in the depends_on section of the docker-compose.yaml file. If it does, Docker Compose ensures that those dependent services are started first or recreated as necessary. On the other hand, if you want to update everything, use the following command:

docker compose up -d --remove-orphans

Omitting the service name will recreate all services. The --remove-orphans flag removes any containers that were created by a previous version of the docker-compose.yaml file but are no longer defined in the updated file.

WARNING: If you update your docker-compose.yaml file and remove or rename a service, the containers associated with the removed/renamed service will not be stopped or removed automatically. These are called orphan containers.

Ensuring Minimal Downtime with Docker Compose