docker: multi-stage builds

Multi-stage builds are a new feature requiring Docker 17.05 or higher on the daemon and client. 

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROMinstruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

You only need the single Dockerfile. You don’t need a separate build script, either. Just run:

By default, the stages are not named, and you refer to them by their integer number, starting with 0 for the first FROM instruction. However, you can name your stages, by adding an AS <NAME> to the FROM instruction. This example improves the previous one by naming the stages and using the name in the COPY instruction.

When you build your image, you don’t necessarily need to build the entire Dockerfile including every stage. You can specify a target build stage. The following command assumes you are using the previous Dockerfile but stops at the stage named builder: