Skip to Content
Mengty Learns
Containers

Docker Compose

Run multi-container stacks locally or on a single host with automatic service DNS, health-gated depends_on, isolated networks, env files, secrets, and profiles.

explanationbeginnerTested with docker 29.4.3, docker-compose 5.1.3Updated Nov 26, 2026

TL;DR

Docker Compose wires multi-container apps together on one host using a single compose.yaml. Every service gets an entry in Compose's embedded DNS, explicit networks isolate traffic, depends_on with condition: service_healthy prevents race conditions at startup, and profiles keep optional tools out of your default stack.

Intent

TL;DR

Understand how Docker Compose resolves service names over DNS, gates startup on real healthchecks, and separates configuration (env_file) from credentials (secrets) without needing Kubernetes.

Problem

Running docker run by hand for a web server, database, and cache quickly breaks down: container IPs change on every restart, the web app crashes if it connects before Postgres finishes initializing, and passing passwords via -e exposes them in docker inspect.

Solution

Compose reads a declarative compose.yaml and sets up three primitives automatically:

  1. Service DNS & networks: Compose creates a project-scoped bridge network. Every container's /etc/resolv.conf points to Docker's embedded DNS server (127.0.0.11), which resolves service names (like db) to the active container IP. You can also split frontend and backend networks with internal: true so the database has no outbound internet route.
  2. Startup order with healthchecks: Plain depends_on: [db] only waits for the container to start, not for the process inside to accept connections. Pairing depends_on with condition: service_healthy and a healthcheck ensures web starts only after db passes its readiness command (e.g., pg_isready).
  3. env_file vs secrets, plus profiles: Use env_file for non-sensitive config (NODE_ENV, PORT), secrets to mount sensitive files at /run/secrets/<name>, and profiles: ["debug"] so admin UIs or seeders only run when requested (docker compose --profile debug up).

Real-world analogy

▶ Flow: watch it work

Service-to-Service Request via Compose DNS

Overview of Service-to-Service Request via Compose DNS. 4 steps.
Overview4 steps in this flow

Press Start or tap any step dot to trace the path.

▸All steps (4)

Code

A minimal, production-minded compose.yaml using service_healthy, isolated networks, env_file, secrets, and profiles:

# compose.yaml (not run)
services:
  web:
    build: .
    ports:
      - "3000:3000"
    env_file:
      - .env.app
    secrets:
      - db_password
    networks:
      - frontend
      - backend
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:17-alpine
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets:
      - db_password
    networks:
      - backend
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 3s
      retries: 5
    volumes:
      - pgdata:/var/lib/postgresql/data

  adminer:
    image: adminer:4
    profiles: ["debug"]
    ports:
      - "8080:8080"
    networks:
      - backend

networks:
  frontend:
  backend:
    internal: true

volumes:
  pgdata:

secrets:
  db_password:
    file: ./secrets/db_password.txt

When you DON'T need this

  • Single-container apps or static sites: If your app is one stateless binary or static assets (like this site), docker run or a static host is simpler than maintaining a compose.yaml.
  • Multi-node scheduling or auto-scaling: Compose targets a single Docker Engine host. Once you need rolling updates across multiple machines, bin-packing, or self-healing across node failures, move to Kubernetes or a managed container platform.
  • Cost / complexity trade-off: Compose is free and ships with Docker Desktop / Docker Engine, so its operational overhead is near zero—but single-host simplicity means zero built-in high availability if that host goes down.

When to use / when to avoid

When to use
  • Local development environments where an app needs a database, cache, or queue with one command.
  • Single-VM deployments where simplicity matters more than multi-node orchestration.
When to avoid
  • Production workloads requiring zero-downtime multi-node failover or horizontal autoscaling.
  • Sharing host secrets via plain .env files committed to Git.

How to implement

Define isolated networks

Put web on frontend and backend, and put db only on backend with internal: true.

Add a real healthcheck on dependencies

Gate web with depends_on.db.condition: service_healthy so startup waits for readiness.

Separate env_file from secrets

Keep non-sensitive flags in .env.app and mount credentials via secrets: at /run/secrets/.

Pros / Cons

Pros
  • Zero cluster overhead: one YAML file and docker compose up -d.
  • Automatic service discovery via embedded DNS without hardcoding IPs.
  • Profiles keep heavy debug or migration containers out of normal runs.
Cons
  • Single-host only: no built-in multi-node scheduling or automatic node failover.
  • Compose secrets on standalone Engine are plain bind-mounted files, not encrypted-at-rest Swarm/KMS secrets.

Relations

Check yourself

Check Yourself

1. Why does depends_on: [db] without a condition still let your web app fail on startup?

2. Where does Docker Compose mount a secret named db_password inside a container by default?

Sources