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
800
An educational retro game using only Python? Challenge Accepted!
siloraptor
0
140
Packaging Django Projects for PyPI
siloraptor
1
530
Liberation and modernization of legacy government data using Django
siloraptor
0
280
Other Decks in Programming
See All in Programming
ソフトウェアエンジニアの成長
masuda220
PRO
10
1.1k
Rails アプリ地図考 Flush Cut
makicamel
1
120
SwiftUI Viewの責務分離
elmetal
PRO
1
240
sappoRo.R #12 初心者セッション
kosugitti
0
250
動作確認やテストで漏れがちな観点3選
starfish719
6
1k
Pulsar2 を雰囲気で使ってみよう
anoken
0
240
GoとPHPのインターフェイスの違い
shimabox
2
190
Amazon Bedrock Multi Agentsを試してきた
tm2
1
290
2024年のWebフロントエンドのふりかえりと2025年
sakito
3
250
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
910
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
110
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
150
Featured
See All Featured
The Cult of Friendly URLs
andyhume
78
6.2k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Unsuck your backbone
ammeep
669
57k
Gamification - CAS2011
davidbonilla
80
5.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1k
Bash Introduction
62gerente
611
210k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
A Tale of Four Properties
chriscoyier
158
23k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
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!