Skip to content

Cross-compile golang to linux and darwin

License

Notifications You must be signed in to change notification settings

InjectiveLabs/go-cross

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-cross

docker.injective.dev/go-cross is a Docker image to cross compile golang binaries and plugins to linux/darwin/windows.

It is based off work from docker/cli:

Cross-compiling using docker build

Using docker.injective.dev/go-cross:mod to cache go modules and docker.injective.dev/go-cross:build that has ONBUILD triggers you can define a Dockerfile with only the following:

FROM docker.injective.dev/go-cross:mod AS mod
FROM docker.injective.dev/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

Private Go modules

If you have private go modules, you can use that as long as the following are true:

  • The stage is set to AS mod
  • The go module cache is in /root/.cache/go-build and /root/go/pkg/mod
FROM golang:1.14-alpine AS mod
ENV GOPATH=/root/go
ENV GOCACHE=/root/.cache/go-build
RUN apk add --no-cache git openssh

# Copy or mount in SSH, certificates, etc...

WORKDIR /src
COPY go.mod .
COPY go.sum .
RUN go mod download
RUN touch /mod

FROM docker.injective.dev/go-cross:build
# ensures that dependency on mod stage (1) is properly resolved
COPY --from=mod /mod /mod

About

Cross-compile golang to linux and darwin

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dockerfile 53.3%
  • Shell 30.7%
  • Makefile 16.0%