Docker Multi-Stage: How to split up into multiple Dockerfiles

天涯浪子 提交于 2020-12-01 09:42:28

问题


I am successfully using Docker's Multi-Stage feature to build some code and then copy the resulting artifacts into a final image.

Is it possible to split this one big-ish Dockerfile into multiple files?

I would like to improve the readability of the individual stages. Which will become more important when more stages are added.

EDIT: I am aware that I could write a Makefile (or similar) where I first build an image named "myproject-stage1" and then use FROM myproject-stage1 AS build. However, I'd rather avoid the external build tool if possible.


回答1:


If your Dockerfile currently looks something like:

FROM ubuntu:16.04 AS builder
RUN apt-get install build-essential
COPY . ./
RUN ./build_all_the_things \
 && make install PREFIX=/usr DESTDIR/out

FROM alpine:3.8
COPY --from=builder /out/ /
CMD the_app

You can readily split out the first part into its own Dockerfile, as is, and build it

docker build -f Dockerfile.builder -t me/builder .

COPY --from has to name some previous stage in your Dockerfile, but the stage doesn’t need to do much; you can then change the second half of this to

FROM me/builder AS builder
FROM alpine:3.8
COPY --from=builder /out/ /
CMD the_app

The big downside of this is that you need two separate docker build commands to actually build the final image. You could write a shell script to do both of them together.



来源:https://stackoverflow.com/questions/53659993/docker-multi-stage-how-to-split-up-into-multiple-dockerfiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!