I have noticed a common pattern amonst some new utilities such as
terraform: There’s only 1 single executable file to install, and by ‘install’ it can be put anywhere as long as it’s in $PATH. This was before I learned some Golang but it’s easy to find out that the reason behind this pattern is that they are all written in Go.
And in the containers’ realm, the new-ish multi-stage build steps of Docker released in 2017 are super beneficial to Golang containers. A TL;DR example looks like:
- use a 1GB Debian container with all Golang tools and build dependencies to build the Golang executable(
FROM ... ASin the sample ).
- put the executable into a tiny run-time container such as Alpine Linux, resulting in a < 20MB container image(depending on the size of the app obviously) (
COPY --FROMin the sample )
A multi-stage ‘hello world’ Dockerfile looks like:
FROM golang:1.12.5-alpine3.9 as builder
RUN apk update --no-cache && \
apk add git
ADD ./ /app
RUN go build -o golang-test .
RUN addgroup -g 2000 golang && \
adduser -D -u 2000 -G golang golang
COPY --from=builder /app/golang-test .
Note: To be able to use the multi-stage feature, the Docker version has to be > 17.06.