This is a highly opinionated example of how to manage dependencies of a python application and how to package it in a small alpine-based Docker image.
Copy the Dockerfile
into the root of your python project folder (next to your
pyproject.toml
) and run:
docker build -t myimagetag .
The application can be packaged into a slim (< 80 MB overhead) alpine-based Docker image by simply running:
docker build -t isay .
in the root folder.
If your app is using poetry as the dependency management system, you are good to go. Check out the docs at https://python-poetry.org/docs/.
Some python packages (including, maybe, your own) may have system dependencies
like openssl
or gcc
either in order to be compiled or in order to run. You
can define these dependencies in the [tools.sys.alpine3-11.dev-dependencies]
and [tools.sys.alpine3-11.dependencies]
sub-sections of your pyproject.toml
file. They each include the list of system dependencies along with their exact
versions. The dev-dependencies
are the ones needed for building wheels for the
dependencies (and for the project) while the dependencies
are needed to run
the application.
The alpine3-11
part in the section name has to match the version of alpine
linux that the python image is based on. The second line in the Dockerfile
reads:
FROM python:3.8-alpine3.11 as base
The alpine3.11
part in the Dockerfile
must match the alpine3-11
part in
pyproject.toml
(with the .
replaced by a -
due to restrictions on section
names in toml
).
If you'd like to use another version of alpine for your project, change that in
the second line of the Dockerfile
. If you rely on any system dependencies, refer
to the section below.
Whether you are running the build on your development machine or on a continuous integration (CI) build agent, the build process will try to make use of Docker's caching mechanism by building layers that depend on data likely to change less frequently ahead of the layers that depend on data that is likely to change more frequently. Here is the sequence:
- Python version:
Dockerfile
- Dev & runtime dependencies (system and python):
pyproject.toml
,poetry.lock
- Application installed in both the dev and runtime virtual environments: app source code