<!doctype html><metacharset=utf-8><metaname=viewportcontent="width=device-width,initial-scale=1"><linkrel=preconnecthref=https://fonts.googleapis.com><linkrel=preconnecthref=https://fonts.gstatic.comcrossorigin><linkhref="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap"rel=stylesheet><linkhref=https://haunt98.github.io/iosevka_webfont/iosevka-term-ss08/iosevka-term-ss08.cssrel=stylesheet><linkrel=stylesheethref=styles.css><ahref=index>Index</a><h1>Dockerfile for Go</h1><p>Each time I start a new Go project, I repeat many steps.<br>Like set up <code>.gitignore</code>, CI configs, Dockerfile, ...<p>So I decide to have a baseline Dockerfile like this:<pre><codeclass=language-Dockerfile>FROM golang:1.19-bullseye as builder
</code></pre><p>I use <ahref=https://docs.docker.com/develop/develop-images/multistage-build/>multi-stage build</a> to keep my image size small.<br>First stage is <ahref=https://hub.docker.com/_/golang>Go official image</a>,<br>second stage is <ahref=https://github.com/GoogleContainerTools/distroless>Distroless</a>.<p>Before Distroless, I use <ahref=https://hub.docker.com/_/alpine>Alpine official image</a>,<br>There is a whole discussion on the Internet to choose which is the best base image for Go.<br>After reading some blogs, I discover Distroless as a small and secure base image.<br>So I stick with it for a while.<p>Also, remember to match Distroless Debian version with Go official image Debian version.<pre><codeclass=language-Dockerfile>FROM golang:1.19-bullseye as builder
</code></pre><p>This is Go image I use as a build stage.<br>This can be official Go image or custom image is required in some companies.<pre><codeclass=language-Dockerfile>RUN go install golang.org/dl/go1.19@latest \
</code></pre><p>This is optional.<br>In my case, my company is slow to update Go image so I use this trick to install latest Go version.<pre><codeclass=language-Dockerfile>WORKDIR /build
</code></pre><p>I use <code>/build</code> to emphasize that I am building something in that directory.<p>The 4 <code>COPY</code> lines are familiar if you use Go enough.<br>First is <code>go.mod</code> and <code>go.sum</code> because it defines Go modules.<br>The second is <code>vendor</code>, this is optional but I use it because I don't want each time I build Dockerfile, I need to redownload Go modules.<pre><codeclass=language-Dockerfile>RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath .
</code></pre><p>This is where I build Go program.<p><code>CGO_ENABLED=0</code> because I don't want to mess with C libraries.<br><code>GOOS=linux GOARCH=amd64</code> is easy to explain, Linux with x86-64.<br><code>GOAMD64=v3</code> is new since <ahref=https://go.dev/doc/go1.18#amd64>Go 1.18</a>,<br>I use v3 because I read about AMD64 version in <ahref=https://gitlab.archlinux.org/archlinux/rfcs/-/blob/master/rfcs/0002-march.rst>Arch Linux rfcs</a>. TLDR's newer computers are already x86-64-v3.<p><code>-tags timetzdata</code> to embed timezone database incase base image does not have.<br><code>-trimpath</code> to support reproduce build.<pre><codeclass=language-Dockerfile>FROM gcr.io/distroless/base-debian11
</code></pre><p>Finally, I copy <code>app</code> to Distroless base image.</p><ahref=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>