Montréal-Django
Django development using
December 1, 2015

Welcome
Daniel LeBlanc
CEO at ctrlweb
@danidou

https://ca.linkedin.com/in/dleblanc78
YulDev Organizer
Montréal-Django Organizer
Symfony Montréal Co-organizer
Laravel Montréal Co-organizer

daniel@ctrlweb.ca

Job Offers
Job Seekers

Tonight's Objectives
Learn the basic principles behind Docker
Learn a few neat tricks to build a working Django dev environment
And that's about it!
This is an
introductory
talk
Glossary
Docker Engine
Creates and runs Docker containers.
Runtime instance of a
Docker image.
Docker Container
Docker image
"Recipe" to build a container. Stateless and never changes. Shared via the Docker Hub.
Docker Hub
Hosted registry service for managing images. The "GitHub" of Docker images.
Base image
Parentless images. Docker images can "extend" other Docker images.
Docker Compose
Defines multi-container applications.
What is Docker?
Docker allows you to package an application with all of its dependencies into a standardized unit for software development.
- Docker website

Architecture
But why?

Hacking like a pro on your machine

thinking: "Docker is for n00bz!"
And then you deploy

on another machine...
WORKS ON MY MACHINE!
Here are a few good
reasons to use Docker:
- Gives you consistency between environments
- Speeds up environment setup & deployments
- Allows you to share your projects, environment included
- Allows you to build portable and scalable solutions
- Gives you environment versioning
- ... and much, much more!
Do you know what Vagrant is?
Nope
Yup
Do you use it to build Django apps?
Nope
Yup
(a side adventure!)

VS


Vagrant VMs
Docker containers
Vagrant VMs
Containers
Large
Small
Size (drive space)
Startup speed
Fast
Slow
Interaction
Very easy
Complicated
Memory footprint
Tiny
Large
Versioning
Each container
Whole VM
Vagrant is not bad!
It's just a different tool altogether!
It can be very useful!
http://tinyurl.com/docker-vs-vagrant
In fact...
From the author of Docker:
If you want to manage machines, you should use Vagrant. And if you want to build and run applications environments, you should use Docker.
All right, then. Let's move on!
Let's
Yup
A few basic commands
$ docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 2729 [OK]¶
[...]
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
2332d8973c93: Downloading [=================> ] 23.25 MB/65.67 MB
ea358092da77: Download complete
[...]
Search for an image in Docker Hub
Create a container from a Docker Image
$ docker run -it ubuntu /bin/bash
root@c2bb2bc1e0e8:/#
Run a command within a container
Docker (engine)
$ docker-compose build
db uses an image, skipping
Building app...
Step 0 : FROM python:3.3
---> d6f2c2d52f99
[...]
$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------
ctrlwebca_app_1 python3 manage.py runserve ... Exit 137
ctrlwebca_db_1 /docker-entrypoint.sh postgres Exit 0
Build an application's containers
List an application's containers
$ docker-compose up -d
Starting ctrlwebca_db_1...
Starting ctrlwebca_app_1...
Create and start an application's containers
Docker Compose
Recipe time!
Recipe #1
A micro PaaS server
autodock:
image: prologic/autodock
restart: always
ports:
- "1338:1338/udp"
- "1338:1338/tcp"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
autodocklogger:
image: prologic/autodock-logger
restart: always
links:
- autodock
autodockhipache:
image: prologic/autodock-hipache
restart: always
links:
- autodock
- hipache:redis
hipache:
image: tutum/hipache
restart: always
ports:
- 80:80
- 443:443
< Docker automation daemon
< Docker events logger
< Automatic Hipache VirtualHost
< Distributed HTTP & WS proxy
Step 1: Create a Docker Compose file
$ docker-compose up -d
Step 2: Run it
And that's it! You have a micro PaaS server running on your machine!
Recipe #2
A working Django application
Step 1: Create a Dockerfile
FROM python:3.3
ENV PYTHONBUFFERED 1
ENV APPLICATION_ROOT /code
RUN mkdir -p $APPLICATION_ROOT
WORKDIR $APPLICATION_ROOT
ADD requirements.txt $APPLICATION_ROOT/
RUN pip install -r requirements.txt
ADD . $APPLICATION_ROOT/
Dockerfile
Step 2: Create a requirements file
Django
psycopg2
requirements.txt
Step 3: Create a Docker Compose file
app:
build: .
expose:
- "80"
links:
- db
volumes:
- .:/code
command: python3 manage.py runserver 0.0.0.0:80
environment:
VIRTUALHOST: montreal-django.local
db:
image: postgres
docker-compose.yml
Step 4: Setup a Django project
$ docker-compose run app django-admin.py startproject tutorial .
Starting dockertutorial_db_1...
Step 5: Configure your DB
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
Step 6: Run it
$ docker-compose up -d
Starting dockertutorial_db_1...
Starting dockertutorial_app_1...
Also, just so you know...
You can use Docker to...
- run multiple applications on a single node
- run a single application on multiple clustered nodes (via Swarm)
tutum

currently in free beta
Merci!
Thank you!

Django development using Docker
By Daniel LeBlanc
Django development using Docker
Montréal-Django #2
- 735