화. 8월 12th, 2025

D: 🚀 Introduction
Docker Compose is a powerful tool that simplifies the management of multi-container Docker applications. Whether you’re a developer, DevOps engineer, or just starting with containerization, understanding Docker Compose can significantly streamline your workflow. In this guide, we’ll break down its core principles, walk through practical examples, and show you how to deploy services efficiently.


🔍 What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications using a single configuration file (docker-compose.yml). Instead of manually running docker run for each container, you can define all services, networks, and volumes in one file and start them with a single command:

docker-compose up

Key Features

Simplified Configuration – Define services, networks, and volumes in YAML.
Single-Command Management – Start, stop, and rebuild services easily.
Environment Variables Support – Configure different environments (dev, prod).
Dependency Management – Control service startup order.


🧩 Core Concepts of Docker Compose

1️⃣ Services

A service is a containerized application defined in docker-compose.yml. Example:

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
  db:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: mysecretpassword

📌 Here, two services (web and db) are defined, running Nginx and PostgreSQL respectively.

2️⃣ Networks

By default, Compose creates a dedicated network for your services, allowing them to communicate.

services:
  app:
    networks:
      - my-network
networks:
  my-network:
    driver: bridge

3️⃣ Volumes

Persist data between container restarts:

services:
  db:
    volumes:
      - db_data:/var/lib/postgresql/data
volumes:
  db_data:

4️⃣ Environment Variables

Configure services dynamically:

services:
  app:
    environment:
      - DB_HOST=db
      - DB_USER=admin

🛠 Practical Example: Deploying a Flask + Redis App

Let’s deploy a simple Flask app with Redis using Docker Compose.

Step 1: Project Structure

flask-redis/  
├── app.py  
├── requirements.txt  
└── docker-compose.yml  

Step 2: app.py (Flask App)

from flask import Flask
import redis

app = Flask(__name__)
redis_client = redis.Redis(host='redis', port=6379)

@app.route('/')
def hello():
    redis_client.incr('hits')
    return f"Hello! This page has been viewed {redis_client.get('hits').decode()} times."

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Step 3: docker-compose.yml

version: '3.8'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - redis
  redis:
    image: redis:alpine

Step 4: Build & Run

docker-compose up --build

🌐 Open http://localhost:5000 to see the Flask app counting visits via Redis!


🚀 Deploying to Production

For production, consider:
✅ Using .env for environment variables.
✅ Setting resource limits (CPU, memory).
✅ Enabling restart policies.

Example:

services:
  web:
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
    restart: unless-stopped

🔥 Tips & Best Practices

🔹 Use docker-compose down – Cleans up containers, networks, and volumes.
🔹 Override configurations – Use docker-compose.override.yml for dev-specific settings.
🔹 Health Checks – Ensure services are running properly before dependencies start.


📌 Conclusion

Docker Compose is a game-changer for managing multi-container apps. From local development to production deployment, mastering it will make your container workflows faster and more reliable.

💡 Now it’s your turn! Try deploying your own multi-service app with Docker Compose.

🔗 Further Reading:

Happy Dockering! 🐳

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다