Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Dockerizing Django projects
Search
Roberto Rosario
April 15, 2016
Programming
0
220
Dockerizing Django projects
Pros and Cons of the different ways there are to turn a Django project into a Docker container.
Roberto Rosario
April 15, 2016
Tweet
Share
More Decks by Roberto Rosario
See All by Roberto Rosario
Geospatial three amigos: Python, Leaflet, and ElasticSearch
siloraptor
1
460
OpenHolter project: D.I.Y. Electrocardiography using Arduino and Django
siloraptor
0
1.3k
Django tasty salad- DOs and DON'Ts using Celery
siloraptor
0
760
An educational retro game using only Python? Challenge Accepted!
siloraptor
0
130
Packaging Django Projects for PyPI
siloraptor
1
490
Liberation and modernization of legacy government data using Django
siloraptor
0
270
Other Decks in Programming
See All in Programming
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
270
Beyond ORM
77web
3
460
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
1
150
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
CSC509 Lecture 14
javiergs
PRO
0
140
php-conference-japan-2024
tasuku43
0
230
Jakarta EE meets AI
ivargrimstad
0
240
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Writing Fast Ruby
sferik
628
61k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
A Tale of Four Properties
chriscoyier
157
23k
Rails Girls Zürich Keynote
gr2m
94
13k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Building Applications with DynamoDB
mza
91
6.1k
The Cult of Friendly URLs
andyhume
78
6.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Fireside Chat
paigeccino
34
3.1k
Transcript
Dockerizing Django projects Roberto Rosario robertorosario.com
Who am I? robertorosario.com
[email protected]
Roberto Rosario
None
What is Docker?
Docker provides an integrated technology suite that enables development and
IT operations teams to build, ship, and run distributed applications anywhere.
In human now.
Containers.
Lightweight virtualization. ie: No video hardware emulation.
Some devops now hate me.
Deploy everywhere.
Easy scalability.
Even auto scaling.
Deploying Django: Old school.
None
None
None
None
None
None
Deployed! Congratulations!
Your users have now quadrupled!
None
None
Server are old, let’s go virtual!
None
None
None
Different technology, same problem.
None
None
None
None
Django and Docker?
Configuration files.
First steps.
None
None
Classes Instances
More devops hate me now.
None
Dockerfile
An entire OS for just a part of the Django
project?
Not to worry.
None
None
Your Django project
Your Django project
Your Django project image Your Django project
Base image = Base class Your image = Subclass
Pretty much every devops hate me at this point.
One size doesn’t fit all.
Monolithic
App Workers Web server
docker run --name postgres -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e POSTGRES_PASSWORD=mysecretpassword
-v /var/lib/postgresql/data -d postgres docker run --name redis -d redis docker run --name mayan-edms -p 80:80 --link postgres:postgres --link redis:redis -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e POSTGRES_PASSWORD=mysecretpassword -v /usr/local/lib/python2.7/dist- packages/mayan/media -d mayanedms/monolithic
• Single download • Low entry barrier • Easiest to
maintain • Big size download • Not good for scaling • Same VM/Server problem
Split
App Workers Web server
Split ~ Frontend + Backend
• Scale individually • Smaller download • Best approach for
production • Harder to setup • Need documentation • Several DockerFiles
Mixed
Role Frontend Backend
None
• Single download • Can reuse configuration file templates •
Single core repo • Bigger size download
Lean
Lean = Split without any 3rd party
App Workers Web server
• Smallest download • Hardest to configure • Might not
scale best • Might violate port export concept
Django + uWSGI NGINX
Django + uWSGI NGINX TCP Port uwsgi_pass 127.0.0.1:3031; socket =
127.0.0.1:3031
Django + uWSGI NGINX socket = /tmp/uwsgi.sock uwsgi_pass unix:///tmp/uwsgi.sock; ?
Django + uWSGI NGINX socket = /tmp/uwsgi.sock uwsgi_pass unix:///tmp/uwsgi.sock; Volume?
• Need to include NGINX config
$ docker run --name some-nginx -v /some/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
environment: - NGINX_HOST=foobar.com - NGINX_PORT=80 command: /bin/bash -c "envsubst <
/etc/nginx/conf.d/mysite.template > /etc/nginx/conf. d/default.conf && nginx -g 'daemon off;'"
Last approach
Kitchen sink
App Workers Database Web server Broker
• Single download • Lowest entry barrier • Biggest size
download • Terrible for scaling • Same VM/Server problem • Support 3rd party software
None
Educational purposes only
Don’t use in production
EVER!
Kitchen sink will freeze your servers
Devops don’t like frozen servers
None
Don’t turn your devops into Gordon Ramsay
Post init actions
migrate & collectstatic
None
None
None
None
Service synchronization
$ docker run --name postgres -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e
POSTGRES_PASSWORD=mysecretpassword -v /var/lib/postgresql/data -d postgres $ docker run --name redis -d redis $ docker run --name mayan-edms -p 80:80 --link postgres:postgres --link redis:redis -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e POSTGRES_PASSWORD=mysecretpassword -v /usr/local/lib/python2.7/dist- packages/mayan/media -d mayanedms/monolithic
Database connection error.
None
$ docker run --name postgres -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e
POSTGRES_PASSWORD=mysecretpassword -v /var/lib/postgresql/data -d postgres $ docker run --name redis -d redis $ docker run --name mayan-edms -p 80:80 --link postgres:postgres --link redis:redis -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e POSTGRES_PASSWORD=mysecretpassword -v /usr/local/lib/python2.7/dist- packages/mayan/media -d mayanedms/monolithic
$ docker run --name postgres -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e
POSTGRES_PASSWORD=mysecretpassword -v /var/lib/postgresql/data -d postgres $ docker run --name redis -d redis $ docker run --name mayan-edms -p 80:80 --link postgres:postgres --link redis:redis -e POSTGRES_DB=mayan -e POSTGRES_USER=mayan -e POSTGRES_PASSWORD=mysecretpassword -v /usr/local/lib/python2.7/dist- packages/mayan/media -d mayanedms/monolithic until nc -z $POSTGRES_PORT_5432_TCP_ADDR $POSTGRES_PORT_5432_TCP_PORT; do echo "$(date) - waiting for postgres..." sleep 1 done
Service synchronization doesn’t belong in configuration files.
until nc -z $POSTGRES_PORT_5432_TCP_ADDR $POSTGRES_PORT_5432_TCP_PORT; do echo "$(date) - waiting
for postgres..." sleep 1 done until nc -z $RABBITMQ_PORT_5672_TCP_ADDR $RABBITMQ_PORT_5672_TCP_PORT; do echo "$(date) - waiting for rabbitmq..." sleep 1 done
More problems.
• Not foolproof • Mute services • nc is not
universal • API URL paths?
Solutions?
None
$ docker run -d --name mycontainer some-image-or-other $ docker run
--link mycontainer:mycontainer aanand/wait waiting for TCP connection to 172.17.0.105:5432......ok
Interesting, but...
None
An entire container to run nc.
Cool solution, wrong problem.
None
None
It’s the container manager’s responsibility.
None
None
Kubernetes approach
None
Port, path, initial delay & timeout!
None
None
Docker upstream native approach needed
None
None
Templating
Docker images are static. Less reusable.
None
Docker containers are configured with environment variables.
None
uWSGI support environment variables. NGINX doesn’t.
Docker upstream native approach needed
None
None
“Thank you for using Docker” “But your fix is in
another repository”
None
Options?
None
And then there is genius
None
None
Bonus
None
Can help with logs too
None
Demo time
Lessons learned?
None
None
None
None
None
Thank you!