Skip to content

Commit 44380d9

Browse files
committed
enable cross-compilation, add xcputranslate
Signed-off-by: Batuhan Apaydın <[email protected]>
1 parent e89567b commit 44380d9

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

Dockerfile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
# Build stage
2+
# syntax = docker/dockerfile:1.4.2@sha256:443aab4ca21183e069e7d8b2dc68006594f40bddf1b15bbd83f5137bd93e80e2
23
ARG goversion
3-
FROM golang:${goversion}-alpine as builder
4-
RUN apk add build-base git mercurial
4+
FROM --platform=${BUILDPLATFORM} golang:${goversion}-alpine as builder
5+
ARG TARGETPLATFORM
6+
RUN apk add build-base curl git mercurial
57
WORKDIR /spire
6-
ADD go.mod go.mod
7-
RUN go mod download
8-
ADD . .
9-
RUN make build
8+
COPY go.* .
9+
# https://go.dev/ref/mod#module-cache
10+
RUN --mount=type=cache,target=/go/pkg/mod go mod download
11+
# 📥 Install xcputranslate for your build architecture
12+
COPY --from=qmcgaw/xcputranslate:v0.8.0 /xcputranslate /usr/local/bin/xcputranslate
13+
RUN --mount=type=bind,target=.,rw \
14+
--mount=type=cache,target=/root/.cache/go-build \
15+
--mount=type=cache,target=/go/pkg/mod \
16+
GOARCH="$(xcputranslate translate -targetplatform ${TARGETPLATFORM} -language golang -field arch)" \
17+
GOARM="$(xcputranslate translate -targetplatform ${TARGETPLATFORM} -language golang -field arm)" \
18+
make build
1019

1120
# Common base
12-
FROM alpine AS spire-base
21+
FROM --platform=${BUILDPLATFORM} alpine AS spire-base
1322
RUN apk --no-cache add dumb-init
1423
RUN apk --no-cache add ca-certificates
1524
RUN mkdir -p /opt/spire/bin

Dockerfile.scratch

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# Build stage
22
ARG goversion
3-
FROM golang:${goversion}-alpine as builder
3+
FROM --platform=${BUILDPLATFORM} golang:${goversion}-alpine as builder
4+
ARG TARGETPLATFORM
45
RUN apk add build-base git mercurial ca-certificates
56
RUN apk add --update gcc musl-dev
67
WORKDIR /spire
7-
ADD go.mod go.mod
8-
RUN go mod download
9-
ADD . .
10-
RUN make build-static
8+
COPY go.* .
9+
# https://go.dev/ref/mod#module-cache
10+
RUN --mount=type=cache,target=/go/pkg/mod go mod download
11+
# 📥 Install xcputranslate for your build architecture
12+
COPY --from=qmcgaw/xcputranslate:v0.8.0 /xcputranslate /usr/local/bin/xcputranslate
13+
RUN --mount=type=bind,target=.,rw \
14+
--mount=type=cache,target=/root/.cache/go-build \
15+
--mount=type=cache,target=/go/pkg/mod \
16+
GOARCH="$(xcputranslate translate -targetplatform ${TARGETPLATFORM} -language golang -field arch)" \
17+
GOARM="$(xcputranslate translate -targetplatform ${TARGETPLATFORM} -language golang -field arm)" \
18+
make build-static
1119

1220
# SPIRE Server
1321
FROM scratch AS spire-server-scratch

Dockerfile.static

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
# Build stage
2+
# syntax = docker/dockerfile:1.4.2@sha256:443aab4ca21183e069e7d8b2dc68006594f40bddf1b15bbd83f5137bd93e80e2
23
ARG goversion
3-
FROM cgr.dev/chainguard/go:${goversion}-r0 as builder
4+
FROM --platform=${BUILDPLATFORM} golang:${goversion}-alpine as builder
5+
ARG TARGETPLATFORM
6+
RUN apk add build-base curl git mercurial
47
WORKDIR /spire
58
ENV CGO_ENABLED=1
6-
ADD go.mod go.mod
7-
RUN go mod download
8-
ADD . .
9-
RUN go build -ldflags '-s -w -linkmode external -extldflags "-static"' -o ./bin/spire-server ./cmd/spire-server
10-
RUN go build -ldflags '-s -w -linkmode external -extldflags "-static"' -o ./bin/spire-agent ./cmd/spire-agent
11-
RUN go build -ldflags '-s -w -linkmode external -extldflags "-static"' -o ./bin/k8s-workload-registrar ./support/k8s/k8s-workload-registrar
12-
RUN go build -ldflags '-s -w -linkmode external -extldflags "-static"' -o ./bin/oidc-discovery-provider ./support/oidc-discovery-provider
9+
COPY go.* .
10+
# https://go.dev/ref/mod#module-cache
11+
RUN --mount=type=cache,target=/go/pkg/mod go mod download
12+
# 📥 Install xcputranslate for your build architecture
13+
COPY --from=qmcgaw/xcputranslate:v0.8.0 /xcputranslate /usr/local/bin/xcputranslate
14+
RUN --mount=type=bind,target=.,rw \
15+
--mount=type=cache,target=/root/.cache/go-build \
16+
--mount=type=cache,target=/go/pkg/mod \
17+
GOARCH="$(xcputranslate translate -targetplatform ${TARGETPLATFORM} -language golang -field arch)" \
18+
GOARM="$(xcputranslate translate -targetplatform ${TARGETPLATFORM} -language golang -field arm)" \
19+
make build-static
1320

1421
# SPIRE Server
15-
FROM cgr.dev/chainguard/static:latest AS spire-server-static
22+
FROM --platform=${BUILDPLATFORM} cgr.dev/chainguard/static:latest AS spire-server-static
1623
WORKDIR /opt/spire
1724
COPY --from=builder /spire/bin/spire-server bin/spire-server
1825
ENTRYPOINT ["/opt/spire/bin/spire-server", "run"]
1926
CMD []
2027

21-
FROM cgr.dev/chainguard/static:latest AS spire-agent-static
28+
FROM --platform=${BUILDPLATFORM} cgr.dev/chainguard/static:latest AS spire-agent-static
2229
WORKDIR /opt/spire
2330
COPY --from=builder /spire/bin/spire-agent bin/spire-agent
2431
EXPOSE 8080 8443
2532
ENTRYPOINT ["/opt/spire/bin/spire-agent", "run"]
2633
CMD []
2734

2835
# K8S Workload Registrar
29-
FROM cgr.dev/chainguard/static:latest AS k8s-workload-registrar-static
36+
FROM --platform=${BUILDPLATFORM} cgr.dev/chainguard/static:latest AS k8s-workload-registrar-static
3037
WORKDIR /opt/spire
3138
COPY --from=builder /spire/bin/k8s-workload-registrar bin/k8s-workload-registrar
3239
ENTRYPOINT ["/opt/spire/bin/k8s-workload-registrar"]
3340
CMD []
3441

3542
# OIDC Discovery Provider
36-
FROM cgr.dev/chainguard/static:latest AS oidc-discovery-provider-static
43+
FROM --platform=${BUILDPLATFORM} cgr.dev/chainguard/static:latest AS oidc-discovery-provider-static
3744
WORKDIR /opt/spire
3845
COPY --from=builder /spire/bin/oidc-discovery-provider bin/oidc-discovery-provider
3946
ENTRYPOINT ["/opt/spire/bin/oidc-discovery-provider"]

0 commit comments

Comments
 (0)