Skip to content

Commit

Permalink
feat: add dockerfile cross-compilation for multi-platform images
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <[email protected]>
  • Loading branch information
salasberryfin committed Jan 21, 2025
1 parent 0450b5f commit fbf4894
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Build the manager binary
ARG builder_image

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,15 +30,16 @@ 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}


FROM ${builder_image} as etcd-snapshot-restore-builder
FROM --platform=$BUILDPLATFORM ${builder_image} as etcd-snapshot-restore-builder
WORKDIR /workspace

# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy
Expand All @@ -51,11 +52,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 fbf4894

Please sign in to comment.