Skip to main content

Building a Docker Compose Local Development Stack

Build a Docker Compose local development stack with hot reloading, PostgreSQL, Redis, RabbitMQ, and a debugger for a Node.js API.

44 Terms

Overview and What You Will Learn

A good local development stack starts with one command, reloads code instantly, keeps each developer's data isolated, and uses the same service versions as production. Docker Compose delivers all four when you bind-mount source code, expose tool ports to localhost, and keep optional helpers behind profiles.

In this lab you will:

  • Run an API with PostgreSQL, Redis, and RabbitMQ using one docker compose up.
  • Enable hot reloading with a bind mount, and with Compose Watch as the modern alternative.
  • Attach a Node.js debugger and connect GUI tools to your databases.
  • Put optional services such as a mail catcher behind a profile.
  • Reset the database correctly, including the init-script gotcha.

Why This Matters in Production

Most "works on my machine" bugs come from differing versions of databases, queues, and runtimes. When every engineer runs the same pinned images from the same file, bugs reproduce the same way for everyone, and production-only surprises shrink. Teams at companies like Spotify and Swiggy use containerised dependencies for exactly this reason: the local stack is a cheap rehearsal of the real one.

A second benefit is isolation. Each developer owns a private database volume, so experiments and destructive migrations never affect a shared environment.

Core Principles

Source code travels into the container through a bind mount, while dependencies stay inside the image so native modules match the container's operating system.

flowchart TD H[Host: ./src] -->|bind mount| A[api container] A -->|DB_HOST| P[(postgres)] A -->|REDIS_HOST| R[(redis)] A -->|AMQP_URL| Q[rabbitmq] D[Developer tools] -->|localhost ports| P D -->|localhost ports| Q classDef host fill:#FFEDD5,color:#1E293B classDef app fill:#DBEAFE,color:#1E293B classDef data fill:#D1FAE5,color:#1E293B class H,D host class A app class P,R,Q data

Keep these rules in mind:

  1. Mount source, not dependencies. A host node_modules built for macOS or Windows breaks native modules in a Linux container.
  2. Bind ports to 127.0.0.1. Developer tools get access, but other machines on the network do not.
  3. Mirror production images. Use the same major versions of PostgreSQL, Redis, and RabbitMQ as production.
  4. Use development overrides, not a different file. Keep one base definition and layer dev-only settings on top.

Detailed Step-by-Step Practical Lab

How to Define the Full Development Stack
YAML
name: orders-dev
services:
api:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
- "127.0.0.1:9229:9229" # Node.js inspector, local only
volumes:
- ./src:/app/src # hot reload source
- api-node-modules:/app/node_modules # keep Linux-built modules
environment:
NODE_ENV: development
DB_HOST: postgres
DB_NAME: orders_dev
DB_USER: developer
DB_PASSWORD: ${DB_PASSWORD:-devpassword}
REDIS_HOST: redis
AMQP_URL: amqp://developer:${MQ_PASSWORD:-devpassword}@rabbitmq:5672
SMTP_HOST: mailhog
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
rabbitmq:
condition: service_healthy
command: ["npm", "run", "dev"]
postgres:
image: postgres:16-alpine
ports:
- "127.0.0.1:5432:5432" # for TablePlus, DBeaver, psql
environment:
POSTGRES_DB: orders_dev
POSTGRES_USER: developer
POSTGRES_PASSWORD: ${DB_PASSWORD:-devpassword}
volumes:
- postgres-dev-data:/var/lib/postgresql/data
- ./db/seed.sql:/docker-entrypoint-initdb.d/seed.sql:ro
healthcheck:
test: ["CMD-SHELL", "pg_isready -U developer -d orders_dev"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
redis:
image: redis:7-alpine
ports:
- "127.0.0.1:6379:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
rabbitmq:
image: rabbitmq:3-management-alpine
ports:
- "127.0.0.1:5672:5672"
- "127.0.0.1:15672:15672" # management UI
environment:
RABBITMQ_DEFAULT_USER: developer
RABBITMQ_DEFAULT_PASS: ${MQ_PASSWORD:-devpassword}
volumes:
- rabbitmq-dev-data:/var/lib/rabbitmq
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
mailhog:
image: mailhog/mailhog
profiles: ["email"] # only starts when requested
ports:
- "127.0.0.1:1025:1025" # SMTP
- "127.0.0.1:8025:8025" # web UI
volumes:
api-node-modules:
postgres-dev-data:
rabbitmq-dev-data:
How to Write a Development Dockerfile
Dockerfile
# Dockerfile.dev
FROM node:20-alpine
WORKDIR /app
# Install all dependencies, including devDependencies
COPY package.json package-lock.json ./
RUN npm ci
# Source is bind-mounted at run time, so it is not copied here
EXPOSE 3000 9229
CMD ["npm", "run", "dev"]

The dev script in package.json should run a watcher such as nodemon --watch src --ext ts,js,json --inspect=0.0.0.0:9229 --exec ts-node src/index.ts. Binding the inspector to 0.0.0.0 inside the container is required, and the 127.0.0.1 mapping above keeps it private on the host.

How to Enable Hot Reloading with Compose Watch

Compose 2.22 and later can sync files without a bind mount, which avoids slow file sharing on macOS and Windows. Add a develop block to the service:

YAML
services:
api:
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: package.json
Bash
docker compose watch
# source edits sync into the container, package.json changes rebuild the image
How to Start Optional Services with Profiles
Bash
docker compose up -d # api, postgres, redis, rabbitmq
docker compose --profile email up -d # also starts mailhog
How to Run Migrations, Tests, and Shells
Bash
docker compose exec api npm run migrate
docker compose exec api npm test
docker compose exec postgres psql -U developer -d orders_dev
docker compose logs -f api
How to Reset the Database Correctly

Scripts in /docker-entrypoint-initdb.d run only when PostgreSQL initialises an empty data directory. Restarting the container will not re-run seed.sql.

Bash
# Option 1: recreate just the database, keeping the volume
docker compose exec postgres psql -U developer -d postgres \
-c "DROP DATABASE orders_dev WITH (FORCE);" \
-c "CREATE DATABASE orders_dev;"
docker compose exec api npm run migrate
# Option 2: full reset, re-runs seed.sql (deletes this project's volumes)
docker compose down -v
docker compose up -d

Production Best Practices & Common Pitfalls

  • Match production versions: if production runs PostgreSQL 16, develop on 16, not latest.
  • Keep dev-only settings out of production files: debugger ports, exposed databases, and weak passwords belong in development configuration only.
  • Refresh the modules volume after dependency changes: run docker compose up -d --build -V so the named node_modules volume is rebuilt instead of serving stale packages.
  • Do not commit .env: commit .env.example with safe defaults.
  • Bind ports to localhost: an exposed PostgreSQL port on 0.0.0.0 is reachable by anyone on the same café Wi-Fi.
  • Expect slower file sharing on macOS and Windows: prefer Compose Watch for large source trees.
Tip

Expose database and queue ports on 127.0.0.1 so GUI tools such as TablePlus and the RabbitMQ management UI work, then omit those ports in any shared or production compose file.

Remember

Mounting only ./src and keeping node_modules in a separate volume stops host-built native modules from overriding the Linux ones inside the container.

Common Mistake

Editing seed.sql and expecting a container restart to apply it. Init scripts run once on an empty volume, so reset the volume or run the SQL by hand.

Security

Development passwords such as devpassword must never appear in production configuration. Keep them in .env defaults used only locally.

Quick Reference & Troubleshooting Commands

Command Description
docker compose up -d Start the core stack
docker compose --profile email up -d Start the stack plus the mail catcher
docker compose watch Sync and rebuild on file changes
docker compose exec api npm run migrate Run migrations inside the API container
docker compose up -d --build -V Rebuild and renew anonymous volumes
docker compose down -v Delete containers and this project's volumes
Problem Likely cause Fix
Code changes do not reload Watcher not running or no mount Check the dev script and the ./src mount
Native module crash on start Host node_modules overrides container Use a separate modules volume
seed.sql not applied Volume already initialised Reset the volume with down -v
Cannot attach debugger Inspector bound to 127.0.0.1 in container Use --inspect=0.0.0.0:9229
port is already allocated Local PostgreSQL already running Stop it or change the host port

Resources

Explore More in Docker Compose and Multi-Container Applications

All 6 Topics

Frequently Asked Questions

Is Building a Docker Compose Local Development Stack free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Building a Docker Compose Local Development Stack topic cover?

Build a Docker Compose local development stack with hot reloading, PostgreSQL, Redis, RabbitMQ, and a debugger for a Node.js API.

Found a mistake, an outdated step or a broken link?