From 2ce69222ef271b16939166528eaa87a29d234132 Mon Sep 17 00:00:00 2001 From: Andrew Gouin Date: Mon, 16 Oct 2023 08:33:29 -0600 Subject: [PATCH] Docker image - cross compile (#372) * Docker images for branches * Cross compile * export vars --- .github/workflows/release.yaml | 5 ++++- Dockerfile | 28 +++++++++++++++++++++------- local.Dockerfile | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 local.Dockerfile diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 14c35645..8c97d7c6 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -4,6 +4,8 @@ on: push: tags: - '**' + branches: + - '**' env: REGISTRY: ghcr.io @@ -42,12 +44,13 @@ jobs: # set latest tag for default branch type=raw,value=latest,enable={{is_default_branch}} type=ref,event=tag + type=ref,event=branch - name: Build and push Docker image uses: docker/build-push-action@v3 with: context: . - platforms: linux/amd64 + platforms: linux/amd64,linux/arm64 file: Dockerfile push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/Dockerfile b/Dockerfile index b8ca27d8..f03e29bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,15 @@ -# Build the manager binary -FROM golang:1.20 as builder +FROM --platform=$BUILDPLATFORM golang:1.20-alpine AS builder + +RUN apk add --update --no-cache gcc libc-dev + +ARG TARGETARCH +ARG BUILDARCH + +RUN if [ "${TARGETARCH}" = "arm64" ] && [ "${BUILDARCH}" != "arm64" ]; then \ + wget -c https://musl.cc/aarch64-linux-musl-cross.tgz -O - | tar -xzvv --strip-components 1 -C /usr; \ + elif [ "${TARGETARCH}" = "amd64" ] && [ "${BUILDARCH}" != "amd64" ]; then \ + wget -c https://musl.cc/x86_64-linux-musl-cross.tgz -O - | tar -xzvv --strip-components 1 -C /usr; \ + fi WORKDIR /workspace # Copy the Go Modules manifests @@ -17,12 +27,16 @@ COPY internal/ internal/ ARG VERSION -# Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/strangelove-ventures/cosmos-operator/internal/version.version=$VERSION" -a -o manager . +RUN if [ "${TARGETARCH}" = "arm64" ] && [ "${BUILDARCH}" != "arm64" ]; then \ + export CC=aarch64-linux-musl-gcc CXX=aarch64-linux-musl-g++;\ + elif [ "${TARGETARCH}" = "amd64" ] && [ "${BUILDARCH}" != "amd64" ]; then \ + export CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++; \ + fi; \ + export GOOS=linux GOARCH=$TARGETARCH CGO_ENABLED=1 LDFLAGS='-linkmode external -extldflags "-static"'; \ + go build -ldflags "-X github.com/strangelove-ventures/cosmos-operator/internal/version.version=$VERSION $LDFLAGS" -a -o manager . -# Use distroless as minimal base image to package the manager binary -# Refer to https://github.com/GoogleContainerTools/distroless for more details -FROM gcr.io/distroless/static:nonroot +# Build final image from scratch +FROM scratch LABEL org.opencontainers.image.source=https://github.com/strangelove-ventures/cosmos-operator diff --git a/local.Dockerfile b/local.Dockerfile new file mode 100644 index 00000000..0ef37e31 --- /dev/null +++ b/local.Dockerfile @@ -0,0 +1,33 @@ +FROM golang:1.20-alpine AS builder + +RUN apk add --update --no-cache gcc libc-dev + +WORKDIR /workspace +# Copy the Go Modules manifests +COPY go.mod go.mod +COPY go.sum go.sum +# cache deps before building and copying source so that we don't need to re-download as much +# and so that source changes don't invalidate our downloaded layer +RUN go mod download + +# Copy the go source +COPY *.go . +COPY api/ api/ +COPY controllers/ controllers/ +COPY internal/ internal/ + +ARG VERSION + +RUN export CGO_ENABLED=1 LDFLAGS='-linkmode external -extldflags "-static"'; \ + go build -ldflags "-X github.com/strangelove-ventures/cosmos-operator/internal/version.version=$VERSION $LDFLAGS" -a -o manager . + +# Build final image from scratch +FROM scratch + +LABEL org.opencontainers.image.source=https://github.com/strangelove-ventures/cosmos-operator + +WORKDIR / +COPY --from=builder /workspace/manager . +USER 65532:65532 + +ENTRYPOINT ["/manager"]