Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

Docker Compose for Local Development: A Practical Guide

Run your app, database and cache with one command. Learn Docker Compose services, volumes, networks, health checks, environment variables and hot reload for local development.

Sumit KumarUpdated Oct 8, 20263 min readDocker
Docker Compose for Local Development: A Practical Guide

A real app rarely runs alone. It needs a database, maybe a cache, maybe a queue. Installing all of that on every developer's laptop is slow and causes the classic "works on my machine" bug. Docker Compose describes the whole stack in one file and starts it with one command.

What Docker Compose does#

Docker runs one container. Compose runs a group of containers that work together, called services, and connects them on a private network. You describe everything in a compose.yaml file:

  • which image each service uses, or how to build it
  • which ports to expose to your machine
  • environment variables
  • volumes for data that must survive restarts
  • which services depend on which
Docker Compose network: browser reaches api on port 3000; api reaches db at db:5432 and cache at cache:6379 by service name

A complete example#

Here is a Node.js API with PostgreSQL and Redis:

YAML
# compose.yaml
services:
  api:
    build: .
    ports:
      - "3000:3000"
    env_file: .env
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/app
      REDIS_URL: redis://cache:6379
    volumes:
      - ./src:/app/src
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started

  db:
    image: postgres:16
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U app"]
      interval: 5s
      retries: 10

  cache:
    image: redis:7

volumes:
  db-data:

And a simple Dockerfile for the API:

DOCKERFILE
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["npm", "run", "dev"]

The key ideas, one by one#

Services find each other by name#

Compose puts all services on one network and gives each a hostname equal to its service name. That is why the API connects to db:5432 and cache:6379, not localhost. Inside a container, localhost means the container itself.

Ports: host first, container second#

"3000:3000" means port 3000 on your machine maps to port 3000 in the container. You only need to publish ports you want to reach from your browser or tools. The cache above has no ports entry, so only other services can reach it.

Volumes keep data and enable hot reload#

Named volume keeps database data managed by Docker; bind mount syncs ./src on the laptop with /app/src in the container for hot reload
  • Named volume (db-data): Docker manages it, and your database survives docker compose down.
  • Bind mount (./src:/app/src): your local folder appears inside the container, so a file watcher such as nodemon or node --watch restarts the app when you save.

Health checks fix start-up races#

A plain depends_on only waits for the database container to start, not to be ready. With condition: service_healthy, Compose waits until the health check passes, so your API doesn't crash with "connection refused" on the first boot.

Timeline: without a health check the api starts early and gets connection refused; with service_healthy the api waits until the database is healthy

Commands you'll use every day#

CommandWhat it does
docker compose up -dStart everything in the background
docker compose up --buildRebuild images, then start
docker compose logs -f apiFollow one service's logs
docker compose psList services and their status
docker compose exec db psql -U appOpen a shell or tool inside a service
docker compose downStop and remove containers
docker compose down -vAlso delete volumes (wipes data)
Tip

Modern Docker uses docker compose (with a space) built into the Docker CLI. The old docker-compose binary and the top-level version: key in the file are no longer needed.

Environment variables and secrets#

Put local settings in a .env file, load it with env_file, and add .env to .gitignore. Commit a .env.example with dummy values so teammates know what to fill in. The passwords in a local compose file are fine for development, but never reuse them in production.

Common problems#

  • "Port is already allocated": something on your machine already uses that port. Stop it or change the host side, for example "5433:5432".
  • Code changes don't show up: check the bind mount path, and make sure the dev command watches files.
  • Database keeps old data: that is the named volume doing its job. Use docker compose down -v to start fresh.
  • node_modules conflicts: mount only src (as above), not the whole project, so the container keeps its own installed packages.

Key takeaways#

  • Compose describes your whole stack in one compose.yaml and starts it with docker compose up.
  • Services reach each other by service name, not localhost.
  • Named volumes keep data; bind mounts give hot reload.
  • Use health checks with condition: service_healthy to avoid start-up races.