By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Dependency control in Docker allows you to define relationships between containers. depends_on is the key command to specify these relationships.
mkdir docker-project && cd docker-project
docker-compose.yml
version: '3' services: db: image: postgres web: image: python:3.9-slim depends_on: - db
docker-compose up -d
docker-compose ps
docker-compose down
rm docker-compose.yml
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.
-d
depends_on
web
db
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.
healthcheck
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.
condition
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.
web_1 exited with code 1
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.
db_1 is unhealthy
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.
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.
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.
docker-compose logs
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.
• 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.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.