Skip to content

Commit 5500497

Browse files
gianfaradoering
authored andcommitted
feat(docs, docker-best-practices): add minimum-poetry and poetry-multistage
1 parent e2d89e8 commit 5500497

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

docs/docker-best-practices.md

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,68 @@ menu:
1111

1212
# Docker Best Practices
1313

14-
## Introduction
14+
- [Best Practices](#best-practices)
15+
- [Imags examples and use cases](#imags-examples-and-use-cases)
16+
- [Minimum-poetry](#minimum-poetry)
17+
- [Specifics](#specifics)
18+
- [Use cases](#use-cases)
19+
- [Poetry-multistage](#poetry-multistage)
20+
- [Specifics](#specifics-1)
21+
- [Use cases](#use-cases-1)
22+
23+
Poetry is a very valuable tool for increasing the robustness and reproducibility of a virtual environment on which your python code is based. When integrating Poetry into a Docker image, adopting some best practices will help improve build efficiency, container security, and help achieve lighter images. In this section, we will explore best practices for creating optimized and secure Docker images for projects managed with Poetry.
24+
This section is a developing project, so you are warmly invited to contribute new suggestions.
25+
26+
## Best Practices
1527

16-
....blabla
1728
The following best practices should be kept in mind
1829

19-
- [optional] set the latest python version, in order to get the latest patch
20-
- [highly suggested] use pip to install poetry
21-
- [critical] never hardcode credentials to private sources
22-
- ...
30+
- [optional] Set the latest python version, in order to get the latest security patch.
31+
- CAVEAT: It might reduce the reproducibility of the code, between one image build and another, since some function might change from one version of python to another.
32+
- [highly suggested] Use `pip` to install poetry (see https://python-poetry.org/docs/#ci-recommendations).
33+
- [highly suggested] Clear Poetry cache after the installation.
34+
- [critical] Never hardcode credentials to private sources.
35+
- [optional] Install Poetry in a dedicated venv
36+
- [highly suggested] Install the virtual env in the Python project (see `POETRY_VIRTUALENVS_IN_PROJECT`). This will be more convenient for carrying the env around with everything you need, making the project more self-contained.
37+
- [highly suggested] Take advantage of Docker's layer caching mechanism to rebuild the image much faster. This means that you should reduce the variability points in the Dockerfile and the files linked to it (e.g. ARGS that may change). In alternative you can move them as far down in the Dockerfile as possible. For more info please see:
38+
- https://docs.docker.com/build/cache/
39+
- https://pythonspeed.com/docker/
40+
- [highly suggested] copy source code only after `poetry install`. For more info see:
41+
- https://python-poetry.org/docs/faq/#poetry-busts-my-docker-cache-because-it-requires-me-to-copy-my-source-files-in-before-installing-3rd-party-dependencies
42+
43+
## Imags examples and use cases
44+
45+
Below are general examples of Docker images, along with their typical use cases, to help you get started with developing your specific application.
46+
47+
### Minimum-poetry
48+
49+
[Minimum-poetry](../docker-examples/minimum-poetry/README.md) is the minimum-constructible image containing poetry, from an official python base image.
50+
51+
Expected size: ~218 MB, virtual env layer excluded.
52+
53+
#### Specifics
54+
55+
- Based on *python:3.11-slim* official image.
56+
- Just installs Poetry via pip.
57+
- A basic virtual environment is created passing a pyproject.toml, via build context.
58+
59+
#### Use cases
2360

24-
## Use cases
61+
As in the case of [Minimum-poetry](../docker-examples/minimum-poetry/README.md), this image is useful when you need to create a virtual self-content environment, complex at will.
2562

26-
The following are general use cases that you can use a starting point for your specific case
63+
### Poetry-multistage
2764

28-
### UC1: Dev environment
65+
[Poetry-multistage](./../docker-examples/poetry-multistage/README.md) is a minimum-constructible multistage image containing Poetry, from an official Python base image. It is very similar to [Minimum-poetr](#minimum-poetry), except that it may be more complex as it implements at least 2 more best practices.
2966

30-
Here is an example of how to create a dev container aimed to host a basic development env. Once the image is built nobody can make OS changes, except the admin. An example of usage is a container used by a team.
67+
Expected size: ~130MB, virtual env layer excluded.
3168

3269
#### Specifics
3370

34-
- Unprivileged User.
35-
- multistage, in order to make the image lighter
36-
- ...
71+
- Based on *python:3.11-slim* official image.
72+
- Installs Poetry via pip.
73+
- A basic virtual environment is created in the project folder (`POETRY_VIRTUALENVS_IN_PROJECT=1`, `POETRY_VIRTUALENVS_CREATE=1`).
74+
- A multistage build is implemented, allowing you to directly copy only the project virtual env and set its reference in path, so as to minimize memory waste.
3775

38-
#### Dockerfile
76+
#### Use cases
3977

40-
FROM python .......
41-
....
78+
The usefulness of this image lies in the Dockerfile that shows an example of how to build a multistage image, to optimize the construction of the virtual environment. Always use it as a starting point for your images that you want to optimize in size.

0 commit comments

Comments
 (0)