Skip to content

Commit

Permalink
Support cross-building
Browse files Browse the repository at this point in the history
- Specify --platform in the golang build stage
- Set GOOS and GOARCH env vars
- Set CGO_ENABLED=0 to build static binaries
- Remove apt-get upgrades; changed base image to chainguard/static

I don't think we need buildx?
  • Loading branch information
axw committed Mar 28, 2024
1 parent 6103e4a commit 0ef952f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
23 changes: 8 additions & 15 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Base image for build
ARG base_image_version=1.20.7
FROM golang:${base_image_version} as builder
FROM --platform=$BUILDPLATFORM golang:${base_image_version} as builder
ARG TARGETARCH
ARG TARGETOS

# Switch workdir
WORKDIR /opt/apm-perf
Expand All @@ -9,12 +11,15 @@ WORKDIR /opt/apm-perf
COPY . .

# Build
ENV GOOS=$TARGETOS
ENV GOARCH=$TARGETARCH
ENV CGO_ENABLED=0
RUN \
go mod download \
&& make build

# Base image for build
FROM debian:bookworm
# Base image for final image
FROM cgr.dev/chainguard/static

# Arguments
ARG commit_sha
Expand All @@ -37,18 +42,6 @@ LABEL \
# Switch workdir
WORKDIR /opt/apm-perf

# Add minimal stuff
RUN \
apt-get update > /dev/null \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends \
"apt-utils=*" \
"ca-certificates=*" \
"curl=*" \
"gnupg=*" \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Copy files for apmsoak
COPY --from=builder /opt/apm-perf/dist/apmsoak /usr/bin/apmsoak
COPY ./internal/loadgen/events ./events
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ package: PROJECT_URL=$$(go list -m all | head -1)
package:
mkdir -p $(STATES_DIR)
echo "$(IMAGE_REF)" > "$(STATES_DIR)/image_ref"
docker buildx build \
docker build \
--build-arg base_image_version=$(BASE_IMAGE_VERSION) \
--build-arg commit_sha=$(COMMIT_SHA) \
--build-arg current_time=$(CURRENT_TIME_ISO) \
Expand Down

0 comments on commit 0ef952f

Please sign in to comment.