Docker Compose

Difficulty: Beginner

Question

What is Docker Compose and why is it used? How do services in a Compose file talk to each other?

Answer

Docker Compose is a tool for defining and running multi-container applications from a single YAML file, usually compose.yaml. You describe each service (its image or build, ports, environment, volumes, dependencies) once, then docker compose up creates the network, volumes and containers together, and docker compose down tears them down. It turns a long list of docker run commands into versioned, shareable configuration.

What Compose sets up for you

When you run docker compose up -d in a project directory, Compose:

  1. Reads compose.yaml and merges compose.override.yaml if present.
  2. Creates a dedicated network called <project>_default.
  3. Creates any named volumes declared under volumes:.
  4. Builds images for services with a build: section, or pulls the image:.
  5. Starts containers in dependency order, each attached to the project network.

On that network every service is reachable by its service name through Docker's built-in DNS. If you have services api and redis, the API connects to redis:6379, not localhost:6379. Inside a container, localhost means that container itself.

Why teams use it

  • Reproducible local environments: a new intern clones the repo, runs one command and gets the API, Postgres, Redis and a mock payment service running with the same versions everyone else uses.
  • Integration tests in CI: spin up real dependencies, run tests, tear down.
  • Small single-host deployments: internal tools or side projects on one VM.
  • Infrastructure as code: the environment is reviewed in pull requests like any other file.

For multi-node production with self-healing and autoscaling, teams typically move to Kubernetes or a managed container service; Compose is a single-host tool.

Things that changed recently

  • Compose v2 is a Docker CLI plugin invoked as docker compose (with a space). The old Python docker-compose v1 is end of life.
  • The top-level version: key is obsolete under the Compose Specification; current Compose ignores it and prints a warning.
  • depends_on only controls start order by default. To wait until a database is actually ready, add a healthcheck to the database and use condition: service_healthy.
  • docker compose watch with a develop.watch section syncs file changes or rebuilds automatically, an alternative to bind mounts for hot reload.
  • profiles let you start optional services (for example a monitoring stack) only when asked.

Common mistakes

  • Connecting to localhost from one container to reach another.
  • Assuming depends_on waits for readiness, causing the API to crash on boot because Postgres is still initialising.
  • Running docker compose down -v and losing the local database volume.
  • Committing real secrets in environment:; use an .env file that is git-ignored, or Compose secrets.
What docker compose up does. One command creates the network, volumes and containers in dependency order.
  1. Parse compose.yaml and overrides: Merges files and resolves .env variables.
  2. Create project network: A user-defined bridge with DNS for service names.
  3. Create named volumes: Volumes persist across down and up unless -v is used.
  4. Build or pull images: Uses build: or image: for each service.
  5. Start containers by dependency: Waits for healthchecks when condition is service_healthy.

Code examples

API + Postgres + Redis with health-gated startup

# compose.yaml (no version key needed)
services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://app:devpass@db:5432/orders
      REDIS_URL: redis://cache:6379
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started

  db:
    image: postgres:17
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: devpass
      POSTGRES_DB: orders
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app -d orders"]
      interval: 5s
      retries: 10

  cache:
    image: redis:7-alpine

volumes:
  pgdata:

The API reaches the database at host db and Redis at host cache via Compose DNS, and it only starts once Postgres passes its healthcheck.

Everyday Compose commands

docker compose up -d --build   # build images and start in background
docker compose ps              # status of services
docker compose logs -f api     # follow logs of one service
docker compose exec db psql -U app orders
docker compose down            # stop and remove containers + network
docker compose down -v         # ALSO delete named volumes (data loss!)

These cover the daily loop of starting, inspecting, debugging and tearing down a local stack.

Key points

  • Compose defines a multi-container app in one YAML file and runs it with docker compose up.
  • Services reach each other by service name on the project network, never via localhost.
  • depends_on orders startup; add healthchecks and service_healthy to wait for readiness.
  • The version key is obsolete; use docker compose (v2 plugin), not docker-compose v1.
  • Compose is single-host; Kubernetes handles multi-node orchestration.

Common follow-up questions

  • How do two services in a Compose file communicate with each other?
  • What does docker compose up do step by step?
  • How do you make a service wait until the database is ready?
  • How would you use different settings for dev and CI with the same Compose file?
  • When would you move from Compose to Kubernetes?

Interview tips

  • Explain service-name DNS and why localhost fails between containers; it is the most common follow-up.
  • Mentioning that the version key is obsolete and depends_on needs healthchecks shows current, hands-on knowledge.
  • Position Compose correctly: great for dev, CI and single-host apps, not a replacement for Kubernetes.

Concepts covered

Docker Compose, compose.yaml, Service Discovery, depends_on, Multi-container Apps

More Docker & Containers questions