From 75092bf0bc83ae183de4ed7e8a837b2ac755aeac Mon Sep 17 00:00:00 2001 From: Lavi Blumberg Date: Wed, 19 Apr 2023 18:15:00 -0400 Subject: [PATCH] Build correct arch for container image (#817) Signed-off-by: Lavi Blumberg --- Dockerfile | 10 ++++++---- Dockerfile.debian-stable-slim | 10 ++++++---- Dockerfile.ubuntu | 10 ++++++---- Makefile | 8 ++++++++ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index a95f73b3..e0818d27 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine as builder +FROM --platform=$BUILDPLATFORM golang:1.20-alpine as builder RUN apk add --no-cache make git WORKDIR /workspace/helmfile @@ -7,11 +7,12 @@ COPY go.mod go.sum /workspace/helmfile/ RUN go mod download COPY . /workspace/helmfile -RUN make static-linux +ARG TARGETARCH +RUN make static-linux-${TARGETARCH} # ----------------------------------------------------------------------------- -FROM alpine:3.16 +FROM --platform=$BUILDPLATFORM alpine:3.16 LABEL org.opencontainers.image.source https://github.com/helmfile/helmfile @@ -89,6 +90,7 @@ RUN helm plugin install https://github.com/databus23/helm-diff --version v3.6.0 # Allow users other than root to use helm plugins located in root home RUN chmod 751 ${HOME} -COPY --from=builder /workspace/helmfile/dist/helmfile_linux_amd64 /usr/local/bin/helmfile +ARG TARGETARCH +COPY --from=builder /workspace/helmfile/dist/helmfile_linux_${TARGETARCH} /usr/local/bin/helmfile CMD ["/usr/local/bin/helmfile"] diff --git a/Dockerfile.debian-stable-slim b/Dockerfile.debian-stable-slim index e85203a2..d7cfbf8e 100644 --- a/Dockerfile.debian-stable-slim +++ b/Dockerfile.debian-stable-slim @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine as builder +FROM --platform=$BUILDPLATFORM golang:1.20-alpine as builder RUN apk add --no-cache make git WORKDIR /workspace/helmfile @@ -7,11 +7,12 @@ COPY go.mod go.sum /workspace/helmfile/ RUN go mod download COPY . /workspace/helmfile -RUN make static-linux +ARG TARGETARCH +RUN make static-linux-${TARGETARCH} # ----------------------------------------------------------------------------- -FROM debian:stable-slim +FROM --platform=$BUILDPLATFORM debian:stable-slim LABEL org.opencontainers.image.source https://github.com/helmfile/helmfile @@ -94,6 +95,7 @@ RUN helm plugin install https://github.com/databus23/helm-diff --version v3.6.0 # Allow users other than root to use helm plugins located in root home RUN chmod 751 ${HOME} -COPY --from=builder /workspace/helmfile/dist/helmfile_linux_amd64 /usr/local/bin/helmfile +ARG TARGETARCH +COPY --from=builder /workspace/helmfile/dist/helmfile_linux_${TARGETARCH} /usr/local/bin/helmfile CMD ["/usr/local/bin/helmfile"] diff --git a/Dockerfile.ubuntu b/Dockerfile.ubuntu index acd4fbf0..e6c2e015 100644 --- a/Dockerfile.ubuntu +++ b/Dockerfile.ubuntu @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine as builder +FROM --platform=$BUILDPLATFORM golang:1.20-alpine as builder RUN apk add --no-cache make git WORKDIR /workspace/helmfile @@ -7,11 +7,12 @@ COPY go.mod go.sum /workspace/helmfile/ RUN go mod download COPY . /workspace/helmfile -RUN make static-linux +ARG TARGETARCH +RUN make static-linux-${TARGETARCH} # ----------------------------------------------------------------------------- -FROM ubuntu:20.04 +FROM --platform=$BUILDPLATFORM ubuntu:20.04 LABEL org.opencontainers.image.source https://github.com/helmfile/helmfile @@ -94,6 +95,7 @@ RUN helm plugin install https://github.com/databus23/helm-diff --version v3.6.0 # Allow users other than root to use helm plugins located in root home RUN chmod 751 ${HOME} -COPY --from=builder /workspace/helmfile/dist/helmfile_linux_amd64 /usr/local/bin/helmfile +ARG TARGETARCH +COPY --from=builder /workspace/helmfile/dist/helmfile_linux_${TARGETARCH} /usr/local/bin/helmfile CMD ["/usr/local/bin/helmfile"] diff --git a/Makefile b/Makefile index f162687c..0500bd33 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,14 @@ static-linux: env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOFLAGS=-mod=readonly go build -o "dist/helmfile_linux_amd64" -ldflags="$(GO_BUILD_VERSION_LDFLAGS)" ${TARGETS} .PHONY: static-linux +static-linux-amd64: + env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOFLAGS=-mod=readonly go build -o "dist/helmfile_linux_amd64" -ldflags="$(GO_BUILD_VERSION_LDFLAGS)" ${TARGETS} +.PHONY: static-linux-amd64 + +static-linux-arm64: + env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GOFLAGS=-mod=readonly go build -o "dist/helmfile_linux_arm64" -ldflags="$(GO_BUILD_VERSION_LDFLAGS)" ${TARGETS} +.PHONY: static-linux-arm64 + install: env CGO_ENABLED=0 go install -ldflags="$(GO_BUILD_VERSION_LDFLAGS)" ${TARGETS} .PHONY: install