0
0
Dockerdevops~5 mins

Depends_on for service ordering in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When running multiple services together, some services need to start before others. Depends_on helps control the order so services start in the right sequence.
When you want a database service to start before your web app connects to it.
When a cache service must be ready before your application uses it.
When you have multiple microservices that depend on each other starting in order.
When you want to avoid errors caused by services trying to connect before others are ready.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
  web:
    image: nginx:stable
    depends_on:
      - db

This file defines two services: db and web.

The depends_on key under web tells Docker Compose to start the db service before starting web.

This helps ensure the database is running before the web server tries to use it.

Commands
This command starts all services defined in the docker-compose.yml file in detached mode, respecting the depends_on order.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "default_db_data" with default driver Creating default_db_1 ... done Creating default_web_1 ... done
-d - Run containers in the background (detached mode)
This command lists all running services and their current status to verify they started correctly.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports -------------------------------------------------------------------------------- default_db_1 docker-entrypoint.sh postgres Up 5432/tcp default_web_1 nginx -g 'daemon off;' Up 80/tcp
This command shows the logs of the web service to check if it started without errors and connected to the db.
Terminal
docker-compose logs web
Expected OutputExpected
[default_web_1] 2024/06/01 12:00:00 [notice] 1#1: using the "epoll" event method [default_web_1] 2024/06/01 12:00:00 [notice] 1#1: nginx started
Key Concept

depends_on controls the start order of services but does not wait for a service to be fully ready.

Common Mistakes
Assuming depends_on waits for the service to be fully ready before starting the next.
depends_on only waits for the container to start, not for the service inside to be ready, which can cause connection errors.
Use healthchecks with depends_on condition or add retry logic in your application to handle service readiness.
Not specifying depends_on when one service needs another to be running first.
Services may start in any order, causing failures if dependencies are not ready.
Always use depends_on to define service start order when dependencies exist.
Summary
depends_on in docker-compose.yml sets the order services start.
docker-compose up -d starts services respecting depends_on.
Check service status with docker-compose ps and logs to confirm proper startup.