๐ณ 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¶
- Introduction
- Prerequisites
- Installation
- How Docker Compose Works
- Getting Started
- Project Structure
- Creating a docker-compose.yml File
- Dockerfile for Development
- Development Workflow
- Common Docker Compose Commands
- Examples
- Tips and Best Practices
- Troubleshooting
- Resources
- 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:
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:
---
### ๐งฉ 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:¶
If dependencies change:¶
Full clean rebuild:¶
This approach:
- Matches production behavior
- Avoids dev-only tooling
- Keeps environment predictable
๐งช Common Docker Compose Commands¶
Start Services¶
Stop Services¶
Rebuild Images¶
Restart a Service¶
View Logs¶
List Containers¶
Execute Inside Container¶
๐ฆ 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:
๐ 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¶
- Docker Compose Docs: https://docs.docker.com/compose/
- Docker Docs: https://docs.docker.com/
- Docker Hub: https://hub.docker.com/
๐ 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