Cross-compile golang to linux and darwin
1.9K
hinshun/go-cross is a Docker image to cross compile golang binaries and plugins to linux/darwin. It is based off work from docker/cli:
Using hinshun/go-cross:mod to cache go modules and hinshun/go-cross:build that has ONBUILD triggers you can define a Dockerfile with only the following:
FROM hinshun/go-cross:mod AS mod
FROM hinshun/go-cross:build
For example, if my project looks like this:
❯ tree
.
├── cmd
│ └── program
│ └── main.go
├── Dockerfile
├── go.mod
└── go.sum
Then you can cross-compile the program like so (BUILDMODE and LDFLAGS are optional):
docker build -t my-cross-compile --build-arg PKG="./cmd/program" --build-arg BUILDMODE="default" --build-arg LDFLAGS="" .
Extract the binaries by creating a temporary container and docker cp them out.
mkdir bin
docker create --name tmp my-cross-compile bash
docker cp tmp:/root/go/bin/. bin
docker rm tmp
If you have private go modules, you can use that as long as the following are true:
AS mod/root/.cache/go-build and /root/go/pkg/modFROM golang:1.11-alpine AS mod
ENV GOPATH=/root/go
ENV GOCACHE=/root/.cache/go-build
# Copy or mount in SSH, certificates, etc...
RUN go mod download
FROM hinshun/go-cross:build
Content type
Image
Digest
Size
687.4 MB
Last updated
almost 6 years ago
docker pull hinshun/go-cross