appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

BuildKit, scanning and supply chain

Lesson 2 of 411 minAdvanced

Faster builds, secrets that never reach the image and checks on what you ship.

BuildKit is Docker's modern build engine (the default in current versions). It builds independent stages in parallel and adds features such as cache mounts and build secrets.

Cache the package manager between builds
# syntax=docker/dockerfile:1
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install -r requirements.txt
COPY . .
Use a secret during build without storing it
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci
docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp .

A secret mounted this way exists only for that one RUN step and is not saved in any layer.

Know what you ship

  • Pin by digest for repeatable builds: FROM node:22-slim@sha256:.... A tag can be moved to different content, a digest cannot.
  • Scan images for known vulnerabilities (for example with Trivy or Docker Scout) in CI and fail on serious findings.
  • Generate an SBOM (software bill of materials) so you can tell which images contain a vulnerable library.
  • Sign images (Sigstore cosign) and verify the signature before deploying.
  • Rebuild regularly: a base image from last year has security fixes you have not received.

CarefulA clean scan does not mean an image is safe. It only finds known issues in known components. Combine scanning with minimal bases, least privilege and patching.

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

1. What is the advantage of pinning a base image by digest?
2. What does RUN --mount=type=secret achieve?
3. What is an SBOM?

Key terms