Compose를 활용한 다중 컨테이너 앱
Docker Compose를 사용하여 웹 앱과 데이터베이스 같은 여러 Docker 컨테이너를 로컬 개발 환경에서 조정합니다.
Compose를 활용한 다중 컨테이너 앱은(는) CoddyKit의 무료 Docker & Kubernetes for Developers 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Docker & Kubernetes for Developers 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Docker & Kubernetes for Developers 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Multi-Container Apps?
In real-world applications, you rarely have just one component. Imagine a web application: you need a web server (like Nginx), a backend application (like Python Flask), and a database (like Redis or PostgreSQL).
Each of these components has its own dependencies and configuration. Managing them individually and ensuring they communicate correctly can quickly become complex.
Meet Docker Compose
Docker Compose is a tool designed to simplify the process of defining and running multi-container Docker applications.
- You define your entire application's services (containers) in a single YAML file.
- Then, with one simple command, Compose creates and starts all the services for you.
- It's incredibly useful for local development and testing environments.
The Compose YAML File
The heart of Docker Compose is the docker-compose.yml file. This YAML file describes your application's services, networks, and volumes.
It typically starts with a version key, followed by the main services section where you define each container that makes up your application.
Defining Services
Under the services section, you list each component of your application. Each service corresponds to a Docker container.
Key properties for defining a service include:
image: Specifies the Docker image to use (e.g.,nginx:latest).build: Provides the path to a directory containing aDockerfileto build a custom image.container_name: An optional, fixed name for your container, making it easier to reference.
Exposing & Configuring Services
You often need to expose your services to the host machine or configure them with environment variables.
ports: Maps ports from the host to the container (e.g.,"8080:80"maps host port 8080 to container port 80).environment: Passes environment variables into the container (e.g.,DB_HOST: "database").
These settings are crucial for communication and customization.
Connecting Your Services
When you use Docker Compose, it automatically creates a default network for your application. All services defined in your docker-compose.yml file join this network.
This allows services to communicate with each other using their service names as hostnames. For example, a web app service can connect to a database service named redis by simply using redis as the hostname.
Example: Python + Redis
Let's build a simple multi-container application: a Python Flask web app that counts how many times it has been visited, storing the count in a Redis database.
This setup clearly shows how a 'web' service can interact with a 'database' service within a single Compose application.
Web App Code & Dockerfile
Here's our simple Python Flask application (app.py) and its Dockerfile. The Flask app connects to the redis service, and the Dockerfile packages it all up.
# app.py
import time
import redis
from flask import Flask
app = Flask(__name__)
# Connects to 'redis' service in Compose network
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello from CoddyKit! I have been seen {} times.\n'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
# requirements.txt
# flask
# redis
# Dockerfile
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
Your First Compose File
This docker-compose.yml ties everything together. It defines two services: web (our Python app, built from the current directory) and redis (using the official Redis image).
Try running this example!
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"Managing Your Compose App
To start your multi-container application in detached mode (in the background), navigate to the directory containing your docker-compose.yml and supporting files, then run:
docker compose up -dTo see the running services and their status:
docker compose psTo stop and remove the containers, networks, and volumes created by Compose:
docker compose downCompose Configuration Check
Consider the following docker-compose.yml snippet:
version: '3.8'
services:
app:
build: .
ports:
- "8080:80"
database:
image: "postgres:13"Which statement is TRUE about this configuration?
Recap: Docker Compose
You've learned how Docker Compose simplifies managing multi-container applications for local development!
- It uses a
docker-compose.ymlfile to define all your services. - Services can communicate with each other using their service names on a default network.
- Commands like
docker compose upanddocker compose downmanage your entire application stack with ease.
Docker Compose is an essential tool for local development setups involving multiple Docker containers.
자주 묻는 질문
“Compose를 활용한 다중 컨테이너 앱” 강의는 무료인가요?
네 — “Compose를 활용한 다중 컨테이너 앱” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Docker & Kubernetes for Developers 강의 전체를 잠금 해제할 수 있습니다. Docker & Kubernetes for Developers 강의에는 총 4개의 강의가 포함되어 있습니다.
“Compose를 활용한 다중 컨테이너 앱”에서 뭘 배우나요?
Docker Compose를 사용하여 웹 앱과 데이터베이스 같은 여러 Docker 컨테이너를 로컬 개발 환경에서 조정합니다. 브라우저에서 직접 실행하는 실습 코드로 Docker & Kubernetes for Developers을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Docker & Kubernetes for Developers을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Docker & Kubernetes for Developers은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“Compose를 활용한 다중 컨테이너 앱” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Docker & Kubernetes for Developers 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Docker & Kubernetes for Developers 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 이미지용 Dockerfile 작성
- 사용자 지정 Docker 이미지 빌드
- Compose를 활용한 다중 컨테이너 앱
- Docker 볼륨으로 데이터 영속화하기