After reading this blog post, you will be able to configure Celery with Django, PostgreSQL, Redis, and RabbitMQ, and then run everything in Docker containers.

Today, you'll learn how to set up a distributed task processing system for quick prototyping. You will configure Celery with Django, PostgreSQL, Redis, and RabbitMQ, and then run everything in Docker containers. You'll need some working knowledge of Docker for this tutorial, which you can get in one my previous posts here.

Django is a well-known Python web framework, and Celery is a distributed task queue. You'll use PostgreSQL as a regular database to store jobs, RabbitMQ as message broker, and Redis as a task storage backend.


When you build a web application, sooner or later you'll have to implement some kind of offline task processing.


Alice wants to convert her cat photos from .jpg to .png or create a .pdf from her collection of .jpg cat files. Doing either of these tasks in one HTTP request will take too long to execute and will unnecessarily burden the web server - meaning we can't serve other requests at the same time. The common solution is to execute the task in the background - often on another machine - and poll for the result.  

A simple setup for an offline task processing could look like this:

1. Alice uploads a picture.  
2. Web server schedules job on worker.  
3. Worker gets job and converts photo.  
4. Worker creates some result of the task (in this case, a converted photo).  
5. Web browser polls for the result.  
6. Web browser gets the result from the server.  

This setup looks clear, but it has a serious flaw - it doesn't scale well. What if Alice has a lot of cat pictures and one server wouldn't be enough to process them all at once? Or, if there was some other very big job and all other jobs would be blocked by it? Does she care if all of the images are processed at once? What if processing fails at some point?

Frankly, there is a solution that won't kill your machine every time you get a bigger selection of images. You need something between the web server and worker: a broker. The web server would schedule new tasks by communicating with the broker, and the broker would communicate with workers to actually execute these tasks. You probably also want to buffer your tasks, retry if they fail, and monitor how many of them were processed.

You would have to create queues for tasks with different priorities, or for those suitable for different kinds of workers.

All of this can be greatly simplified by using Celery - an open-source, distributed tasks queue. It works like a charm after you configure it - as long as you do so correctly.

How Celery is built

Celery consists of:

  • Tasks, as defined in your app
  • A broker that routes tasks to workers and queues
  • Workers doing the actual work
  • A storage backend

You can watch a more in-depth introduction to Celery here or jump straight to Celery's getting started guide.

Your setup

Start with the standard Django project structure. It can be created with django-admin, by running in shell:

$ django-admin startproject myproject

Which creates a project structure:

└── myproject
    └── myproject

At the end of this tutorial, it'll look like this:

├── Dockerfile
├── docker-compose.yml
├── myproject
│   ├──
│   └── myproject
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       ├──
│       └──
├── requirements.txt

Creating containers

Since we are working with Docker 1.12, we need a proper Dockerfile to specify how our image will be built.

Custom container


# use base python image with python 2.7
FROM python:2.7

# add requirements.txt to the image
ADD requirements.txt /app/requirements.txt

# set working directory to /app/

# install python dependencies
RUN pip install -r requirements.txt

# create unprivileged user
RUN adduser --disabled-password --gecos '' myuser  

Our dependencies are:



I've frozen versions of dependencies to make sure that you will have a working setup. If you wish, you can update any of them, but it's not guaranteed to work.

Choosing images for services

Now we only need to set up RabbitMQ, PostgreSQL, and Redis. Since Docker introduced its official library, I use its official images whenever possible. However, even these can be broken sometimes. When that happens, you'll have to use something else.

Here are images I tested and selected for this project:

Using docker-compose to set up a multicontainer app

Now you'll use docker-compose to combine your own containers with the ones we chose in the last section.


version: '2'

  # PostgreSQL database
    image: postgres:9.4
    hostname: db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
      - "5432:5432"

  # Redis
    image: redis:2.8.19
    hostname: redis

  # RabbitMQ
    hostname: rabbit
    image: rabbitmq:3.6.0
      - "5672:5672"  # we forward this port because it's useful for debugging
      - "15672:15672"  # here, we can access rabbitmq management plugin

  # Django web server
      context: .
      dockerfile: Dockerfile
    hostname: web
    command: ./
      - .:/app  # mount current directory inside container
      - "8000:8000"
    # set up links so that web knows about db, rabbit and redis
      - db
      - rabbit
      - redis
      - db

  # Celery worker
      context: .
      dockerfile: Dockerfile
    command: ./
      - .:/app
      - db
      - rabbit
      - redis
      - rabbit

Configuring the web server and worker

You've probably noticed that both the worker and web server run some starting scripts. Here they are (make sure they're executable):


# wait for PSQL server to start
sleep 10

cd myproject  
# prepare init migration
su -m myuser -c "python makemigrations myproject"  
# migrate db, so we have the latest db schema
su -m myuser -c "python migrate"  
# start development server on public ip interface, on port 8000
su -m myuser -c "python runserver"


# wait for RabbitMQ server to start
sleep 10

cd myproject  
# run Celery worker for our project myproject with Celery configuration stored in Celeryconf
su -m myuser -c "celery worker -A myproject.celeryconf -Q default -n [email protected]%h"  

The first script - - will migrate the database and start the Django development server on port 8000.
The second one ,, will start a Celery worker listening on a queue default.

At this stage, these scripts won't work as we'd like them to because we haven't yet configured them. Our app still doesn't know that we want to use PostgreSQL as the database, or where to find it (in a container somewhere). We also have to configure Redis and RabbitMQ.

But before we get to that, there are some useful Celery settings that will make your system perform better. Below are the complete settings of this Django app.


import os

from kombu import Exchange, Queue

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '[email protected]^+)it4e&ueu#!4tl9p1h%2sjr7ey0)m25f'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True  

# Application definition


    # required by Django 1.9



    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.AllowAny',),
    'PAGINATE_BY': 10

ROOT_URLCONF = 'myproject.urls'

WSGI_APPLICATION = 'myproject.wsgi.application'

# Localization ant timezone settings

USE_TZ = True


LANGUAGE_CODE = 'en-us'  
USE_I18N = True  
USE_L10N = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'

# Database Condocker-composeuration
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('DB_ENV_DB', 'postgres'),
        'USER': os.environ.get('DB_ENV_POSTGRES_USER', 'postgres'),
        'PASSWORD': os.environ.get('DB_ENV_POSTGRES_PASSWORD', 'postgres'),
        'HOST': os.environ.get('DB_PORT_5432_TCP_ADDR', 'db'),
        'PORT': os.environ.get('DB_PORT_5432_TCP_PORT', ''),

# Redis

REDIS_PORT = 6379  
REDIS_DB = 0  
REDIS_HOST = os.environ.get('REDIS_PORT_6379_TCP_ADDR', 'redis')

RABBIT_HOSTNAME = os.environ.get('RABBIT_PORT_5672_TCP', 'rabbit')

if RABBIT_HOSTNAME.startswith('tcp://'):  

BROKER_URL = os.environ.get('BROKER_URL',  
if not BROKER_URL:  
    BROKER_URL = 'amqp://{user}:{password}@{hostname}/{vhost}/'.format(
        user=os.environ.get('RABBIT_ENV_USER', 'admin'),
        password=os.environ.get('RABBIT_ENV_RABBITMQ_PASS', 'mypass'),
        vhost=os.environ.get('RABBIT_ENV_VHOST', ''))

# We don't want to have dead connections stored on rabbitmq, so we have to negotiate using heartbeats
BROKER_HEARTBEAT = '?heartbeat=30'  


# Celery configuration

# configure queues, currently we have only one
    Queue('default', Exchange('default'), routing_key='default'),

# Sensible settings for celery

# By default we will ignore result
# If you want to see results and try out tasks interactively, change it to False
# Or change this setting on tasks level

# Set redis as celery result backend

# Don't use pickle as serializer, json is much safer
CELERY_ACCEPT_CONTENT = ['application/json']


Those settings will configure the Django app so that it will discover the PostgreSQL database, Redis cache, and Celery.

Now, it's time to connect Celery to the app. Create a file and paste in this code:


import os

from celery import Celery  
from django.conf import settings

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

app = Celery('myproject')


app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)  

That should be enough to connect Celery to our app, so the run_X scripts will work. You can read more about first steps with Django and Celery here.

Defining tasks

Celery looks for tasks inside the file in each Django app. Usually, tasks are created either with a decorator, or by inheriting the Celery Task Class.

Here's how you can create a task using decorator:

def power(n):  
    """Return 2 to the n'th power"""
    return 2 ** n

And here's how you can create a task by inheriting after the Celery Task Class:

class PowerTask(app.Task):  
    def run(self, n):
    """Return 2 to the n'th power"""
        return 2 ** n

Both are fine and good for slightly different use cases.


from functools import wraps

from myproject.celeryconf import app  
from .models import Job

# decorator to avoid code duplication

def update_job(fn):  
    """Decorator that will update Job with result of the function"""

    # wraps will make the name and docstring of fn available for introspection
    def wrapper(job_id, *args, **kwargs):
        job = Job.objects.get(id=job_id)
        job.status = 'started'
            # execute the function fn
            result = fn(*args, **kwargs)
            job.result = result
            job.status = 'finished'
            job.result = None
            job.status = 'failed'
    return wrapper

# two simple numerical tasks that can be computationally intensive

def power(n):  
    """Return 2 to the n'th power"""
    return 2 ** n

def fib(n):  
    """Return the n'th Fibonacci number.
    if n < 0:
        raise ValueError("Fibonacci numbers are only defined for n >= 0.")
    return _fib(n)

def _fib(n):  
    if n == 0 or n == 1:
        return n
        return _fib(n - 1) + _fib(n - 2)

# mapping from names to tasks

    'power': power,
    'fibonacci': fib

Building an API for scheduling tasks

If you have tasks in your system, how do you run them? In this section, you'll create a user interface for job scheduling. In a backend application, the API will be your user interface. Let's use the Django REST Framework for your API.

To make it as simple as possible, your app will have one model and only one ViewSet (endpoint with many HTTP methods).

Create your model, called Job, in myproject/

from django.db import models

class Job(models.Model):  
    """Class describing a computational job"""

    # currently, available types of job are:
    TYPES = (
        ('fibonacci', 'fibonacci'),
        ('power', 'power'),

    # list of statuses that job can have
    STATUSES = (
        ('pending', 'pending'),
        ('started', 'started'),
        ('finished', 'finished'),
        ('failed', 'failed'),

    type = models.CharField(choices=TYPES, max_length=20)
    status = models.CharField(choices=STATUSES, max_length=20)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    argument = models.PositiveIntegerField()
    result = models.IntegerField(null=True)

    def save(self, *args, **kwargs):
        """Save model and if job is in pending state, schedule it"""
        super(Job, self).save(*args, **kwargs)
        if self.status == 'pending':
            from .tasks import TASK_MAPPING
            task = TASK_MAPPING[self.type]
            task.delay(, n=self.argument)

Then create a serializer, view, and URL configuration to access it.


from rest_framework import serializers

from .models import Job

class JobSerializer(serializers.HyperlinkedModelSerializer):  
    class Meta:
        model = Job


from rest_framework import mixins, viewsets

from .models import Job  
from .serializers import JobSerializer

class JobViewSet(mixins.CreateModelMixin,  
    API endpoint that allows jobs to be viewed or created.
    queryset = Job.objects.all()
    serializer_class = JobSerializer


from django.conf.urls import url, include  
from rest_framework import routers

from myproject import views

router = routers.DefaultRouter()  
# register job endpoint in the router
router.register(r'jobs', views.JobViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [  
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))

For completeness, there is also myproject/, defining WSGI config for the project:

import os  
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

from django.core.wsgi import get_wsgi_application  
application = get_wsgi_application()  


#!/usr/bin/env python
import os  
import sys

if __name__ == "__main__":  
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

    from import execute_from_command_line


Leave empty.

That's all. Uh... lots of code. Luckily, everything is on GitHub, so you can just fork it.

Running the setup

Since everything is run from Docker Compose, make sure you have both Docker and Docker Compose installed before you try to start the app:

$ cd /path/to/myproject/where/is/docker-compose.yml
$ docker-compose build
$ docker-compose up

The last command will start five different containers, so just start using your API and have some fun with Celery in the meantime.

Accessing the API

Navigate in your browser to to browse your API and schedule some jobs.

Scale things out

Currently, we have only one instance of each container. We can get information about our group of containers with the docker-compose ps command.

$ docker-compose ps
           Name                          Command               State                                        Ports                                      
dockerdjangocelery_db_1       / postgres   Up>5432/tcp  
dockerdjangocelery_rabbit_1   / rabb ...   Up>15672/tcp, 25672/tcp, 4369/tcp, 5671/tcp,>5672/tcp  
dockerdjangocelery_redis_1    / redis-server      Up      6379/tcp  
dockerdjangocelery_web_1      ./                     Up>8000/tcp  
dockerdjangocelery_worker_1   ./                  Up  

Scaling out a container with docker-compose is extremely easy. Just use the docker-compose scale command with the container name and amount:

$ docker-compose scale worker=5
Creating and starting dockerdjangocelery_worker_2 ... done  
Creating and starting dockerdjangocelery_worker_3 ... done  
Creating and starting dockerdjangocelery_worker_4 ... done  
Creating and starting dockerdjangocelery_worker_5 ... done  

Output says that docker-compose just created an additional four worker containers for us. We can double-check it with the docker-compose ps command again:

$ docker-compose ps
           Name                          Command               State                                        Ports                                      
dockerdjangocelery_db_1       / postgres   Up>5432/tcp  
dockerdjangocelery_rabbit_1   / rabb ...   Up>15672/tcp, 25672/tcp, 4369/tcp, 5671/tcp,>5672/tcp  
dockerdjangocelery_redis_1    / redis-server      Up      6379/tcp  
dockerdjangocelery_web_1      ./                     Up>8000/tcp  
dockerdjangocelery_worker_1   ./                  Up  
dockerdjangocelery_worker_2   ./                  Up  
dockerdjangocelery_worker_3   ./                  Up  
dockerdjangocelery_worker_4   ./                  Up  
dockerdjangocelery_worker_5   ./                  Up  

You'll see there five powerful Celery workers. Nice!


Congrats! You just married Django with Celery to build a distributed asynchronous computation system. I think you'll agree it was pretty easy to build an API, and even easier to scale workers for it! However, life isn't always so nice to us, and sometimes we have to troubleshoot.


Original article written by Justyna Ilczuk, updated by Michał Kobus.