Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Dockerfile, do I have to COPY the files from stage "a" to stage "b" if stage "b" is defined as "FROM a AS b"?

I use multi stage Dockerfile with stages build and start, as in:

FROM <whatever> as build

WORKDIR /app

COPY ./a.file ./
COPY ./b.file ./

RUN <build command>

FROM build as start

WORKDIR /app

COPY --from=build /app/a.file ./
COPY --from=build /app/b.file ./

CMD <start command>

I wonder, in start stage, do I have to do COPY --from=build <...files> again?

In other words, does start reuse file system of build? If it does, then I don’t need to copy the files again (since all files necessary for start have already been copied in build). Is that correct?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

No you don’t. In your Dockerfile, the second FROM statement doesn’t do anything at all, since the start stage continues on from the build stage. So you can remove the second FROM statement and your final image will be the same as before.

The reason people use multi-stage Dockerfiles is usually to build the app in the first stage with an image that contains a build environment. Then, after the build is done, you start a new run-time image and copy over the built application.

That way your final image doesn’t contain the complete build environment and you create a smaller final image.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading