ONBUILD

Difficulty: Advanced

Question

What is the ONBUILD instruction in a Dockerfile, when does it run, and why is it rarely used today?

Answer

ONBUILD registers an instruction that does not run when you build the current image. Instead it is stored in the image metadata as a trigger, and it runs automatically in any later build that uses this image in its FROM line, immediately after that FROM. It lets a base image author define steps every child image must perform, such as copying source code and installing dependencies.

How the trigger mechanism works

  1. The base image Dockerfile contains, for example, ONBUILD COPY package*.json ./ and ONBUILD RUN npm ci.
  2. Building the base image skips those lines but records them in Config.OnBuild.
  3. A team writes FROM company/node-base:1.0 in their app Dockerfile.
  4. The builder runs the triggers in order right after FROM, using the child build's context, then continues with the child's own instructions.
  5. Triggers are consumed: they do not carry over to grandchildren. If the child image is itself used as a base, the original triggers do not run again.

You can see an image's triggers with docker inspect --format '{{json .Config.OnBuild}}' <image>.

Restrictions

  • ONBUILD ONBUILD is not allowed.
  • ONBUILD FROM and ONBUILD MAINTAINER are not allowed.
  • If any trigger fails, the child build fails at its FROM line, which is confusing to debug.

Why it fell out of favour

Official images used to ship -onbuild variants (node:onbuild, python:onbuild). Docker deprecated and stopped publishing them years ago, for good reasons:

  • Hidden behaviour. Someone reading the child Dockerfile sees only FROM and cannot tell that files are being copied and commands run.
  • Loss of control over caching. A trigger like ONBUILD COPY . . runs before the child can copy just package.json first, so every code change busts the dependency cache.
  • Assumed context layout. The trigger assumes files exist at certain paths in the child's context. Different repo layouts break it.
  • Portability. ONBUILD is Docker-specific metadata; some non-Docker builders or OCI-format builds may drop it.

What teams use instead

  • Multi-stage builds, where the "build" logic is visible in every Dockerfile.
  • A shared base image without triggers, plus a documented Dockerfile template.
  • Build tooling such as Cloud Native Buildpacks, which detect the language and build the image without a Dockerfile.

ONBUILD still has a niche: internal platform teams that want every service built on their base to, say, copy a mandatory CA certificate bundle or run a compliance scan step. If you use it, document it loudly in the base image README.

When ONBUILD triggers fire. Triggers are stored in the base image and executed only in the next build that uses it.
  1. Base Dockerfile with ONBUILD: Trigger lines are recorded, not executed.
  2. Base image metadata: Config.OnBuild holds the trigger list.
  3. Child Dockerfile: FROM base: Builder runs every trigger right after FROM.
  4. Child's own instructions: Triggers are consumed and not passed to grandchildren.

Code examples

Base image with triggers and a child that uses it

# ---- base/Dockerfile ----
FROM node:22-alpine
WORKDIR /app
ONBUILD COPY package*.json ./
ONBUILD RUN npm ci --omit=dev
ONBUILD COPY . .
CMD ["node", "server.js"]

# docker build -t acme/node-base:1.0 base/
#   -> the three ONBUILD lines do not run here

# ---- orders-service/Dockerfile ----
FROM acme/node-base:1.0
# Triggers run now: copy package files, npm ci, copy source
EXPOSE 3000

The child Dockerfile is two lines, but three hidden steps run during its build, which is both the convenience and the problem of ONBUILD.

Inspecting triggers on an image

docker inspect acme/node-base:1.0 --format '{{json .Config.OnBuild}}'

Always check this before using an unfamiliar base image so its triggers do not surprise you.

Key points

  • ONBUILD stores an instruction that runs in a downstream build right after FROM.
  • Triggers run only one level down; grandchildren do not re-run them.
  • ONBUILD ONBUILD, ONBUILD FROM and ONBUILD MAINTAINER are not allowed.
  • Official -onbuild image variants were deprecated because the behaviour was hidden and cache-unfriendly.
  • Prefer multi-stage builds or explicit templates in most cases.

Common follow-up questions

  • Why did the official images drop their -onbuild variants?
  • How would you enforce a mandatory build step across 50 microservices without ONBUILD?
  • How do Cloud Native Buildpacks compare with Dockerfiles?

Interview tips

  • Be precise about timing: the trigger runs in the child build, immediately after FROM.
  • Show judgement by explaining why it is discouraged rather than just defining it.
  • Mention the one-level-only rule; it is a common trick question.

Concepts covered

ONBUILD, Build Triggers, Base Images, Dockerfile Inheritance

More Docker & Containers questions