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
230
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
790
An educational retro game using only Python? Challenge Accepted!
siloraptor
0
140
Packaging Django Projects for PyPI
siloraptor
1
500
Liberation and modernization of legacy government data using Django
siloraptor
0
280
Other Decks in Programming
See All in Programming
Rubyでつくるパケットキャプチャツール
ydah
0
170
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
ゼロからの、レトロゲームエンジンの作り方
tokujiros
3
1k
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
390
歴史と現在から考えるスケーラブルなソフトウェア開発のプラクティス
i10416
0
300
ISUCON14感想戦で85万点まで頑張ってみた
ponyo877
1
590
『改訂新版 良いコード/悪いコードで学ぶ設計入門』活用方法−爆速でスキルアップする!効果的な学習アプローチ / effective-learning-of-good-code
minodriven
28
4.1k
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
170
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
1.3k
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
26
6k
Swiftコンパイラ超入門+async関数の仕組み
shiz
0
170
return文におけるstd::moveについて
onihusube
1
1.4k
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
240
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Faster Mobile Websites
deanohume
305
30k
Embracing the Ebb and Flow
colly
84
4.5k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
GitHub's CSS Performance
jonrohan
1030
460k
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!