x1unix / docker-go-mingw

Docker image for building Go binaries with MinGW toolchain

Home Page:https://hub.docker.com/r/x1unix/go-mingw

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

go-mingw

Docker Hub Docker Hub

Docker image for building Go binaries for Windows with MinGW-w64 toolchain based on official Go Docker image.

The repository provides simple cross-compilation environment for windows 32 and 64bit builds.

Usage

You can pull Docker image with desired Go version from Docker Hub:

docker pull x1unix/go-mingw:latest # or "1.17" for specific Go version

Recommended: Please take a look at full project build example before starting to work.

Building Go applications inside container

Mount directory with app source and build it:

docker run --rm -it -v /YourPackageSrc:/go/work \
    -w /go/work \
    x1unix/go-mingw go build .

You will get compiled Windows binary.

For 32-bit toolchain

To build a 32-bit executable, set GOARCH=386 variable:

docker run --rm -it -e GOARCH=386 -v /YourPackageSrc:/go/work \
    -w /go/work \
    x1unix/go-mingw go build .

Recommended: See full project build example here.

Go linker flags override

You can override Go linker flags and other flags by specifying environment variable for a container using -e option.

Example:

docker exec -it
    -e LDFLAGS="-linkmode external -extldflags '-static -s -w'"
    ...

Produced files ownership

By default, the container starts as root user. It means, that all produced files will be owned by root:root user.

To set files to be owned by your current user by default, you need to start the container with your current uid/gid.

Use -u flag to start container with different user/group id.

# Start container as other uid/gid
docker exec --rm -it -u "$UID:$GID" ...

Attention: we recommend to mount your host GOPATH and GOCACHE instead of separated volumes approach when using UID/GID other than root.

Go Build Cache

In order to speed up build times and keep Go build cache, you can mount your Go build cache directory or create a separate Docker volume for it.

Local GOPATH

docker run --rm -it \
    -u $UID \
    -v /YourPackageSrc:/go/work \
    -v $(go env GOCACHE):/go/cache \
    -e GOCACHE=/go/cache \
    -w /go/work \
    x1unix/go-mingw go build .

Volume:

# Create Docker volume
docker volume create go-cache

# Run container with attached volume
docker run --rm -it \
    -v /YourPackageSrc:/go/work \
    -v go-cache:/go/cache \
    -e GOCACHE=/go/cache \
    -w /go/work \
    x1unix/go-mingw go build .

See Docker volumes docs for more info.

Go modules cache

In addition to Go build cache, you may also want to mount Go modules cache to avoid modules re-download on each build.

To do this, mount your GOPATH or Go modules directory ($GOPATH/pkg).

Building custom Docker image

You can build image locally with specified Go version:

make image GO_VERSION=1.17

Replace 1.17 with desired Go version.

About

Docker image for building Go binaries with MinGW toolchain

https://hub.docker.com/r/x1unix/go-mingw

License:MIT License


Languages

Language:Shell 41.7%Language:Go 31.5%Language:Makefile 21.2%Language:Dockerfile 5.6%