Skip to content
Go back

Using Docker Compose for Local Development Environments

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

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

docker-compose up —build

- Run in background:
  ```bash
docker-compose up -d

docker-compose down

- View logs:
  ```bash
docker-compose logs -f

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

Step 4: Tips

environment:

- 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.


Share this post on:

Previous Post
Building a Custom CLI Tool with Node.js and TypeScript
Next Post
Configuring ESLint, Prettier, and Husky for Team Projects