Skip to content

๐Ÿณ Docker Compose Tutorial (Development Focused)

Welcome to the Docker Compose Tutorial for Development.
This guide explains how to use Docker Compose to run multi-container applications in a development environment, where:

  • Source code changes are reflected in containers
  • Containers can be rebuilt easily
  • No hot reload tools are required
  • Behavior stays close to production

๐Ÿ“‘ Table of Contents

  1. Introduction
  2. Prerequisites
  3. Installation
  4. How Docker Compose Works
  5. Getting Started
  6. Project Structure
  7. Creating a docker-compose.yml File
  8. Dockerfile for Development
  9. Development Workflow
  10. Common Docker Compose Commands
  11. Examples
  12. Tips and Best Practices
  13. Troubleshooting
  14. Resources
  15. License

๐Ÿ“Œ Introduction

Docker Compose is a tool that allows you to define and run multiple Docker containers as a single application.

Instead of running containers one by one, Docker Compose lets you: - Define services - Configure networks - Attach volumes - Start everything with one command

This tutorial focuses on development usage, not production.


โœ… Prerequisites

Ensure the following are installed:

  • Docker
  • Docker Compose (included with Docker Desktop)

Verify installation:

docker --version
docker compose version
````

---

## โš™ Installation

Follow the official installation guide if Docker is not installed:

* [https://docs.docker.com/get-docker/](https://docs.docker.com/get-docker/)

Docker Compose comes bundled with Docker Desktop (Windows/macOS/Linux).

---

## ๐Ÿง  How Docker Compose Works

Conceptually:
docker-compose.yml โ†“ Defines services (app, db, cache, etc.) โ†“ Docker builds images (if needed) โ†“ Docker runs containers โ†“ Containers share network & volumes
Key points:

* Each service = one container
* Services can talk using service names
* Volumes persist data
* Bind mounts sync source code

---

## ๐Ÿš€ Getting Started

### ๐Ÿ“ Project Structure

Example Node.js project:
project-root/ โ”œโ”€โ”€ docker-compose.yml โ”œโ”€โ”€ Dockerfile โ”œโ”€โ”€ package.json โ”œโ”€โ”€ server.js โ””โ”€โ”€ src/
---

### ๐Ÿงฉ Creating a `docker-compose.yml` File

This example defines:

* A backend service
* A PostgreSQL database
* Source code mounted for development

```yaml
version: "3.8"

services:
  app:
    build: .
    container_name: dev-app
    ports:
      - "3000:3000"
    volumes:
      - .:/app
    depends_on:
      - db
    environment:
      NODE_ENV: development

  db:
    image: postgres:15
    container_name: dev-db
    environment:
      POSTGRES_DB: devdb
      POSTGRES_USER: devuser
      POSTGRES_PASSWORD: devpass
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:


๐Ÿ“„ Dockerfile for Development

This Dockerfile is simple and explicit.

FROM node:18

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]

โš  No hot reload tools (nodemon, pm2) are used.


๐Ÿ” Development Workflow (Important Section)

This setup follows a manual-rebuild dev workflow:

When you change code:

  • Source code updates inside container (via volume)
  • App does NOT auto-restart

To reflect changes:

docker compose restart app

If dependencies change:

docker compose build
docker compose up -d

Full clean rebuild:

docker compose down
docker compose up -d --build

This approach:

  • Matches production behavior
  • Avoids dev-only tooling
  • Keeps environment predictable

๐Ÿงช Common Docker Compose Commands

Start Services

docker compose up
docker compose up -d

Stop Services

docker compose down

Rebuild Images

docker compose build

Restart a Service

docker compose restart app

View Logs

docker compose logs
docker compose logs -f app

List Containers

docker compose ps

Execute Inside Container

docker compose exec app bash

๐Ÿ“ฆ Examples

Node.js + PostgreSQL

  • Backend service
  • Database service
  • Persistent DB volume

Frontend + Backend

  • Frontend container (React/Vue build)
  • Backend API container

Backend + Redis

  • Cache layer
  • API service dependency

๐Ÿง  Tips and Best Practices

โœ” Use bind mounts only in development โœ” Avoid hot reload in Docker unless needed โœ” Keep Dockerfile simple โœ” Use .dockerignore โœ” One service = one responsibility โœ” Restart containers explicitly

Example .dockerignore:

node_modules
.git
.env

๐Ÿ›  Troubleshooting

Containers not updating after code change

  • Restart the service
  • Check volume mounts

Port already in use

  • Change host port
  • Stop conflicting service

Database data lost

  • Ensure named volumes are used

๐Ÿ“š Resources


๐Ÿ“„ License

This project is licensed under the MIT License.


๐ŸŽฏ Final Notes

This Docker Compose setup is ideal for:

  • Backend development
  • API services
  • Local testing
  • CI consistency

It avoids: โŒ Hot reload complexity โŒ Dev-only runtime hacks