From 7e3b838c45a11d617f206359343a1446f84577db Mon Sep 17 00:00:00 2001 From: DarthSim Date: Fri, 11 Feb 2022 18:30:46 +0600 Subject: [PATCH] Use native Go in Docker build --- .dockerignore | 1 - docker/Dockerfile | 5 ++++- docker/go.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100755 docker/go.sh diff --git a/.dockerignore b/.dockerignore index 89298ae2..e393659f 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,7 +5,6 @@ tmp/ imgproxy docs/ -docker/ examples/ LICENSE README.md diff --git a/docker/Dockerfile b/docker/Dockerfile index aa501ebc..39370282 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,8 +2,11 @@ ARG BASE_IMAGE_VERSION="v3.2.1" FROM darthsim/imgproxy-base:${BASE_IMAGE_VERSION} +ARG BUILDPLATFORM +ARG TARGETPLATFORM + COPY . . -RUN ["bash", "-c", "go build -v -o /usr/local/bin/imgproxy"] +RUN docker/go.sh build -v -o /usr/local/bin/imgproxy # ================================================================================================== # Final image diff --git a/docker/go.sh b/docker/go.sh new file mode 100755 index 00000000..60064a57 --- /dev/null +++ b/docker/go.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +set -e + +# This is pretty dirty hack. Building imgproxy under Qemu is pretty slow. +# So we install Go binary native for the BUILDPLATFORM. +if [[ $BUILDPLATFORM != $TARGETPLATFORM ]]; then + case "$BUILDPLATFORM" in + amd64 | "linux/amd64") + BUILD_ARCH="amd64" + ;; + + arm64 | "arm64/v8" | "linux/arm64" | "linux/arm64/v8") + BUILDPLATFORM="arm64" + ;; + + *) + echo "Unknown platform: $BUILDPLATFORM" + exit 1 + esac + + case "$TARGETPLATFORM" in + amd64 | "linux/amd64") + TARGET_ARCH="amd64" + ;; + + arm64 | "arm64/v8" | "linux/arm64" | "linux/arm64/v8") + TARGET_ARCH="arm64" + ;; + + *) + echo "Unknown platform: $TARGETPLATFORM" + exit 1 + esac + + GOLANG_VERSION=$(go version | sed -E 's/.*go([0-9]+\.[0-9]+\.[0-9]+).*/\1/') + + rm -rf /usr/local/go + + dpkg --add-architecture ${BUILD_ARCH} + apt-get update + apt-get install -y --no-install-recommends libstdc++6:${BUILD_ARCH} + + curl -Ls https://golang.org/dl/go${GOLANG_VERSION}.linux-${BUILD_ARCH}.tar.gz \ + | tar -xzC /usr/local + + export CGO_ENABLED=1 + export GOOS=linux + export GOARCH=$TARGET_ARCH +fi + +go $@