Sub topic 11
Enforcing size rules in CI with dive
This chapter assumes you can build images with Docker, read the docker history and dive output from earlier chapters, and understand how wasted space and the efficiency score are calculated. Here you will turn those manual checks into an automated gate that fails a pipeline when an image grows wasteful.
Why automate the check
Reading dive's interface once tells you whether an image is wasteful today. It does not stop someone from adding an RUN rm after a large COPY next month. A CI check catches that regression in the pull request that introduces it.
dive supports this with a non-interactive mode. It analyses the image, compares the results with a set of rules, prints a pass or fail result for each rule, and exits with a status code that CI understands.
The three rules
Rules live in a YAML file named .dive-ci in the directory where you run dive.
rules:
lowestEfficiency: 0.95
highestWastedBytes: 20MB
highestUserWastedPercent: 0.20| Rule | Meaning | Fails when |
|---|---|---|
| `lowestEfficiency` | Minimum efficiency score, from 0 to 1 | The score is below the value |
| `highestWastedBytes` | Maximum total wasted space | Wasted bytes exceed the value |
| `highestUserWastedPercent` | Maximum share of the image that is wasted by your own layers | The share is above the value |
The third rule is the most useful for application images. It measures waste in layers you wrote, so a wasteful base image that you cannot change will not fail the check on its own.
You can turn off a rule by setting its value to disabled. Use this when a rule does not fit your project, rather than deleting it, so the intent stays visible.
Running dive in CI mode
Setting the CI environment variable to true makes dive skip the interface and read .dive-ci.
CI=true dive demo:wastefulThe command exits with status 0 if every rule passes and a non-zero status if any rule fails. Most CI systems treat a non-zero exit as a failed step, so no extra scripting is needed.
If your rules file has a different name or location, point dive at it with --ci-config.
CI=true dive demo:wasteful --ci-config ci/image-rules.yamlMany CI systems already set CI=true for you. In that case, running dive on an image in a pipeline starts the non-interactive mode without any extra setup. Check your runner's documentation before relying on this, and set the variable explicitly if you want the behaviour to be obvious to readers of the pipeline.
Where the check fits in a pipeline
The check needs a built image on the machine that runs dive. Build first, then analyse, and only publish if the analysis passes.
A minimal shell version of that sequence looks like this.
set -e
docker build -t demo:ci .
CI=true dive demo:ci
docker push demo:ciWith set -e, the script stops at the first failing command, so a failed dive check prevents the push.
Choosing thresholds
Start from measurements, not guesses.
- Run dive interactively on your current image and note its efficiency score and wasted bytes.
- Set thresholds slightly looser than those values, so the first run passes.
- Fix real waste using the techniques from the previous chapter, then tighten the thresholds.
If you set a threshold the current image already violates, the pipeline fails on every commit and the team will learn to ignore it. A gate that always passes is also useless, so review the numbers when the image changes in a meaningful way.
Common errors
- The check always passes. dive may have run in interactive mode or without a rules file. Confirm that
.dive-ciis in the working directory, or pass--ci-config. - The pipeline hangs. dive tried to open its interface. Set
CI=trueexplicitly. - The image is not found. The image must exist in the local Docker daemon on the runner. Run the build step in the same job, or load the image before analysing it.
- The check fails because of the base image. Prefer
highestUserWastedPercent, which focuses on your layers, and loosen the other rules.
Exercise
Your team wants the pipeline to fail if more than 10 percent of the image is wasted by layers the team wrote, but to ignore the absolute number of wasted bytes. What does the .dive-ci file look like, and what command runs the check on the image app:ci?
Show answer
The file sets highestUserWastedPercent: 0.10 and disables the byte rule: rules: with highestUserWastedPercent: 0.10 and highestWastedBytes: disabled. You can also disable lowestEfficiency if you do not want that rule. The command is CI=true dive app:ci. It exits non-zero when user-layer waste exceeds 10 percent, which fails the CI step.