Using Docker Compose for Local Development Environments
Introduction
Docker Compose simplifies running multi-container applications locally, ensuring all dependencies are available with a single command.
Prerequisites
- Docker and Docker Compose installed
- Source code of application
Step 1: Create docker-compose.yml
version: '3.8'
services:
app:
build: .
command: pnpm dev
ports:
- '3000:3000'
environment:
DATABASE_URL: postgres://postgres:password@db:5432/app
REDIS_URL: redis://redis:6379
volumes:
- .:/app
- /app/node_modules
depends_on:
- db
- redis
db:
image: postgres:14-alpine
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: app
ports:
- '5432:5432'
volumes:
- db-data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
ports:
- '6379:6379'
mailhog:
image: mailhog/mailhog
ports:
- '1025:1025'
- '8025:8025'
adminer:
image: adminer
restart: always
ports:
- '8080:8080'
depends_on:
- db
volumes:
db-data:
Step 2: Commands
- Start services:
docker-compose up —build
- Run in background:
```bash
docker-compose up -d
- Stop services:
docker-compose down
- View logs:
```bash
docker-compose logs -f
- Execute commands in container:
docker-compose exec app sh
## Step 3: Service Access
- Application: http://localhost:3000
- PostgreSQL (Adminer): http://localhost:8080
- Redis CLI:
```bash
docker-compose exec redis redis-cli
- Mailhog UI: http://localhost:8025
Step 4: Tips
- Use
.env
file and reference in Compose:
environment:
- DB_USER=${DB_USER}
- Enable healthchecks:
```yaml
healthcheck:
test: ['CMD','curl','-f','http://localhost:3000/health']
interval: 30s
retries: 3
Summary
Docker Compose provides a reproducible local environment, orchestrating multiple dependencies for development and testing with minimal setup.