Can we Inline COPY --from command in multistage build dockerfile?

82 Views Asked by At

I have a dockerfile which has following stages/layers

COPY --from=build /usr/local /usr/local
COPY --from=build /home/platform /home/platform
COPY --from=build /usr/lib /usr/lib

here: build is the name of first stage and platform is just the code files.

Is it possible to inline all these copy statements in one so that layer count can be lowered ?

Already tried

COPY README.md package.json gulpfile.js __BUILD_NUMBER ./

This seems to be working only for copying in single folder.

I want Something like

RUN --from build cp -r /usr/local /usr/local/ && \        
    cp -r /home/platform /home/platform/ && \                                      
    cp -r /usr/lib /usr/lib/ \

Let me know if there is anything confusing. Thanks in Advance

1

There are 1 best solutions below

0
Hans Kilian On

If you really want to minimize the number of layers, you can copy your final image into a new, 'really final' image based on scratch.

Something like

FROM ... as build
...
FROM ... as final
COPY --from=build /usr/local /usr/local
COPY --from=build /home/platform /home/platform
COPY --from=build /usr/lib /usr/lib

FROM scratch as reallyfinal
COPY --from=final / /

You'll need to set up any ENTRYPOINT, CMD, ENV etc. in the reallyfinal image.

Depending on your use-case it might be worth it or not. Personally, I don't think it's worth it if your only goal is to minimize the number of layers.