golang:1.8-onbuild if you need to compile your go code image INSIDE a container. That's typically useful for CI builds. Otherwise avoid since it's a massive image.
A much much faster solution is to build your Go application locally (dev env for instance) and copy the final Go application to a very lightweight container.
I'll give you our standard process.
If your local machine is a Mac or Windows, you need to
cross-compileyour Go code for linux using:
GOOS=liux GOARCH=amd64 go build -o myapp_linux-amd64. The
linux-amd64is just a convention to remind yourself that the file is compiled for linux, not mac or windows.
We also deploy our Go apps to the very lightweight Alpine linux container. Alpine is now the standard Docker image to create app. It's very small and secure but it has one major quirk; it is using the musc instead of the more common glibc as the underlying OS/IO library, so we need a few more compilation flags:
-a -ldflags '-w -extldflags "-static"'
As an extra, we also remove the developer's own path in the filename listed in a stacktrace using:
The resulting compile command that we use is:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOROOT_FINAL=$(pwd) go build -a -ldflags '-w -extldflags "-static"' -gcflags=-trimpath=$(pwd) -asmflags=-trimpath=$(pwd) -o myapp_linux-amd64
You can now build your app locally on your dev env and create the image using the following Dockerfile:
FROM alpine:3.6 COPY ./myapp_linux-amd64 /usr/local/bin/myapp ENTRYPOINT  CMD /usr/local/bin/myapp
build it using:
docker build -t myimagename:tag .