Post

Docker Compose로 Celery + Django + Redis 환경 구성하기

Celery, Django, Redis를 통합한 비동기 백엔드 환경을 Docker Compose로 로컬에 구축하는 과정을 단계별로 정리합니다.

Docker Compose로 Celery + Django + Redis 환경 구성하기

Celery는 Django와 Redis와 함께 자주 사용되는 비동기 처리 도구입니다. 이 글에서는 Celery + Django + Redis 조합을 Docker Compose로 통합 실행하는 방법을 정리합니다.

프로젝트 구조

1
2
3
4
5
6
7
8
9
10
11
12
myproject/
├── docker-compose.yml
├── Dockerfile
├── config/
│   ├── celery.py
│   └── __init__.py
├── django_app/
│   ├── __init__.py
│   ├── tasks.py
│   └── views.py
├── requirements.txt
└── beat_schedule.py  # Celery Beat 스케줄 정의

실습에 필요한 구성 요소를 최대한 간단하게 설정했습니다.

1. requirements.txt

1
2
3
Django==4.2
celery==5.3.1
redis==5.0.1

2. Django 설정: celery.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# config/celery.py
import os
from celery import Celery
from celery.schedules import crontab

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

app = Celery("config")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()

# 스케줄 설정 (코드 내 직접 정의 or beat_schedule.py 불러오기)
app.conf.beat_schedule = {
    "print-hello-every-10s": {
        "task": "django_app.tasks.print_hello",
        "schedule": 10.0,  # 초 단위 실행
    },
}
1
2
3
# django_app/__init__.py
from config.celery import app as celery_app
__all__ = ("celery_app",)
1
2
3
# config/settings.py
CELERY_BROKER_URL = "redis://redis:6379/0"
CELERY_RESULT_BACKEND = "redis://redis:6379/0"

3. 예제 Task

1
2
3
4
5
6
7
8
9
10
# django_app/tasks.py
from celery import shared_task

@shared_task
def add(x, y):
    return x + y

@shared_task
def print_hello():
    print("[BEAT TASK] Hello from Celery Beat!")

4. docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: '3.8'
services:
  django:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - redis

  redis:
    image: redis:7
    ports:
      - "6379:6379"

  celery:
    build: .
    command: celery -A config worker --loglevel=info
    volumes:
      - .:/code
    depends_on:
      - redis
      - django

  beat:
    build: .
    command: celery -A config beat --loglevel=info
    volumes:
      - .:/code
    depends_on:
      - redis

  flower:
    image: mher/flower
    command: celery --broker=redis://redis:6379/0 flower
    ports:
      - "5555:5555"
    depends_on:
      - redis

5. Dockerfile

1
2
3
4
5
6
7
8
FROM python:3.10-slim

WORKDIR /code

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

6. 실행 및 테스트

1
docker-compose up --build
  • localhost:8000 → Django 앱 실행 확인
  • localhost:5555 → Flower UI로 작업 모니터링 가능
  • 콘솔에서 10초마다 “Hello from Celery Beat!” 출력 확인
1
2
3
# 추가 테스트
from django_app.tasks import add
add.delay(3, 7)  # 결과는 10

마무리

Docker Compose를 사용하면 Celery + Redis + Django + Beat 조합을 간단히 통합 실행할 수 있습니다. 개발과 테스트 환경을 빠르게 재현할 수 있고, 협업 시에도 일관된 설정을 유지할 수 있다는 점이 큰 장점입니다.

This post is licensed under CC BY 4.0 by the author.