Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker Compose Dependency Control depends on healthcheck condition
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-compose-dependency-control-depends-on-healthcheck-condition

Docker Docker Docker Compose Dependency Control depends on healthcheck condition

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Dependency control in Docker allows you to define relationships between containers. depends_on is the key command to specify these relationships.

2. Step-by-Step

  1. Create a new directory for your project and navigate into it: mkdir docker-project && cd docker-project
  2. Create a new file named docker-compose.yml and add the following content:
version: '3'
services:
  db:
image: postgres web:
image: python:3.9-slim
depends_on:
- db
  1. Run docker-compose up -d to start the containers in detached mode: docker-compose up -d
  2. Check the status of the containers: docker-compose ps
  3. ⚠️ This deletes all stopped containers: docker-compose down
  4. Clean up by removing the docker-compose.yml file: rm docker-compose.yml

3. Real Commands with Examples


Example 1: Simple Dependency

Purpose: Start a web service that depends on a PostgreSQL database.
Command: docker-compose up -d Flags: -d runs the containers in detached mode.
Explanation: The depends_on directive in the docker-compose.yml file specifies that the web service depends on the db service.

Example 2: Health Check

Purpose: Start a web service that depends on a PostgreSQL database and has a health check.
Command: docker-compose up -d Flags: -d runs the containers in detached mode.
Explanation: The healthcheck directive in the docker-compose.yml file specifies a command to check the health of the db service.

Example 3: Conditional Start

Purpose: Start a web service that depends on a PostgreSQL database, but only if the database is available.
Command: docker-compose up -d Flags: -d runs the containers in detached mode.
Explanation: The condition directive in the docker-compose.yml file specifies that the web service should only start if the db service is available.

4. Common Errors


Error 1: Missing Dependency

Error message: web_1 exited with code 1 Why it happens: The web service is trying to start before the db service is available.
One-line fix: Add the depends_on directive to the docker-compose.yml file.

Error 2: Health Check Failure

Error message: db_1 is unhealthy Why it happens: The health check command failed for the db service.
One-line fix: Check the health check command in the docker-compose.yml file.

Error 3: Conditional Start Failure

Error message: web_1 exited with code 1 Why it happens: The db service is not available, so the web service is not starting.
One-line fix: Check the condition directive in the docker-compose.yml file.

5. Quick Checks


Scenario 1: Check Container Status

Situation: You want to check the status of the containers.
Command: docker-compose ps What a correct response looks like: The output should show the status of the containers, including the db and web services.

Scenario 2: Check Container Logs

Situation: You want to check the logs of the containers.
Command: docker-compose logs What a correct response looks like: The output should show the logs of the containers, including any errors or warnings.

Scenario 3: Check Container Health

Situation: You want to check the health of the containers.
Command: docker-compose ps What a correct response looks like: The output should show the health of the containers, including any errors or warnings.

6. Last-Minute Reference

depends_on: specifies the dependencies between services.
healthcheck: specifies a command to check the health of a service.
condition: specifies a condition for starting a service.
docker-compose up -d: runs the containers in detached mode.
docker-compose ps: shows the status of the containers.
docker-compose logs: shows the logs of the containers.
docker-compose down: stops and removes the containers.
docker-compose rm: removes the containers without stopping them.
docker-compose up --build: builds the images before starting the containers.
docker-compose config: validates the configuration file.




ADVERTISEMENT