In the previous episode, we explored Docker Networking and how containers communicate with each other. But as applications grow, managing multiple containers with just docker run
becomes painful. This is where Docker Compose comes to the rescue.
🔹 What is Docker Compose?
Docker Compose is a tool that allows you to define and run multi-container Docker applications. Instead of starting each container manually, you define services in a single YAML file (docker-compose.yml
) and bring everything up with one command.
🔹 Why Use Docker Compose?
- Manage multiple containers easily.
- Define networks and volumes in one place.
- Consistent environment setup across teams.
- One command (
docker-compose up
) starts the entire stack.
🔹 Installing Docker Compose
Most modern Docker installations already include Compose. To check:
docker compose version
If not installed, follow Docker’s official installation guide.
🔹 Basic docker-compose.yml
Example
Let’s run a simple web app with Flask (Python) and Redis.
version: '3'
services:
web:
build: ./app
ports:
- "5000:5000"
depends_on:
- redis
redis:
image: redis:alpine
Here:
-
web
→ our Flask app (built from./app
directory). -
redis
→ a Redis container using the official image. -
depends_on
→ ensuresredis
starts beforeweb
.
🔹 Running the App
docker compose up
This will start both containers together.
To stop:
docker compose down
🔹 Adding Volumes & Networks
You can define persistent storage and custom networks:
version: '3'
services:
db:
image: postgres:alpine
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
This ensures your database data isn’t lost when the container restarts.
🔹 Real-World Example
A full-stack app might have:
- Frontend (React/Next.js)
- Backend (Node.js, Django, Flask, etc.)
- Database (Postgres, MongoDB, MySQL)
- Cache (Redis)
All defined in a single docker-compose.yml
file.
🔹 Key Commands
-
docker compose up -d
→ start in detached mode. -
docker compose down
→ stop and remove containers. -
docker compose logs -f
→ view logs. -
docker compose ps
→ list running services.
🔹 Best Practices
- Use environment files (
.env
) to manage secrets. - Keep services modular (separate DB, cache, etc.).
- Use volumes for persistent data.
- Avoid hardcoding credentials inside
docker-compose.yml
.
✅ In the next episode, we’ll cover Episode 17: Docker Volumes & Persistent Data Management — making sure your data survives container restarts.
💡 Question for you: Do you want me to create a real-world multi-service app with Docker Compose (e.g., Node.js + MongoDB + Redis) in the upcoming episode?
Top comments (0)