Running a multi-service app with Docker Compose: depends_on with healthy checks and a .env file

  1. Goal
  2. Prerequisites
  3. Why Compose
  4. Steps
  5. Step 1: write compose.yaml
  6. Step 2: create the .env file
  7. Step 3: see the result after substitution
  8. Step 4: run
  9. What depends_on really does
  10. Checking
  11. Common problems
  12. Summary
  13. References

Goal

Run an application made of two services, app and db (PostgreSQL), with a single command.

You will see app start only once db reports healthy, and the password live in .env instead of the configuration file.

Prerequisites

Why Compose

Running containers one by one with docker run and dozens of options is easy to get wrong and hard to remember.

Compose puts everything in one file: which image, which port, which environment variables, which service depends on which. The file sits next to the source code, so the whole team runs the same thing.

The recommended file name is compose.yaml; the version field at the top of the file is obsolete and can be dropped.

Compose also creates a private network for the project. Services call each other by service name: app connects to the database at the address db, port 5432, with no need to know an IP.

Steps

Steps

Step 1: write compose.yaml

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env}
      POSTGRES_DB: appdb
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d appdb"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 10s

  app:
    build: .
    ports:
      - "${APP_PORT:-8080}:8080"
    environment:
      DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/appdb
    depends_on:
      db:
        condition: service_healthy

A few points to understand:

Step 2: create the .env file

In the same directory as compose.yaml, create .env:

DB_PASSWORD=change-this-password
APP_PORT=8080

Compose automatically reads the .env file in the project directory to substitute the ${...} placeholders in compose.yaml.

Add .env to .gitignore; if you need to share a template, commit a .env.example that holds only dummy values.

Note two different things: .env is used to substitute values in the compose file, while a service's env_file: entry loads variables inside the container.

If you want variables from .env to be present in the container, you must declare them under environment: as in the example above (or use env_file:).

Step 3: see the result after substitution

docker compose config

The command prints the final configuration with every variable substituted. It also reports syntax errors, so run it before up.

Note that the output contains the real password, so do not paste it anywhere public.

Step 4: run

docker compose up -d --wait

-d runs in the background; --wait makes the command wait until services are running (or healthy, if they have a healthcheck) before returning.

If a service is not healthy, the command reports an error, which is handy in deployment scripts.

What depends_on really does

The short form depends_on: [db] only guarantees that the db container is started before app.

But PostgreSQL needs a few seconds to initialise, so app may open a connection too early and fail. The long form above has three conditions:

Condition

service_started

Meaning

The container has been started (the default)

Condition

service_healthy

Meaning

The service's healthcheck has reported healthy

Condition

service_completed_successfully

Meaning

The service ran to completion and exited with code 0, suited to a database migration step

The service_healthy condition works only when the dependency has a healthcheck.

Your application should still know how to retry its connection when the database hiccups; depends_on only handles the startup order.

Checking

docker compose ps
docker compose logs --tail 20 app

The status column of db must be healthy, and app must be running.

Try removing the DB_PASSWORD line from .env and running docker compose config again: you will see the error message you put after :?.

When you are done:

docker compose down

This stops and removes the project's containers and network.

Data in volumes is kept; adding -v removes the volumes too, so warning: docker compose down -v permanently deletes the database's data.

Common problems

Summary

Compose gathers the whole application into one file: use depends_on with service_healthy to wait at the right moment, .env for per-machine values, and docker compose config to review before running.

A sensible next step is to understand where the data of db is stored.

References

Running a multi-service app with Docker Compose: depends_on with healthy checks and a .env file