title: How do I build a static Go binary for the Docker Alpine image?
body: <p>I want to build a Go 1.9.2 binary and run it on the Docker Alpine image. The Go code I wrote doesn't call any C code. It also uses the <code>net</code> package. Unfortunately it hasn't been as simple as it sounds as Go doesn't seem to quite build static binaries all the time. When I try to execute the binary I often get cryptic messages for why the binary didn't execute. There's quite a bit of information on the internet about this but most of it ends up with people using trial an error to make their binaries work.</p>
  
  <p>So far I have found the following works, however I don't know why, if it is optimal or if it could be simplified.</p>
  
  <pre><code>env GOOS=linux GARCH=amd64 go install -v -a -tags netgo -installsuffix netgo -ldflags "-linkmode external -extldflags -static"
  </code></pre>
  
  <p>What is the canonical way (if it exists) to build a Go binary that will run on the Alpine 3.7 docker image? I am happy to use <code>apk</code> to install packages to the Alpine image if that would make things more efficient/easier. (Believe I need to install <code>ca-certificates</code> anyway.)</p>