Post on 19-Jul-2015
Tamer M Abdul-Radi @tabdulradi Eslam ElHusseiny @EslamElHusseiny https://github.com/kiosk12
Agenda
• Pain - What/Why Docker
• Developing and deploying a Django App
• How Docker works
–Every developer
“But it works on my machine.”
Django ? ? ? ? ?
PostgreSQL ? ? ? ? ?
Redis ? ? ? ? ?
Hadoop ? ? ? ? ?
Message Queue ? ? ? ? ?
Developer Laptop
QA Laptop
Staging Server
Production Server
Cloud
Matrix from Hell
(Pain) = n * m
• Every developer wastes almost a day to configure his machine
• What about tester’s machine?
• Hot patches during release nights!
n = size of team + staging + production m = size of project
Matrix from Hell!Django ? ? ? ?
PostgreSQL ? ? ? ? ?
Redis ? ? ? ? ?
Big Data ? ? ? ? ?
Message Queue ? ? ? ? ?
Developer Laptop
QA Laptop
Staging Server
Prod. on Server
Prod. on Cloud
Code time!Let’s get our hands dirty
Installation$ sudo apt-get install docker.io$ sudo pip install -U fig
For OSX and Windows: check “boot2docker”
Create DockerfileFROM python:2.7 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt ADD . /code/
Create requirements.txt
django psycopg2
Create fig.ymldb:
image: postgres web:
build: . command: python manage.py runserverv 0.0.0.0:8000 volumes:
- ./code ports:
- “8000:8000” links:
- db
Start Django project
$ fig run web django-admin.py startproject example .$ lsDockerfile example fig.yml manage.py requirements.txt$ ls example__init__.py settings.py urls.py wsgi.py
Configure Django AppDATABASES = {
‘default’: { ‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’, ‘NAME’: ‘postgres’, ‘USER’: ‘postgres’, ‘HOST’: ‘db’, ‘PORT’: 5432
} }
Run the app
$ fig up
In your browser
Access containers
$ fig run web ping db$ fig run web cat /etc/hosts
Polls App• Lets’s steal the polls app from official django tutorial
• https://docs.djangoproject.com/en/1.7/intro/tutorial01/
• Don’t forget to add polls to installed apps
$ fig run web python manage.py startapp polls
polls/models.pyfrom django.db import models
class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')
class Choice(models.Model): question = models.ForeignKey(Question) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
polls/admin.py
from django.contrib import adminfrom polls.models import Question
admin.site.register(Question)
Polls App
$ fig run web python manage.py makemigrations polls$ fig run web python manage.py sqlmigrate polls 0001$ fig run web python manage.py sqlmigrate polls 0001
Running the app
#!/bin/sh
python manage.py migrate python manage.py runserver 0.0.0.0:8000
• Running the app became a bit more complex • We need to make sure that models are migrated • create run_web.sh
Update fig.ymldb:
image: postgres web:
build: . command: ./run_web.sh volumes:
- ./code ports:
- “8000:8000” links:
- db
Create admin account
$ fig run web python manage.py createsuperuser
In your browser
Deploy time!
Fig for productiondb:
image: postgres web:
build: . command: ./run_web.shvolumes:
- ./code ports:
- “80:8000” links:
- db
Create Amazon Machine
aws ec2 run-instances —image-id ami-234ecc54 —security-groups default —instance-type c4.large
—key-name kiosk12
Configure Amazon Machine
• sudo apt-get install docker.io
• sudo pip install -U fig
Run
• -d = Daemon mode
• -f = Specify fig file
$ fig up -d -f fig-production.yml
Docker Internals• Images
• Read-only templates
• Clone of the system with installed apps + code
• Containers
• Running instance of an image
• Isolated file system and ports
• Registeres
• Host for Images
• Dockerhub,com
Containers vs VM
Architecture
New Docker Features
Docker Machine
$ docker-machine create -d virtualbox dev$ docker-machine create -d digitalocean --
digitalocean-access-token=... staging$ docker-machine lsNAME ACTIVE DRIVER STATE URL dev virtualbox Running tcp://192.168.99.108:2376 staging * digitalocean Running tcp://104.236.37.134:2376
Docker Swarm
• Clustering for Dockerized distributed apps
• Selects a docker demon for your containers (created by Docker machine)
Docker Compose
• Docker bought fig!
• No need to install two tools in future
• Almost same yaml syntax
Fun with Docker• Explore dockerized apps at dockerhub.com with no
installation hassle!
• docker run mongo
• docker run --rm -v "$PWD" -p 4000:4000 grahamc/jekyll
• docker run -it --rm williamyeh/scala
• Some cool aliases here: http://github.com/tabdulradi/docker-fish-functions
Tamer M Abdul-Radi Software Engineer
twitter.com/tabdulradi github.com/tabdulradi
abdulradi.com
Thanks
Eslam ElHusseiny Systems Engineer twitter.com/EslamElHusseinygithub.com/EslamElHusseiny eslamelhusseiny.wordpress.com
http://github.com/Kiosk12/docker-django-demo