From 6d13122a4d9b271cc765688d25fed1a8bf615882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Tue, 9 May 2023 07:50:40 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Add=20BuildKit=20RUN=20--mount=3Dty?= =?UTF-8?q?pe=3Dcache...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slides/containers/Buildkit.md | 20 ++++++++++ slides/containers/Multi_Stage_Builds.md | 51 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/slides/containers/Buildkit.md b/slides/containers/Buildkit.md index 4684e51e..68c40a7a 100644 --- a/slides/containers/Buildkit.md +++ b/slides/containers/Buildkit.md @@ -348,6 +348,26 @@ It will indicate which executables can be run on your engine. --- +## Cache directories + +```bash +RUN --mount=type=cache,target=/pipcache pip install --cache-dir /pipcache ... +``` + +- The `/pipcache` directory won't be in the final image + +- But it will persist across builds + +- This can simplify Dockerfiles a lot + + - we no longer need to `download package && install package && rm package` + + - download to a cache directory, and skip `rm` phase + +- Subsequent builds will also be faster, thanks to caching + +--- + ## More than builds - Buildkit is also used in other systems: diff --git a/slides/containers/Multi_Stage_Builds.md b/slides/containers/Multi_Stage_Builds.md index f944f17f..e885c687 100644 --- a/slides/containers/Multi_Stage_Builds.md +++ b/slides/containers/Multi_Stage_Builds.md @@ -296,6 +296,8 @@ virtually "free." --- +class: extra-details + ## Build targets * We can also tag an intermediary stage with the following command: @@ -313,6 +315,55 @@ virtually "free." (instead of using multiple Dockerfiles, which could go out of sync) +-- + +class: extra-details + +## Dealing with download caches + +* In some cases, our images contain temporary downloaded files or caches + + (examples: packages downloaded by `pip`, Maven, etc.) + +* These can sometimes be disabled + + (e.g. `pip install --no-cache-dir ...`) + +* The cache can also be cleaned immediately after installing + + (e.g. `pip install ... && rm -rf ~/.cache/pip`) + +--- + +class: extra-details + +## Download caches and multi-stage builds + +* Download+install packages in a build stage + +* Copy the installed packages to a run stage + +* Example: in the specific case of Python, use a virtual env + + (install in the virtual env; then copy the virtual env directory) + +--- + +class: extra-details + +## Download caches and BuildKit + +* BuildKit has a caching feature for run stages + +* It can address download caches elegantly + +* Example: + ```bash + RUN --mount=type=cache,target=/pipcache pip install --cache-dir /pipcache ... + ``` + +* The cache won't be in the final image, but it'll persist across builds + ??? :EN:Optimizing our images and their build process