Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Docker Lessons from Real-World Projects

Docker Lessons from Real-World Projects

Presented at DjangoCon Europe 2017

Explores how to use Docker for complex, real-world Django applications.

Rivo Laks

April 05, 2017
Tweet

More Decks by Rivo Laks

Other Decks in Technology

Transcript

  1. The Problem At Thorgate, we host dozens of our projects

    A single server runs multiple projects
  2. The Problem At Thorgate, we host dozens of our projects

    A single server runs multiple projects Dependency isolation became an issue
  3. How to Get There? Lots of online tutorials for making

    rst steps Practical experience with bigger projects is much more scarce
  4. The Django Service Start with o cial python image Install

    Python requirements Add application source code
  5. The Django Service Start with o cial python image Install

    Python requirements Add application source code Use Gunicorn to serve HTTP
  6. The Django Service Start with o cial python image Install

    Python requirements Add application source code Use Gunicorn to serve HTTP Volumes are used for data dirs This includes static assets, media and logs
  7. The Node Service Bundles JS + CSS using Webpack Isn't

    run continuously, just once during deploy
  8. The Node Service Bundles JS + CSS using Webpack Isn't

    run continuously, just once during deploy Basically o cial Node image + our requirements
  9. The Node Service Bundles JS + CSS using Webpack Isn't

    run continuously, just once during deploy Basically o cial Node image + our requirements Volumes for JS/CSS source dirs
  10. Other Services Redis, Celery, etc Can often use o cial

    image out-of-the-box Volumes for persistent data
  11. Putting it All Together Our application has multiple services working

    in unison, communicating via network Docker Compose is “a tool for de ning and running multi-container Docker applications”
  12. Putting it All Together Our application has multiple services working

    in unison, communicating via network Docker Compose is “a tool for de ning and running multi-container Docker applications” de ne & control multiple containers at once
  13. Putting it All Together Our application has multiple services working

    in unison, communicating via network Docker Compose is “a tool for de ning and running multi-container Docker applications” de ne & control multiple containers at once easier networking among single app's services
  14. The Servers Almost nothing but Docker Postgres and Nginx as

    well, since we chose to leave those global
  15. Deploys docker-compose build docker-compose run --rm node npm run build

    docker-compose run --rm django ./manage.py collectstatic docker-compose run --rm django ./manage.py migrate
  16. Deploys docker-compose build docker-compose run --rm node npm run build

    docker-compose run --rm django ./manage.py collectstatic docker-compose run --rm django ./manage.py migrate docker-compose up -d
  17. Dev vs Production Should ideally be very similar, but some

    di erences are useful Commands are slightly di erent
  18. Dev vs Production Should ideally be very similar, but some

    di erences are useful Commands are slightly di erent Sources not baked into images
  19. Dev vs Production Should ideally be very similar, but some

    di erences are useful Commands are slightly di erent Sources not baked into images Postgres is run locally, along other services
  20. Dev vs Production Should ideally be very similar, but some

    di erences are useful Commands are slightly di erent Sources not baked into images Postgres is run locally, along other services Separate Docker Compose con g le
  21. Django App dir is mounted as volume, not baked into

    image Python dependencies still part of the image
  22. Django App dir is mounted as volume, not baked into

    image Python dependencies still part of the image Django's runserver instead of Gunicorn
  23. Django App dir is mounted as volume, not baked into

    image Python dependencies still part of the image Django's runserver instead of Gunicorn Separate Dockerfile
  24. Node Image contains libraries but not our sources (same as

    production) Webpack watch mode, similar to Django's runserver
  25. Commands docker-compose run --rm django ./manage.py runserver docker-compose run --rm

    django ./manage.py migrate Make le FTW! make docker make migrate
  26. Commands docker-compose run --rm django ./manage.py runserver docker-compose run --rm

    django ./manage.py migrate Make le FTW! make docker make migrate Bonus: make setup
  27. Service Discovery How to route HTTP requests to correct Django

    container Ran Nginx in container, used Docker's built-in routing
  28. Service Discovery How to route HTTP requests to correct Django

    container Ran Nginx in container, used Docker's built-in routing But container IP can change, Nginx does DNS lookup only once
  29. Service Discovery How to route HTTP requests to correct Django

    container Ran Nginx in container, used Docker's built-in routing But container IP can change, Nginx does DNS lookup only once Created a service monitoring Docker events and reloading Nginx
  30. Waiting for Related Services Need to wait for Postgres to

    be available for Django wait-for-it.sh scripts
  31. Performance on non-Linux OSs On macOS / Windows, Docker containers

    run in VM Filesystem performance su ers localhost isn't localhost anymore
  32. PyCharm Supports Python interpreter in Docker There are bugs though

    Running app inside PyCharm was tricky node_modules isn't visible to PyCharm
  33. Problems Learning curve – you need to learn Docker You'll

    also spend time on interesting new issues
  34. Problems Learning curve – you need to learn Docker You'll

    also spend time on interesting new issues Ongoing problems: Tooling
  35. Problems Learning curve – you need to learn Docker You'll

    also spend time on interesting new issues Ongoing problems: Tooling Performance outside Linux
  36. Advantages Isolation & reproducability Easier to add new services Bundling

    everything into a single entity Limiting/monitoring resource use is easier
  37. Advantages Isolation & reproducability Easier to add new services Bundling

    everything into a single entity Limiting/monitoring resource use is easier Single-command project setup
  38. It Depends... For legacy projects, maybe not But makes more

    sense for new projects Thorgate open-sourcing our project template
  39. Thanks! Rivo Laks ⋅ @rivolaks ⋅ rivolaks.com CTO at Thorgate

    ⋅ thorgate.eu https://github.com/thorgate/django-project-template