Safeguard
Developer Tools•

What does ONBUILD do in a Dockerfile, and should you use it?

ONBUILD runs hidden steps when someone builds from your image. Learn how triggers work, why they surprise reviewers, and when a plain stage is safer.

Safeguard Security Team
3 min read

ONBUILD registers an instruction that runs later, when another Dockerfile uses your image in its FROM line. The trigger runs as if it were placed right after that FROM. It can be handy, but it hides work from reviewers, so most teams are better off with explicit stages.

How it works

The Dockerfile reference describes the rules. A trigger is stored in the image. When a downstream build uses that image as its base, triggers run in the order they were registered. If one fails, the downstream build fails. Triggers are cleared from the resulting image, so they do not pass on to grandchild images. ONBUILD ONBUILD is not allowed, and ONBUILD cannot trigger FROM or MAINTAINER.

ONBUILD COPY . /app
ONBUILD RUN make

A team that builds from this base gets a copy and a make step added automatically.

Why it is a risk

IssueEffect
Hidden behaviorThe downstream Dockerfile does not show what runs
Surprise inputsA trigger may copy the whole build context, including files you did not mean to ship
TrustYou run instructions chosen by whoever built the base image
Review gapsStatic checks of the downstream file miss the trigger

The trust point is the important one for security. If you pull a base image from outside your organization, an ONBUILD trigger runs inside your build, with your build context and any secrets you mounted. You can inspect an image for triggers with docker inspect and look at the configuration for an on build field before you use it.

Alternatives

  1. A shared base image with no triggers, plus a documented Dockerfile template in each repository.
  2. A multi-stage Dockerfile where the steps are written out. See build targets.
  3. A build tool or CI template that runs the same steps and is code reviewed.

Each puts the behavior where a reviewer can read it.

If you do use it

Keep triggers to a minimum, document them in the base image README, restrict the base image to your own registry, and test downstream builds in CI. Avoid triggers that copy the entire context, and list them in your image inventory so owners know the base has them.

Checking third party bases

Before adopting a base image, inspect its configuration for triggers, read its Dockerfile source if published, and check that its builds are reproducible. A scan of the final image will not reveal a trigger, since triggers clear after they run.

Quick check

Do ONBUILD triggers appear in the final image?

No. They are cleared from the resulting image, so they run once in the build that uses the base.

How can I see triggers in an image?

Inspect the base image configuration and look for the on build entries before you build from it.

Safeguard

Safeguard does SBOM and dependency scanning, and its policies and gates can run in CI so a bad image does not reach production. You can try a free scan before deciding anything.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.