Skip to content

Commit

Permalink
Cross compile
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Oct 14, 2023
1 parent 123fca5 commit aad67d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
28 changes: 21 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
33 changes: 33 additions & 0 deletions local.Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

0 comments on commit aad67d0

Please sign in to comment.