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.
Keep these rules in mind:
- Mount source, not dependencies. A host
node_modulesbuilt for macOS or Windows breaks native modules in a Linux container. - Bind ports to
127.0.0.1. Developer tools get access, but other machines on the network do not. - Mirror production images. Use the same major versions of PostgreSQL, Redis, and RabbitMQ as production.
- 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
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.devFROM node:20-alpineWORKDIR /app # Install all dependencies, including devDependenciesCOPY package.json package-lock.json ./RUN npm ci # Source is bind-mounted at run time, so it is not copied hereEXPOSE 3000 9229CMD ["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:
services: api: develop: watch: - action: sync path: ./src target: /app/src - action: rebuild path: package.jsondocker compose watch# source edits sync into the container, package.json changes rebuild the imageHow to Start Optional Services with Profiles
docker compose up -d # api, postgres, redis, rabbitmqdocker compose --profile email up -d # also starts mailhogHow to Run Migrations, Tests, and Shells
docker compose exec api npm run migratedocker compose exec api npm testdocker compose exec postgres psql -U developer -d orders_devdocker compose logs -f apiHow 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.
# Option 1: recreate just the database, keeping the volumedocker 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 -vdocker compose up -dProduction 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 -Vso the namednode_modulesvolume is rebuilt instead of serving stale packages. - Do not commit
.env: commit.env.examplewith safe defaults. - Bind ports to localhost: an exposed PostgreSQL port on
0.0.0.0is reachable by anyone on the same café Wi-Fi. - Expect slower file sharing on macOS and Windows: prefer Compose Watch for large source trees.
TipExpose database and queue ports on
127.0.0.1so GUI tools such as TablePlus and the RabbitMQ management UI work, then omit those ports in any shared or production compose file.
RememberMounting only
./srcand keepingnode_modulesin a separate volume stops host-built native modules from overriding the Linux ones inside the container.
Common MistakeEditing
seed.sqland 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.
SecurityDevelopment passwords such as
devpasswordmust never appear in production configuration. Keep them in.envdefaults 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 |