-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docker multi stage build ignoring intermediate stages #5294
Comments
I'm not a build expert (it's only an upstream to us), but my understanding is that this is the correct behaviour. Stages that can't affect the final image are skipped. |
On Linux I am observing different behavior. It will execute all steps when
using the same Dockerfile. I believe that is the expected behaviour. Anyhow
the behaviour is not the same.
…On Tue, 26 Jan 2021 at 11:51, Stephen Turner ***@***.***> wrote:
I'm not a build expert (it's only an upstream to us), but my understanding
is that this is the correct behaviour. Stages that can't affect the final
image are skipped.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5294 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFNZQBNCGGKD3FABHAQ2BXTS32NB3ANCNFSM4WSGCPCA>
.
|
You might be using the old Docker build on Linux, which I think is still the default, and which had fewer optimisations. Try invoking buildkit explicitly (with something like |
This is the expected behaviour when building using BuildKit; BuildKit will only build stages that are needed for the final image. Buildkit constructs a "dependency tree" for steps in the Dockerfile, which allows skipping steps, but also to build stages in parallel. Having build-stages only built when needed allows you to have a single dockerfile for different scenarios, for example if different steps have to be executed depending on "architecture" (when building multi-arch images), or to have (e.g.) stages to build different artefacts. With this, it's also possible to construct if/else-like Dockerfiles, where passing a build-arg allows you to customise what steps are executed; the example below illustrates how to have different paths for "production" and "debugging"; # build-arg to switch between build-types
ARG BUILD_TYPE=production
FROM busybox AS base
RUN mkdir -p /out
# Example build stage with "debug" enabled
FROM base AS build-debug
RUN echo "debugging enabled" > /out/foo.txt
# Example build stage with "debug" enabled
FROM base AS build-production
RUN echo "production build" > /out/foo.txt
# depending on $BUILD_TYPE, this will either be
# "build-debug" or "build-production" (production is the default)
FROM build-${BUILD_TYPE} AS build
# Common build-stage
FROM base AS common
RUN echo "common succeeded" > /out/bar.txt
FROM base AS final
COPY --from=common /out/bar.txt /out/bar.txt
COPY --from=build /out/foo.txt /out/foo.txt
CMD cat /out/* Building and running the Dockerfile twice (once without build-arg (which uses the default "production"), and once with a build-arg; $ docker build -t prod .
$ docker build -t debug --build-arg BUILD_TYPE=debug .
$ docker run --rm prod
common succeeded
production build
$ docker run --rm debug
common succeeded
debugging enabled More complete examples of these features can be found in the Dockerfile for the docker documentation, and the Dockerfile for the docker daemon (which uses if/else constructs enable stages for cross-compiling, and wether or not to include systemd code used in CI) |
I noticed the new output in the recent release, but didn't realize "unneeded" multi-stage builds would be skipped by default. We use an intermediate layer to install test-only requirements/run tests, and it's completely skipping over that.
Gets me back to match my pipeline that is also breaking, but it's a heck of a change to default to. |
same problem on windows 10, change this default without proper explanation is not exactly a nice move, thanks @adamdavis40208 for pointing out the solution ( on windows just type
before launch your docker multistage build |
Issues go stale after 90 days of inactivity. Prevent issues from auto-closing with an If this issue is safe to close now please do so. Send feedback to Docker Community Slack channels #docker-for-mac or #docker-for-windows. |
/lifecycle frozen |
For me this problem is still open on windows, I think the same on macOs ? |
Closed issues are locked after 30 days of inactivity. If you have found a problem that seems similar to this, please open a new issue. Send feedback to Docker Community Slack channels #docker-for-mac or #docker-for-windows. |
Expected behavior
The code above should run 3 stages. The "RUN echo test" should be visible in the output.
Actual behavior
Middle stage is skipped. Output should show "[test 1/1] RUN echo test", but it does not.
Information
Diagnostic logs
Steps to reproduce the behavior
The text was updated successfully, but these errors were encountered: