A few technical notes
Dockerfiles
Venv
The base common py image uses a venv so you need to use .venv/bin/python
and pip --python .venv/bin/python
Caching
There are two types of caching used: One for local building and one in the CI with kaniko.
Local Building
The PIP_CACHE_DIR
and APT_CACHE_DIR
variables are empty, pip and apt use their default cache location. However, the
--mount=type=CACHE
option before the RUN command mounts these cache locations and so the cache can be reused between
different builds. Since the locations are mounted, the files put into there (during the installation command), are not
put into the final image.
Additionally, Docker provides a script for apt to automatically clean the cache (so it does not stay in the final image).
However, we don't want that, and so we need to remove the /etc/apt/apt.conf.d/docker-clean
file.
CI Building
The PIP_CACHE_DIR
and APT_CACHE_DIR
variables are set by the CI to a location (.cache/pip
, .cache/apt
) inside
the project dir. The cache option of the GitLab CI means these locations are persistent between different job runs. They
need to be inside the project dir since GitLab cannot cache locations outside. When kaniko now executes the Dockerfile
it will provide the environment vars with the caching locations. Pip directly reads the PIP_CACHE_DIR
env var (Note
that the ARG command in Docker provides the build argument as an environment variable to the container). For apt we
need to put the location into /etc/apt/apt.conf.d/
. The --ignore-path
option of kaniko ensures that the cache is not
included in the final image.
Also see https://github.com/GoogleContainerTools/kaniko/issues/969#issuecomment-2160910028.