Add BuildKit RUN --mount=type=cache...

This commit is contained in:
Jérôme Petazzoni
2023-05-09 07:50:40 +02:00
parent 8184c46ed3
commit 6d13122a4d
2 changed files with 71 additions and 0 deletions

View File

@@ -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:

View File

@@ -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