Node.js › Docker & Containerization for Node.js
How does Docker Compose simplify running a Node.js application alongside its dependencies (like a database and Redis) during local development?
AdvancedAnswer
Docker Compose defines multiple related services (the Node.js app, a PostgreSQL database, a Redis cache) in a single docker-compose.yml file, including their networking (services can reach each other by service name), volumes (for persisting database data across restarts), and startup dependencies -- letting a developer spin up an entire multi-service local development environment with one command, rather than manually installing and configuring each dependency directly on their machine.
# docker-compose.yml
services:
app:
build: .
ports: ['3000:3000']
depends_on: [db, redis]
environment:
- DATABASE_URL=postgres://user:pass@db:5432/mydb
- REDIS_URL=redis://redis:6379
db:
image: postgres:16
volumes: ['pgdata:/var/lib/postgresql/data']
redis:
image: redis:7
volumes:
pgdata:Real-world example A new engineer joining a team runs a single 'docker-compose up' command and has a fully working local development environment -- the Node.js app, a PostgreSQL database, and Redis all running and correctly networked together -- within minutes, instead of spending their first day manually installing and configuring PostgreSQL and Redis locally.