Skip to the page

Docker for Node and Next.js apps · core

docker compose for local stacks

docker compose runs several containers (API, Postgres, Redis) from one compose.yaml with a shared network where services reach each other by service name, named volumes for data, and health-checked startup order.

The notes

What you must know

  • File name compose.yaml (compose.yml and docker-compose.yml still work); the top-level version: key is obsolete. Command: docker compose (v2), not docker-compose.
  • services: image or build, ports 'host:container', environment / env_file, volumes, depends_on, healthcheck, command.
  • Networking: services share a default network and use the service name as the hostname: DATABASE_URL=postgres://app:pw@postgres:5432/app, not localhost.
  • Named volumes (pgdata:/var/lib/postgresql/data) keep database data across restarts; bind mounts (./src:/app/src) are for live code in development.
  • depends_on only waits for the container to start; use condition: service_healthy with a healthcheck (pg_isready) to wait until Postgres accepts connections.
  • Useful commands: docker compose up -d, logs -f api, exec api sh, down (keeps volumes), down -v (deletes volumes and data).
  • Compose is great for local development and CI test stacks; production usually runs on ECS or Kubernetes.

The red pen

Common traps: mistake → fix

  • Mistake: Use localhost to reach Postgres from the API container

    Fix: localhost is the API container itself; use the service name.

  • Mistake: depends_on means the database is ready

    Fix: it only means the container started; add a healthcheck condition.

  • Mistake: docker compose down -v is a normal restart

    Fix: -v deletes the named volumes, including your database data.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you run your API with Postgres and Redis locally?
  2. How do containers in compose find each other?
  3. How do you make the API wait until Postgres is ready?
  4. Where does Postgres data live in your compose setup?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview