Should ONBUILD in unreferenced stage be triggered?

If a multi-stage build has a FROM instruction with an image that includes ONBUILD instructions, but makes no further reference to that stage, should the ONBUILDs be executed? In Docker 20 they were, but in Docker 24 they are not.

Example:

a.Dockerfile:

FROM debian
ONBUILD RUN echo "the ONBUILD command is being run"

b.Dockerfile:

FROM a AS a
FROM debian

Run: docker build -f a.Dockerfile -t a .; docker build -f b.Dockerfile -t b .

In Docker 20, the ONBUILD trigger from a.Dockerfile is run, and "the ONBUILD command is being run" is printed.

In Docker 24, the ONBUILD trigger is never run.

It wouldn’t make sense running the instructions in “ONBUILD”, when the stage is not used. It would be like having two Dockerfiles in a project folder and when you use one to build an image, Docker would build the other one too. SO I don’t know if it was an intentional fix or the developers just didn’t make the same mistake, but the behavior in newer Docker versions seems right.

When you choose a target in a multistage build, it and it’s dependencies should be built only. When there is no specified target, the last stage will be the chosen one.

One difference between Docker 20.10 and 24.0 is that 24.0 uses buildx by default so it is indeed a newer and a faster builder.