Dockerfile ONBUILD: deferred downstream build steps
Read the recipe
FROM busybox:1.37ONBUILD COPY downstream.txt /downstream.txtCMD ["echo", "base only"]The base image records the COPY trigger without copying downstream.txt. The child recipe selects the local base; its FROM processing triggers COPY using the child build context. A missing required source would fail the child build. After successful execution the inherited trigger is consumed; it does not run again in a grandchild merely because the base defined it. Review the OnBuild configuration of unfamiliar base images before using them. Inspect the trigger before the child build and compare it with the reviewed recipe. If the local child base cannot be resolved offline, treat the exercise as blocked. Do not fetch a similarly named registry image.
Try it in a local lab
Manual lab only, not an interactive ByteMind execution. Use an approved disposable offline Linux VM, verified local Engine and Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls. Confirm the local context/builder, reviewed preloaded busybox:1.37 with root default, no entrypoint, and required applets. Record actual digest/platform and use reviewed digest for repeatability. No network egress; --network none is not a complete builder egress control. Verify every bm-v08 image tag and container name in the commands is unused. Use synthetic data only. Run commands individually and stop on unexpected failure; remove only resources created by this lab after ownership checks. If cleanup cannot be confirmed, retire the disposable VM. Expected results are predictions, not observed output. Before contacting any Engine, resolve DOCKER_HOST, DOCKER_CONTEXT and CLI context overrides; also check BUILDX_BUILDER and any builder override. Confirm that the actual builder used by docker build is the approved local docker driver. Review the base configuration for inherited triggers, volumes, health checks, user, shell and required applets; stop if assumptions differ. Make a private working copy containing only the supplied synthetic files. An approved digest may require registry metadata resolution even when layers are present locally; if the offline builder cannot resolve it, stop and record the limitation. Record the actual base image ID, platform, builder version and output image ID; never substitute an unknown digest. Do not retry by enabling egress. Apply owner-approved time and resource budgets and retire the VM if cleanup is uncertain.
Open lab-fixtures/dockerfile-configuration/09-onbuild/ from the extracted archive. Save the complete recipe and any additional files below in that directory.
docker build --load --network none -t bm-v08-onbuild:lab .docker run --rm --pull=never --network none bm-v08-onbuild:labdocker image inspect --format '{{json .Config.OnBuild}}' bm-v08-onbuild:labdocker build --load --network none -f Dockerfile.child -t bm-v08-onbuild-child:lab .docker run --rm --pull=never --network none bm-v08-onbuild-child:labdocker image inspect --format '{{json .Config.OnBuild}}' bm-v08-onbuild-child:labdocker image rm bm-v08-onbuild-child:labExpect base only from the first run, a recorded COPY trigger in the base configuration, child input from the child run, and no inherited pending trigger in the child configuration. Both builds must use the reviewed local builder; if it cannot resolve the local base, stop rather than enabling network egress.
Confirm the tag belongs to this lab and no container depends on it. Remove it without forcing deletion. Build cache may remain until VM disposal. Record output and exit status before removing the exact lab tag. Recheck the tag against the recorded output image ID; a tag may have been reassigned.
Common mistake
ONBUILD does not run every time a container starts. Hidden triggers also increase maintenance complexity. Prefer explicit child instructions when the behavior would otherwise be surprising.
Manual practice
Predict what happens to the child build if its context lacks downstream.txt. Explain how to document this requirement for another developer. Verify both tags are unused before building.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dockerfile ONBUILD: deferred downstream build steps — Knowledge check
Apply the configuration concept to a new image or container scenario.
When is the COPY trigger executed?
Which context supplies downstream.txt?
Does a consumed trigger automatically repeat in a grandchild?