Don't Restart Your Dependent Containers
When running a service with Docker Compose, we don't always want to restart dependent services. Learn why and how to avoid unnecessary restarts.
Table of Contents 📖
Dependent Containers
When you run a service with Docker Compose, the dependent containers are also restarted. For example, consider the docker-compose.yaml configuration below where the Grafana service depends on the Prometheus service (Prometheus is a data source for Grafana).
grafana:
pull_policy: always
image: grafana/grafana
container_name: ${GRAFANA_CONTAINER}
restart: always
ports:
- ${GRAFANA_PORT}:${GRAFANA_PORT}
env_file: .prod.env
networks:
- wb-bridge-n
depends_on:
- prometheus
If I run the grafana service with Docker Compose, we can see in the terminal output that the dependent service has been restarted as well.
docker compose -f docker-compose-3.yaml --env-file .prod.env up -d grafana
[+] Running 3/3
⠿ prometheus Pulled
⠿ node-exporter Pulled
⠿ grafana Pulled
[+] Running 4/0
⠿ Container node-exporter-c Running
⠿ Container cadvisor-c Running
⠿ Container prometheus-c Running
⠿ Container grafana-c Running
Furthermore, not only do the dependent services restart, but the dependent services of the dependent services are also restarted. Both Node Exporter and cAdvisor are dependent on Prometheus, so when Prometheus is restarted, Node Exporter and cAdvisor are also restarted.
prometheus:
pull_policy: always
image: prom/prometheus:latest
container_name: ${PROMETHEUS_CONTAINER}
restart: always
env_file: .prod.env
volumes:
- ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/prometheus
expose:
- ${PROMETHEUS_PORT}
depends_on:
- node-exporter
- cadvisor
This can be an issue for production environments as minimizing downtime is critical and restarting dependencies can disrupt other services that rely on them.
A Handy Flag
Due to this behavior, Docker Compose provides us with a handy flag called --no-deps which will focus on a specific service without restarting or recreating its dependencies.
docker compose -f docker-compose-3.yaml --env-file .prod.env up -d --no-deps grafana
[+] Running 1/1
⠿ grafana Pulled
[+] Running 1/0
⠿ Container grafana-c Running
The --no-deps flag is used with the docker compose up command. It prevents Docker Compose from starting or recreating dependent services listed in the depends_on section of a docker-compose.yaml file. From this output, we can see that only the grafana service was restarted.
INFO: The --no-deps flag can save time, reduce downtime, and minimize unnecessary interruptions to your workflow.