Compiling Go Binaries with Alpine Docker

So, previously, I discussed how to setup a Self Compiling Go Docker Container. One of the issues I came across while doing so was getting reflex compiled correctly for the alpine environment.

I’m quoting directly from this repo:

Because alpine linux and therefor gliderlabs/alpine docker
containers use musl instead gnu libc, your golang binaries
build using libc will not work on alpine.

You can further read up on wikipedia on what musl is. So, how did I compile reflex for alpine? I used the official Go alpine docker image to do the compiling for me.

export WORKDIR=/go/src/path/to/your/project

docker run --rm -v "$PWD":${WORKDIR} -w ${WORKDIR} golang:1.7-alpine go build -v

This will create a binary in your work directory. Once compiled, I can now use that binary to create a much smaller alpine docker image worthy of production use.