Search code examples
dockerdockerfile

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?


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.