Django + Poetry + Docker
Nathan Clement · September 8, 2026 · 4 min read
Managing Python packages for Django made delightful
Photo by Patrick Tomasso on Unsplash
Why use a Package Management System?
A new developer might start building a Django site by installing the latest version of Python from python.org, and using pip to install any necessary packages. Once local development is complete, they may deploy their code to a server prebuilt to host Python applications, only to see an Exception:
This happens because no one installed Django on the host server.
To avoid this issue, they could define all imports in a Dockerfile. Something like this would work:
This becomes painful to manage as soon as they start installing more dependencies, since they must carefully ensure that all local packages precisely match those in the Dockerfile.
If they start building another project that needs a different version of Python, or a different version of a package, the difficulty grows rapidly.
Several systems have grown within the Python ecosystem to alleviate the pain of package management complexity.
Virtual Environments
One of the most commonly referenced ways to manage Python packages and environments is to use the standard module.
This article does a great job explaining what Virtual Environments are in Python, and how to use them: https://realpython.com/python-virtual-environments-a-primer/
Virtual environments work great, but can also lead to frustration:
- The version of Python is not included in — which can make upgrades less clear.
- Your Virtual Environment will get out of sync if you install a new dependency and forget to run
- Since includes all dependencies for your installed packages, it can be difficult to clean out a package that is no longer being used.
Introducing Poetry
Poetry manages the creation of virtual environments in a way that I have found both powerful and delightful.
Some highlights:
- Files called and manage your environment in a way that feels similar to and in Node.js
- Includes a powerful CLI tool reminiscent of
- Python is managed like a dependency by Poetry, allowing you to configure your Python version within the file
- Scripts can be run with
Putting it all together
Imagine you have a simple Django application that you want to deploy as a Docker container, managed by Poetry.
Using Poetry Locally:
- Install Poetry locally
- Add Poetry to your path if it is not already there. I use so I added the following to my file:
- Initialize Poetry — you can use the CLI prompts to interactively set up your dependencies, or add the later yourself
- Add dependencies (for example )
- Run scripts (i.e. )
Preparing to Deploy with Docker
Here is an example working Dockerfile:
This script starts the Django server (per the Dockerfile above, I store it at
In this case, lets me avoid adding poetry to the PATH in my container.
Deploy this container, and you are off to the races!
Let me know what you think about this approach, and if you see the benefit of a tool like Poetry, or if you have a different preferred Python package management strategy!
Originally published on 2023-01-31 on Medium.