Search code examples
dockerdocker-multi-stage-build

Empty multi-stage build in a Dockerfile


I understand the purpose of using a multi-stage build but I cannot understand why you would like to have an empty stage like this? Can someone please elaborate on the use-case?

# Use an official Ubuntu 18.04 as parent image
FROM ubuntu:18.04

# Install python 2.7
FROM python:2.7

RUN apt-get update
RUN apt-get install -y git make g++
# more stuff, nothing using layer 0.

Source: https://raw.githubusercontent.com/kubeflow/examples/master/xgboost_ames_housing/Dockerfile


Solution

  • The person that wrote that Dockerfile likely doesn't understand how a multi-stage build works. It's not an unusual misunderstanding because a common request with Dockerfiles is to merge multiple images together, and that's not what multiple FROM lines provide.

    What the example does is download an image, and then promptly ignores it. Had there been a COPY command in the later stage, it would be possible to copy from that previous image, which goes to one of the few reasons I've seen to import an image without doing anything with it:

    ARG parent_ver=2.0
    FROM repo:5000/parent:${parent_ver} as parent
    FROM base as release
    COPY --from=parent /some/file /dest
    ...
    

    Normally you could copy directly from another image, but if you want to specify it with a variable like above, the current solution involves the above workaround.

    Since the example provided did none of that, all you will see is an unneeded image downloaded and not used. That slows down the build, takes up bandwidth, and takes up disk space, but only on the build server. It would be worth filling an issue on that GitHub repo to fix their example.