Using a multi-Stage Dockerfile
Use a multi-stage Dockerfile to dockerize multi-git and create a Docker image that can be run on any system that has Docker support.
We'll cover the following...
Using a multi-stage Dockerfile
One of the main benefits of Docker is that it can potentially create very small images, which require less resources and are more secure due to the small surface area. In the previous section, you saw that we can build a 10MB multi-git executable. However, building it requires a lot of foundation. Here is a simple Dockerfile based on the golang:1.14 base image that builds multi-git. I saved it as Dockerfile.big
:
Press + to interact
FROM golang:1.14WORKDIR /buildADD ./main.go main.goADD ./go.mod go.mod### Fetch dependenciesRUN go mod download### Build image as a truly static Go binaryRUN CGO_ENABLED=0 GOOS=linux go build -o /multi-git -a -tags netgo -ldflags '-s -w' .ENTRYPOINT ["/multi-git"]
Let’s use it to build it a Docker image:
$ docker build . -f Dockerfile.big -t multi-git.big
Sending build context to Docker daemon 22.39MB
Step 1/9 : FROM golang:1.14
---> 25c4671a1478
Step 2/9 : WORKDIR /build
---> Using cache
---> 8b8e998005bc
Step 3/9 : ADD main.go main.go
---> Using cache
---> 69d9863aa86a
Step 4/9 : ADD go.mod go.mod
--->
...