diff --git a/Dockerfile b/Dockerfile index b8ca27d8..0614faef 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; \ + 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..e9372dc2 --- /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 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"]