Multiple RUN vs. single chained RUN in Dockerfile, what is better?

后端 未结 4 1464
轻奢々
轻奢々 2020-11-29 16:16

Dockerfile.1 executes multiple RUN:

FROM busybox
RUN echo This is the A > a
RUN echo This is the B > b
RUN echo This is the C         


        
4条回答
  •  日久生厌
    2020-11-29 17:01

    It seems the answers above are outdated. The docs note:

    Prior to Docker 17.05, and even more, prior to Docker 1.10, it was important to minimize the number of layers in your image. The following improvements have mitigated this need:

    [...]

    Docker 17.05 and higher add support for multi-stage builds, which allow you to copy only the artifacts you need into the final image. This allows you to include tools and debug information in your intermediate build stages without increasing the size of the final image.

    https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#minimize-the-number-of-layers

    and

    Notice that this example also artificially compresses two RUN commands together using the Bash && operator, to avoid creating an additional layer in the image. This is failure-prone and hard to maintain.

    https://docs.docker.com/engine/userguide/eng-image/multistage-build/

    Best practice seems to have changed to using multistage builds and keeping the Dockerfiles readable.

提交回复
热议问题