
Beyond Grype: Making Disposable MegaLinter Runs Fast
14 min read
This is the third post in a four-part series following a MegaLinter optimization investigation. The first post moved the workload from GitHub Actions to Jenkins; the second followed Grype from roughly three minutes to about four seconds. This post asks whether the persistence lesson from Grype generalizes to the rest of the pipeline.
By the time I reduced Grype from roughly three minutes to about four seconds, I was less interested in Grype as an isolated problem.
The more useful discovery was architectural.
MegaLinter was still running in disposable containers, exactly as I wanted. Each invocation started clean and disappeared when the lint job finished. The problem was that some of the tools inside those containers performed expensive work whose useful lifetime was much longer than the container that happened to perform it.
Grype made that mismatch visible. It prepared a large vulnerability database inside a container started with docker run --rm, used the database, and then lost the prepared state when Docker removed the container. Giving that database a lifecycle independent of the MegaLinter container reduced a representative Grype run from roughly 190 seconds to about four seconds.
Once that worked, another question followed naturally:
Which other MegaLinter scanners were rebuilding useful state inside a container that was going to disappear a few seconds later?
I did not want to respond by caching everything I could find. Temporary execution state is one of the reasons I like disposable containers in the first place. The goal was narrower: identify expensive filesystem state that was designed to be reused, preserve that state outside the container, and leave the execution environment disposable.
That distinction became the organizing principle for the next round of optimization.
Keep execution disposable and expensive knowledge persistent
The execution model remained:
docker run --rm ...MegaLinter did not become a long-running service. I did not begin reusing containers between repositories, and I did not try to preserve every file a scanner happened to create.
The distinction I was interested in looked more like this:
Disposable:
MegaLinter container
scanner processes
temporary reports and working state
Persistent when useful:
vulnerability databases
compiled scanner rules
downloaded modules
downloaded enrichment dataSeveral MegaLinter scanners expose supported locations for this kind of state:
| Scanner | Persistent state | Configuration |
|---|---|---|
| Grype | Installed vulnerability database | GRYPE_DB_CACHE_DIR |
| Trivy | Vulnerability and misconfiguration cache | TRIVY_CACHE_DIR |
| Checkov | Downloaded external modules | EXTERNAL_MODULES_DIR |
| Kingfisher | Compiled rule database | KF_RULE_CACHE_DIR |
| Syft | Downloaded and enrichment cache | SYFT_CACHE_DIR |
| OSV-Scanner | Local vulnerability databases | OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY |
The existence of a cache does not establish that every repository benefits from using it. That depends upon what the scanner is doing and whether the repository gives it applicable work. What mattered initially was that these tools identified reusable state whose lifecycle did not need to match the lifetime of the MegaLinter container.
The architecture therefore became a generalized version of the Grype solution. Jenkins supplies node-local Docker volumes, while the Makefile mounts only the volumes that have been configured.
The container remains temporary. The useful knowledge acquired or prepared by the scanners can survive it.
Keeping the optimization outside the repository contract
I wanted to preserve another constraint from the earlier Jenkins work: a repository should not depend upon my infrastructure merely because my infrastructure can run it more efficiently.
The organization-level knobs therefore grew to include values conceptually like these:
LINT_GRYPE_CACHE_VOLUME
LINT_TRIVY_CACHE_VOLUME
LINT_CHECKOV_EXTERNAL_MODULES_VOLUME
LINT_KINGFISHER_CACHE_VOLUME
LINT_SYFT_CACHE_VOLUME
LINT_OSV_CACHE_VOLUMEJenkins can provide those values through Organization Folder properties. When a value exists, the MegaLinter Docker invocation mounts the corresponding volume and supplies the scanner’s supported cache location. When a value does not exist, the repository still runs normally without that optimization.
The separation remains:
repository:
knows how to run MegaLinter
Jenkins:
knows which local optimizations are availableA clone of the repository does not require my Jenkins environment or its Docker volumes.
That distinction became more valuable as the number of scanner-specific settings grew. It also created a secondary problem: MegaLinter configuration was beginning to consume too much of the project’s primary Makefile.
Giving MegaLinter its own Make fragment
The first version of the Jenkins integration was compact. The repository exposed make lint, Jenkins invoked it, and the Makefile handled the Docker command.
As image configuration, database endpoints, cache directories, volume names, and scanner-specific environment variables accumulated, that implementation started to become disproportionate to the rest of the Makefile.
I moved the MegaLinter execution machinery into:
mk/megalinter.mkThe main Makefile can then include it:
include mk/megalinter.mkThat produced a separation I like:
.mega-linter.yml
What should MegaLinter do?
mk/megalinter.mk
How should MegaLinter be executed?
Jenkins Organization Folder properties
Which local infrastructure optimizations are available?The distinction is useful beyond the repository where I first developed it. The execution machinery can be reused without turning every project’s primary Makefile into a record of the infrastructure experiments that produced it.
More importantly, the boundaries describe different kinds of responsibility. MegaLinter configuration belongs with the linting policy. Docker invocation belongs with the reusable execution mechanism. Private infrastructure details belong with the environment that provides them.
The caching experiment could then continue without changing that model.
Three runs on the same Jenkins worker
Builds #13, #14, and #15 provided a useful sequence because all three ran on the same Jenkins worker.
Build #13 already had the warm Grype volume from the earlier investigation, but the additional scanner cache volumes had not yet been enabled.
Build #14 introduced volumes for Trivy, Checkov, Kingfisher, Syft, and OSV-Scanner. Those caches were therefore new or beginning to populate.
Build #15 repeated the workload with those volumes available and populated.
At the Jenkins level, the progression looked like this:
| Build | Cache state | Total runtime | Lint stage |
|---|---|---|---|
| #13 | Grype warm; others absent | 1:26 | 1:21 |
| #14 | New cache volumes introduced | 1:19 | 1:14 |
| #15 | Warm cache volumes | 0:58 | 0:54 |
From build #13 to build #15, the complete Jenkins job moved from 86 seconds to 58 seconds.
That removed another 28 seconds, or roughly one-third of the remaining runtime. The complete job also crossed an arbitrary threshold that felt significant when I saw it in Jenkins: less than one minute.
The scanner-level results explained much of the change:
| Scanner | #13 | #14 | #15 warm |
|---|---|---|---|
| Grype | 8.35s | 34.50s | 3.71s |
| Trivy | 25.98s | 20.47s | 4.12s |
| Kingfisher | 19.35s | 15.90s | 3.49s |
| Syft | 18.59s | 6.50s | 3.09s |
| Checkov | 62.38s | 51.42s | 32.08s |
Some of those results were much cleaner than others.
Trivy and Kingfisher behaved like Grype
Trivy produced one of the strongest results.
With the newly created cache in build #14 and that cache warm in build #15, the progression was:
That is roughly an 80 percent reduction between those two runs.
Kingfisher followed a similar pattern:
or approximately a 78 percent reduction.
Kingfisher’s behavior fits the model especially well because its secret-detection rules can be compiled into reusable state. Without persistence, a disposable container can perform that preparation and then throw the result away immediately after the scanner finishes. Preserving the documented cache lets a later invocation reuse that work.
Trivy and Kingfisher therefore joined Grype as strong evidence for the general principle. The containers did not need to survive; the expensive state did.
Grype itself returned to 3.71 seconds in build #15, which is consistent with the roughly four-second warm result from the earlier investigation.
The 34.50-second Grype result in build #14 deserves restraint. Its Docker volume already existed before these three builds, so #14 was not a clean Grype cold-cache experiment. A database refresh or some other transient operation may have occurred, although these measurements do not establish the cause.
The cleaner Grype comparison remains the original one:
I would rather preserve the anomaly than manufacture an explanation for it.
Some caches are harder to prove
Syft’s measurements look encouraging:
Syft also supports persistent cache state, so retaining the volume is reasonable. The timing history does not isolate its effect as cleanly as the Trivy and Kingfisher results, though.
The first Jenkins execution had recorded Syft at only about 2.5 seconds, before this broader caching work existed. That older result means I cannot reasonably attribute every later Syft improvement to the new persistent volume.
The mechanism is supported. The experiment is noisy.
I think that distinction is worth preserving because performance investigations become misleading when every number is forced to support the desired conclusion. A supported cache may still be useful even when a particular set of measurements does not isolate its contribution.
OSV-Scanner provides a cleaner example of that distinction. The reusable Make fragment supports a volume for its local vulnerability databases, but OSV-Scanner was disabled in these builds.
Its mounted cache therefore did no useful work.
The capability exists. This experiment did not validate its effect.
Checkov exposed optimization headroom I deliberately left alone
Checkov’s numbers also improved:
It would be tempting to credit the Checkov external-modules volume for that improvement. I do not think the evidence supports that conclusion.
This particular Bash repository contains no infrastructure-as-code and therefore has no external Terraform modules for that cache to preserve. Whatever caused the runtime variation, this workload does not provide a meaningful test of the Checkov cache.
There is another important distinction here. I did not disable Checkov.
From the beginning of the optimization process through build #15, I did not disable any of the linters to improve the result. I could almost certainly have removed additional time by turning off scanners that had no applicable input in this repository. Checkov was a clear candidate.
That would have answered a different question.
The experiment I was running asked:
How fast can I make the configured pipeline while preserving its enabled linting functionality?
That makes Checkov’s remaining 32.08 seconds interesting for another reason. It represents optimization headroom I intentionally left untouched. The 58-second result is therefore not a claim about a fully optimized MegaLinter configuration. It is the result of improving execution efficiency while keeping the configured linter set intact.
From three-and-a-half minutes to less than one
The larger Jenkins history puts the 58-second result in perspective.
Before the Grype breakthrough, builds #6 through #11 clustered around:
3:29
3:30
3:34
3:42
3:42
3:55Their average was approximately:
3:39Build #15 completed in:
0:58The steady-state local pipeline therefore moved approximately from:
That is roughly a 73 percent reduction.
That reduction occurred without disabling any of the linters that were enabled when the optimization work began.
The shape of the improvement matters more to me than the percentage:
The improvements arrived unevenly rather than as a long series of five-percent reductions.
One pathological cold-start cost dominated the pipeline so completely that almost everything else was background noise. Once Grype’s database survived the container that used it, the next tier of costs became visible. Trivy and Kingfisher then exposed the same lifecycle mismatch on a smaller scale.
Every successful optimization changed what was worth investigating next.
That can be overlooked when tuning systems. Once an expensive component has been identified, it is tempting to keep optimizing it because it remains familiar. The measurements after a change may describe a completely different critical path.
What a completely cold environment looks like
Build #5 does not belong in the steady-state Jenkins comparison.
It was the first invocation, and Docker had to download the MegaLinter image before MegaLinter could begin. Treating that run as representative of normal Jenkins performance would distort the comparison.
It remains useful for a different reason.
Some of its scanner timings were:
| Scanner | Build #5 |
|---|---|
| Grype | 201.68s |
| Checkov | 94.50s |
| npm-groovy-lint | 67.74s |
| Trivy | 26.15s |
| Kingfisher | 17.74s |
This is closer to what “nothing is warm yet” looks like.
That distinction matters when comparing persistent workers with ephemeral CI environments. Container image acquisition, vulnerability databases, compiled rules, and other reusable data all have lifecycles. A platform that starts from an empty environment has to decide whether and how those lifecycles can extend across jobs.
My Jenkins workers gave me a convenient place to do that. They did not make the underlying costs disappear.
Warm nodes are part of the result
The 58-second number needs an important qualification: it is a warm-node result.
The scanner caches live in node-local Docker volumes. Every Jenkins worker therefore has to populate its own Grype, Trivy, Kingfisher, and other applicable state before it receives the full benefit.
With three Jenkins nodes and one job at a time per node, the model is effectively:
worker A -> local caches
worker B -> local caches
worker C -> local cachesI chose that deliberately.
The alternative I had considered earlier was shared writable state, including SQLite databases, across multiple workers. Node-local volumes avoid introducing a shared writable database and the concurrency or network-filesystem behavior that would come with it.
The cost is that a worker encountering a scanner for the first time pays the cold-start cost independently.
Those constraints are part of the architecture, so they belong with the performance result. The 58-second measurement is a warm-node result. It does not claim that every worker will complete every invocation in 58 seconds.
By that point, I had answered the question I wanted this stage of the experiment to answer. The same configured linter set that had averaged about three minutes and 39 seconds was completing in 58 seconds once the expensive, reusable state survived the containers that used it.
I had deliberately left another source of optimization untouched. Some of the remaining scanners did not provide useful evidence for this particular Bash repository. Removing them would make the pipeline faster, although it would also change the workload and therefore answer a different question.
That is the subject of the fourth and final post: Making MegaLinter Do Less Work.