Build Your Docker Image
Distributed Services with Go — by Travis Jeffery (75 / 84)
👈 Write an Agent Command-Line Interface | TOC | Configure and Deploy Your Service with Helm 👉
Create a Dockerfile with this code:
FROM golang:1.14-alpine AS build
WORKDIR /go/src/proglog
COPY . .
RUN CGO_ENABLED=0 go build -o /go/bin/proglog ./cmd/proglog
FROM scratch
COPY --from=build /go/bin/proglog /bin/proglog
ENTRYPOINT ["/bin/proglog"]
Our Dockerfile uses multistage builds: one stage builds our service and one stage runs it. This makes our Dockerfile easy to read and maintain while keeping our build efficient and the image small.
The build stage uses the golang:1.14-alpine image because we need the Go compiler, our dependencies, and perhaps various system libraries. These take up disk space, and we don’t need them after we have compiled our binary. In the second stage, we use the scratch empty image — the smallest Docker image. We copy our binary into this image, and this is the image we deploy.
You must statically compile your binaries for them to run in the scratch image because it doesn’t contain the system libraries needed to run dynamically linked binaries. That’s why we disable Cgo — the compiler links it dynamically. Using the scratch image helps with thinking of the containers as being immutable. Instead of exec’ing into a container and mutating the image by…