Use a cache mount in RUN

Reuse the package cache between builds without making layers bigger

RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt

tool
buildkit4 commands
tags
buildkitperformanceci

More buildkit commands

  • RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci

    Use a secret file in one RUN without saving it in any layer. Pass it with --secret

  • docker buildx build --ssh default -t <name> . # Dockerfile: RUN --mount=type=ssh git clone git@host:repo

    Forward the host SSH agent into the build to clone private git repos. The key never gets into the image

  • docker buildx build --cache-to type=registry,ref=<reg>/cache,mode=max --cache-from type=registry,ref=<reg>/cache -t <reg>/<name> --push .

    Keep the full layer cache in a registry for CI. mode=max also caches intermediate stages

all 4 buildkit commands