docker
docker-compose
container orchestration
fault tolerance
DevOps

How to stop all containers when one container stops with docker-compose?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Docker Compose is a tool used for defining and running multi-container Docker applications. Many developers and system administrators leverage Docker Compose to simplify and manage containerized applications. Often, there's a need to manage the lifecycle of containers collectively, particularly in scenarios where stopping one container necessitates stopping all other containers related to the application. This article delves into how you can achieve such a setup using Docker Compose.

The Challenge

The challenge arises when you want all containers in a Docker Compose setup to stop if a specific container stops. This situation is commonly required in tightly coupled applications where the functionality of one service is crucial to the functioning of others.

Solution: The `depends_on` Property and Condition

Traditionally, Docker Compose uses the `depends_on` property to specify the order of starting containers. However, it does not inherently support stopping containers in the reverse order, nor does it respond automatically to the stopping of a container.

To achieve the intended behavior, you can implement a strategy using a shell script or utilize Docker Compose's `--abort-on-container-exit` flag. We'll explore the latter as it's straightforward and directly linked to the lifecycle of containers.

Using `--abort-on-container-exit` Flag

The `--abort-on-container-exit` flag is a command-line option for `docker-compose up` that stops all running containers if any container stops, regardless of its exit code. This flag ensures that all containers are stopped when one container completes its cycle.

Implementing with Docker Compose

Consider the following example of a `docker-compose.yml` file for a simple application:

  • db
  • Initialization: All services defined in the `docker-compose.yml` file are started concurrently.
  • Monitoring: Docker Compose monitors the exit status of containers while they are running.
  • Stopping: If any container exits (with any exit code), Docker Compose will stop all other containers immediately.
  • Exit Codes: Despite any exit code that a container returns, `--abort-on-container-exit` will terminate all running containers. It's essential to design your application with appropriate error handling if necessary.
  • Data Persistence: Ensure data is appropriately handled and persisted outside of containers, as abrupt terminations may otherwise result in data loss.
  • Docker Compose Version: Ensure you are using a version of Docker Compose that supports `--abort-on-container-exit` (at least version 1.7.0).

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.