Each Dockerfile instruction makes the builder start a container, run a command, and save the result as a layer. This check measures that fixed cost per instruction by timing two builds and subtracting:
(20-step build - one-line build) / 20The first build is a single FROM line. Its duration is the fixed cost of
starting the builder, preparing the base image, and writing the image. The
second build adds 20 instructions that each write a tiny file. Subtracting the
first duration from the second and dividing by 20 removes the fixed costs.
Both builds start from an empty build cache. Each duration is the wall-clock time
of a docker build command on your machine. Each pair of builds gives one
result.
For how durations are taken and how to repeat a check, see Performance checks .
WarningThis check runs
docker buildx prune --all --force, which deletes your entire local build cache. Don't run it on a machine where you need that cache.
Set up
$ mkdir -p ~/perfcheck/twenty-steps && cd ~/perfcheck/twenty-steps
$ printf 'FROM alpine:3.24\n' > Dockerfile.baseline
$ printf 'FROM alpine:3.24\n' > Dockerfile.twentysteps
$ i=1
$ while [ "$i" -le 20 ]; do
printf 'RUN mkdir -p /steps && printf "%02d" > /steps/%02d\n' "$i" "$i" >> Dockerfile.twentysteps
i=$((i + 1))
done
$ printf 'Dockerfile.baseline\nDockerfile.twentysteps\n.dockerignore\n' > .dockerignore
$ docker pull alpine:3.24
Pulling the base image keeps its download out of both timed builds. Stay in this directory for the runs.
Confirm that Dockerfile.twentysteps has exactly 20 RUN lines:
$ grep -c '^RUN ' Dockerfile.twentysteps
20
Run the check
Remove the previous images and empty the build cache. This step isn't timed. On the first run, the removal reports
No such image. That is expected.$ docker image rm --force perfcheck/twenty-baseline:current perfcheck/twenty-steps:current $ docker buildx prune --all --forceTime the one-line build:
$ time docker build --pull=false --progress=plain \ -f Dockerfile.baseline -t perfcheck/twenty-baseline:current .Remove that image and empty the build cache again, so the second build also starts with nothing. This step isn't timed.
$ docker image rm --force perfcheck/twenty-baseline:current $ docker buildx prune --all --forceTime the 20-step build:
$ time docker build --pull=false --progress=plain \ -f Dockerfile.twentysteps -t perfcheck/twenty-steps:current .Check that the run is valid, then clean up:
$ docker image inspect --format '{{.Id}}' perfcheck/twenty-steps:current $ docker image rm --force perfcheck/twenty-steps:currentCalculate the result in milliseconds:
(step 4 duration - step 2 duration) / 20A negative result means something went wrong, most often that the base image downloaded during a timed build.
Calculate the result once for each pair of builds. Don't average several builds and divide once.