Golang cross-compiling Docker container
Creating cross-compiled binaries for Go projects is nothing new, and many projects build binaries for more than just linux/amd64
, great. Some even build binaries for openbsd/amd64
, even better.
At the time of writing there’s no Go 1.5 port for OpenBSD yet, however there are some projects which requires Go >= 1.5. E.g. filebeat.
So I made a simple Docker image which provides a cross-compiler for any supported Go target, but defaulting to openbsd/amd64
: jasperla/go-cross.
Using it easy:
docker run -ti --rm -e APPNAME=myapp -e GOLANG_TARGET_PLATFORM="openbsd/amd64" \
-v "$PWD:/go/src/app" jasperla/docker-go-cross
$APPNAME
and $GOLANG_TARGET_PLATFORM
are optional, defaulting to myapp
and openbsd/amd64
respectively.
So to build a fresh filebeat binary:
git clone https://github.com/elastic/filebeat.git
docker run -ti --rm -e APPNAME=filebeat \
-v "$PWD/filebeat:/go/src/app" jasperla/docker-go-cross
and a little while laters there’s a fresh filebeat-openbsd-amd64
ready and waiting.
Currently the container builds the needed toolchain when created, which does add to the run time, but allows for greater flexibility.
update: I’ve had to rename the image to jasperla/docker-go-cross
in order to make it an automated build on the Docker Hub.