@@ -402,6 +402,64 @@ the image.
402402The ``WORKDIR `` instruction sets the working directory in which
403403the command given by ``CMD `` is executed.
404404
405+ 3.11 ONBUILD
406+ ------------
407+
408+ ``ONBUILD [INSTRUCTION] ``
409+
410+ The ``ONBUILD `` instruction adds to the image a "trigger" instruction to be
411+ executed at a later time, when the image is used as the base for another build.
412+ The trigger will be executed in the context of the downstream build, as if it
413+ had been inserted immediately after the *FROM * instruction in the downstream
414+ Dockerfile.
415+
416+ Any build instruction can be registered as a trigger.
417+
418+ This is useful if you are building an image which will be used as a base to build
419+ other images, for example an application build environment or a daemon which may be
420+ customized with user-specific configuration.
421+
422+ For example, if your image is a reusable python application builder, it will require
423+ application source code to be added in a particular directory, and it might require
424+ a build script to be called *after * that. You can't just call *ADD * and *RUN * now,
425+ because you don't yet have access to the application source code, and it will be
426+ different for each application build. You could simply provide application developers
427+ with a boilerplate Dockerfile to copy-paste into their application, but that is
428+ inefficient, error-prone and difficult to update because it mixes with
429+ application-specific code.
430+
431+ The solution is to use *ONBUILD * to register in advance instructions to run later,
432+ during the next build stage.
433+
434+ Here's how it works:
435+
436+ 1. When it encounters an *ONBUILD * instruction, the builder adds a trigger to
437+ the metadata of the image being built.
438+ The instruction does not otherwise affect the current build.
439+
440+ 2. At the end of the build, a list of all triggers is stored in the image manifest,
441+ under the key *OnBuild *. They can be inspected with *docker inspect *.
442+
443+ 3. Later the image may be used as a base for a new build, using the *FROM * instruction.
444+ As part of processing the *FROM * instruction, the downstream builder looks for *ONBUILD *
445+ triggers, and executes them in the same order they were registered. If any of the
446+ triggers fail, the *FROM * instruction is aborted which in turn causes the build
447+ to fail. If all triggers succeed, the FROM instruction completes and the build
448+ continues as usual.
449+
450+ 4. Triggers are cleared from the final image after being executed. In other words
451+ they are not inherited by "grand-children" builds.
452+
453+ For example you might add something like this:
454+
455+ .. code-block :: bash
456+
457+ [...]
458+ ONBUILD ADD . /app/src
459+ ONBUILD RUN /usr/local/bin/python-build --dir /app/src
460+ [...]
461+
462+
405463 .. _dockerfile_examples :
406464
4074654. Dockerfile Examples
0 commit comments