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

后端 未结 1 503
礼貌的吻别
礼貌的吻别 2021-02-14 11:57

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-i

相关标签:
1条回答
  • 2021-02-14 12:13

    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.

    0 讨论(0)
提交回复
热议问题