Guides · Checked on 28 November 2026
How do I cache Docker layers in GitHub Actions?
Copy your dependency manifests and install packages before copying the rest of the source. Then give BuildKit an external cache: cache-to saves layers and cache-from reads them on a fresh runner. After a source edit, the compiler still runs, but the dependency install can reuse its layer.
Our run of Prometheus Blackbox Exporter v0.29.1 on 28 November 2026
One cold run per runner. Warm times are the median of three runs, with the range in parentheses.
| Workload | GitHub-hosted (2 vCPU) | Checklane (2 vCPU) |
|---|---|---|
| Cold, whole job | 129 s | 49 s |
| Cold, build and load | 108 s | 36 s |
| Warm, whole job | 94 s (93–101) | 39 s (37–42) |
| Warm, build and load | 77 s (71–82) | 27 s (25–29) |
On 28 November 2026 we built Blackbox Exporter v0.29.1 in a private repository. Both runners reported two CPUs. Job time excludes queueing; the build step includes loading the image and, on the cold run only, writing the cache. Times are seconds, with the median and observed range for each condition. The cold-to-warm difference is not the time saved by skipping the install alone.
Rebuild Blackbox Exporter after a one-line edit
Extract Prometheus Blackbox Exporter v0.29.1 into a directory named
blackboxin a private repository. Keep its Apache 2.0 license and NOTICE. The image builds the exporter from source, then copies the binary, configuration and notices into a separate stage.Save this as
Dockerfile.cacheat the repository root:FROM golang:1.26-bookworm@sha256:ea47ee45f1284d60f9cc09cb2c3bb4f508c256b640bd8bb30342ff13d1adcb52 AS build WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 go build -mod=readonly -o /out/blackbox_exporter . FROM scratch COPY --from=build /out/blackbox_exporter /blackbox_exporter COPY blackbox.yml LICENSE NOTICE / ENTRYPOINT ["/blackbox_exporter"]Save the YAML under
.github/workflows/and push it with the source on a new branch namedrevised-docker-2. Its first push writes a separate cache for each runner. Once both jobs pass, change the help text forconfig.filein blackbox/main.go and push. Re-run all jobs on that commit twice. All three warm builds read the first push’s cache and leave it unchanged.
Tested on 28 November 2026
Show the workflow YAML
name: Blackbox Exporter layer cache
on:
push:
branches: [revised-docker-2]
permissions:
contents: read
env:
DOCKER_BUILD_RECORD_UPLOAD: "false"
jobs:
image:
strategy:
fail-fast: false
matrix:
runner: [ubuntu-latest, checklane-2vcpu]
runs-on: ${{ matrix.runner }}
timeout-minutes: 25
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Verify CPU count
run: nproc && test "$(nproc)" -eq 2
- uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: Build and load image
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: ./blackbox
file: Dockerfile.cache
tags: blackbox-cache:example
load: true
cache-from: ${{ !github.event.created && format('type=gha,scope=blackbox-v0291-{0}', matrix.runner) || '' }}
cache-to: ${{ github.event.created && format('type=gha,scope=blackbox-v0291-{0},mode=max', matrix.runner) || '' }}
- name: Check bundled config
run: docker run --rm blackbox-cache:example --config.check
Run on 28 November 2026 on checklane-2vcpu: all three rebuilds reused the dependency layer, compiled the edited source and passed the bundled config check. The same three checks passed on ubuntu-latest.
Related guide: How do I cache pnpm in GitHub Actions?
Frequently asked questions
Will a restored npm or Go cache skip the Docker install step?
A matching Docker layer skips the instruction. A package cache supplies files if the installer runs, and only if the installer can see that cache inside the build. Restoring packages on the runner alone does not restore a Docker layer.
Docker: layers and cache mountsWhy did my application edit invalidate the dependency layer?
Look for a COPY of the whole source tree before the install. Changing a copied file invalidates that instruction and the steps that follow it in the same stage. Copy the lockfile and package manifest first so an application edit lands after the install.
Docker: build cache invalidationDo I need to push the image to test it?
No. Load the image into Docker on the runner and run your check against the local tag. The example runs the built binary to validate its bundled config. A cache hit tells you a build instruction was reused, not that the application passed its tests.
Docker: test before pushing an imageRun this job on Checklane
To compare runner sizes for your own image build, keep the source edit and the seed cache fixed. Check which instructions ran before interpreting the times.
Connect your repository, then set the job’s runner label:
runs-on: checklane