Skip to content

Instantly share code, notes, and snippets.

@nilium nilium/Dockerfile
Last active Feb 18, 2019

Embed
What would you like to do?
Dockerfile to cross-compile Go binaries for a target platform and produce a Docker image for the target platform
# Dockerfile to cross-compile Go binaries for a target platform
#
# Usage:
# $ docker build --platform linux/arm .
# Cross-compile package for target platform from build platform
FROM --platform=${BUILDPLATFORM} golang:1-alpine AS build
ADD . /tmp/go
WORKDIR /tmp/go
ARG PACKAGES=.
# TARGETOS and TARGETARCH are set by buildkit
ARG TARGETOS
ARG TARGETARCH
ENV CGO_ENABLED 0
ENV GOOS ${TARGETOS}
ENV GOARCH ${TARGETARCH}
RUN \
for pkg in `go list ${PACKAGES:-.}`; do \
go build -v -o /var/go/bin/`basename $pkg` $pkg; \
done
# Build executable image for target platform
# TARGETPLATFORM is set by buildkit
ARG TARGETPLATFORM
FROM --platform=${TARGETPLATFORM} alpine:3.8
# Copy binaries from build container
COPY --from=build /var/go/bin/* /usr/local/bin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.