Multi-stage Dockerfile for Golang application

A common workaround for building Golang application in Docker is to have 2 Dockerfiles - one to perform a build and another to ship the results of the first build without tooling in the first image. It called Builder Pattern.
Starting from Docker v17.0.5 it will be possible to do it via single Dockerfile using multi-stage builds.
Application
Let’s start with “Hello world” application:
|
|
Single Dockerfile
With multi-stage builds, a Dockerfile allows multiple FROM directives, and the image is created via the last FROM directive of the Dockerfile.
COPY βfrom=0 takes the file app from the previous stage and copies it to the WORKDIR. This basically copies the compiled go binary created from the previous stage.
The --from flag uses a zero-based index for the stage. You either reference stages by using offsets (like --from=0) or by using names. To name a stage use the syntax FROM [image] as [name].
|
|
Build and check size
|
|
Container size now is small, because it contains only binary file.
|
|
Conclusion
Once the feature is released I would switch over. But for now we can use Builder Pattern as a workaround.