Skip to content

Commit

Permalink
feat: add dockerfile cross-compilation
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <carlos.salas@suse.com>
  • Loading branch information
salasberryfin committed Jan 20, 2025
1 parent 0807e32 commit 4dbced7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
# Build the manager binary
ARG builder_image

FROM ${builder_image} as builder
#FROM ${builder_image} as builder
FROM --platform=$BUILDPLATFORM ${builder_image} as builder
WORKDIR /workspace

# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy
Expand All @@ -30,10 +31,11 @@ COPY ./ ./
# Build
ARG package=.
ARG ldflags
ARG TARGETOS TARGETARCH

# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder
RUN --mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=0 GOOS=linux \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
go build -trimpath -ldflags "${ldflags} -extldflags '-static'" \
-o manager ${package}

Expand All @@ -51,11 +53,12 @@ COPY ./ ./

# Build
ARG ldflags
ARG TARGETOS TARGETARCH

# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=0 GOOS=linux \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
sh -c "cd exp/etcdrestore && ls && go build -trimpath -ldflags \"${ldflags} -extldflags '-static'\" -o manager ${package}"

# Use distroless as minimal base image to package the manager binary
Expand Down

0 comments on commit 4dbced7

Please sign in to comment.