Docker multistage build without copying from previ

2020-06-27 09:16发布

does it have any advantages to use a multistage build in Docker, if you don't copy any files from the previously built image? eg.

FROM some_base_image as base
#Some random commands
RUN mkdir /app
RUN mkdir /app2
RUN mkdir /app3
#ETC

#Second stage starts from first stage
FROM base

#Add some files to image
COPY  foo.txt /app

Does this result in a smaller image or offer any other advantages compared to a non multi-stage version? Or are multi stage builds only useful for preparing some files and then copying those into another base image?

1条回答
乱世女痞
2楼-- · 2020-06-27 09:42

Or are multi stage builds only useful for preparing some files and then copying those into another base image?

This is the main use-case discussed in "Use multi-stage builds"

The main goal is to reduce the number of layers by copying files from one image to another, without including the build environment needed to produce said files.

But, another goal could be not rebuild the entire Dockerfile including every stage.

Then your suggestion (not copying) could still apply.

You can specify a target build stage. The following command assumes you are using the previous Dockerfile but stops at the stage named builder:

$ docker build --target builder -t alexellis2/href-counter:latest .

A few scenarios where this might be very powerful are:

  • Debugging a specific build stage
  • Using a debug stage with all debugging symbols or tools enabled, and a lean production stage
  • Using a testing stage in which your app gets populated with test data, but building for production using a different stage which uses real data
查看更多
登录 后发表回答