You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docker-best-practices.md
+53-16Lines changed: 53 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,31 +11,68 @@ menu:
11
11
12
12
# Docker Best Practices
13
13
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
15
27
16
-
....blabla
17
28
The following best practices should be kept in mind
18
29
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:
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.
- A basic virtual environment is created passing a pyproject.toml, via build context.
58
+
59
+
#### Use cases
23
60
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.
25
62
26
-
The following are general use cases that you can use a starting point for your specific case
63
+
### Poetry-multistage
27
64
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.
29
66
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.
- 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.
37
75
38
-
#### Dockerfile
76
+
#### Use cases
39
77
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