Let's do some docker

Intro

From DjangoCon

Nice talk from DjangoCon.

Docker doc

In case you are lost, or you are tuning your Dockerfile, take a look here, you should find the answer.

Docker index

Here is where we can find most of the 'official' images: docker index

A django app

This demo aims to embed the following django celery demo in a hand made container.

Install

It's simple

Very easy to install:

~$ apt-get install docker

It's light

Few dependencies:

~$ apt-cache depends docker
docker
Depends: libc6
Depends: libglib2.0-0
Depends: libx11-6

First Image

List

List available images:

~$ docker images

Retrieve

Get the the most popular debian image:

~$ docker pull debian
Pulling repository debian
e565fbbc6033: Download complete
511136ea3c5a: Download complete
405cce5cd17d: Download complete

Should have something now:

~$ docker images
REPOSITORY   TAG       IMAGE ID        CREATED         VIRTUAL SIZE
debian       latest    e565fbbc6033    4 weeks ago     115 MB
...

Note: it pulls the latest image tag by default.

Run a command

Echo hi

Let's echo something:

~$ docker run debian echo "hi"

Do it again

Even simple as it is, it's already reproducible:

~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND   CREATED         STATUS                    PORTS    NAMES
8ec4815f3ccd   debian:latest  echo hi   51 seconds ago  Exited (0) 49 seconds ago          silly_hypatia

~$ docker start -i 8ec4815f3ccd
8ec4815f3ccd
hi

Clean

Remove that junk container:

~$ docker rm 8ec4815f3ccd

Start interactive container

Enter in the container

Run a debian container:

~$ docker run --name deby -it debian /bin/bash

Do your stuff:

root@deby:/# echo 'hi'
hi

Get postgres

Pull:

~$ docker pull paintedfox/postgresql:latest

Run:

~$ docker run -d \
--name="postgresql" \
-h "db.local" \
-e USER="docker" \
-e DB="docker" \
-e PASS="docker" \
paintedfox/postgresql

Get rabbitmq

Pull:

~$ docker pull tutum/rabbitmq:latest

Run:

~$ docker run -d \
--name="rabbitmq" \
-e RABBITMQ_PASS="pass" \
-h "amqp.local" \
tutum/rabbitmq

Make my django image

The Dockerfile

~$ echo "
FROM debian:latest
MAINTAINER Florent Pigout "florent@toopy.org"

RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y git python2.7 python-pip python-psycopg2
RUN pip install django django-celery

RUN git clone https://github.com/celery/celery.git /root/celery
RUN cp -rf /root/celery/examples/django /root/celery-example-django

ADD settings_local.py /root/celery-example-django

ADD run.sh /root
RUN chmod +x /root/run.sh

CMD /root/run.sh
" > Dockerfile

A run.sh script

~$ vim run.sh
#!/bin/bash
export DJANGO_SETTINGS_MODULE=settings_local

echo "[run] go to example folder"
cd /root/celery-example-django

echo "[run] syncdb"
python manage.py syncdb --noinput

echo "[run] create superuser"
echo "from django.contrib.auth.models import User
if not User.objects.filter(username='admin').count():
    User.objects.create_superuser('admin', 'admin@example.com', 'pass')
" | python manage.py shell

echo "[run] runserver"
python manage.py runserver 0.0.0.0:8000

Some settings

~$ vim settings_local.py
import os
from proj.settings import *

BROKER_URL = 'amqp://admin:pass@{0}//'.format(os.environ['RABBITMQ_PORT_5672_TCP_ADDR'])

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'docker',
        'USER': 'docker',
        'PASSWORD': 'docker',
        'HOST': os.environ['POSTGRESQL_PORT_5432_TCP_ADDR'],
        'PORT': '',
    }
}

Image Building

Build my image

~$ docker build -t django .

It's ready

~$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
django                     latest              1549cbf94b6e        26 minutes ago      463.6 MB
...

Run it

~$ docker run -it \
--name "django" \
--link postgresql:postgresql \
--link rabbitmq:rabbitmq \
-h "django.local" \
django

Push it

Next time ;)

Comments !