Skip to content

Commit

Permalink
feat: build multi-architecture images in by default
Browse files Browse the repository at this point in the history
This feature utilizes cross-compiling to generate binary files for
the amd64, s390x and arm64 architectures, followed by the creation
of images.

Signed-off-by: Nestor Acuna-Blanco <nestor.acuna@ibm.com>
  • Loading branch information
nestoracunablanco authored and Nestor Acuna Blanco committed Jul 22, 2024
1 parent ae537f8 commit db0c550
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
35 changes: 21 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
FROM registry.access.redhat.com/ubi8/ubi-minimal as builder

# create enviroment variables and files
ENV ENVFILE=/tmp/envfile
RUN echo "export ARCH=`uname -m | sed 's/x86_64/amd64/'`" >> ${ENVFILE}
ENV PATH=$PATH:/usr/local/go/bin
ARG TARGET_ARCH=amd64
FROM registry.access.redhat.com/ubi8/ubi-minimal as base
ARG TARGET_ARCH

# download packages
RUN microdnf install -y make tar gzip which && microdnf clean all
RUN export ARCH=$(uname -m | sed 's/x86_64/amd64/'); curl -L https://go.dev/dl/go1.22.4.linux-${ARCH}.tar.gz | tar -C /usr/local -xzf -


FROM base as builder

RUN . ${ENVFILE}; curl -L https://go.dev/dl/go1.22.4.linux-${ARCH}.tar.gz | tar -C /usr/local -xzf -
# in multistage builds the ARG must be renewed
ARG TARGET_ARCH

# create enviroment variables
ENV PATH=$PATH:/usr/local/go/bin

# copy the Go Modules manifests and vendor directory
WORKDIR /workspace
# Copy the Go Modules manifests and vendor directory
COPY go.mod go.mod
COPY go.sum go.sum
COPY vendor/ vendor/

# Copy the go source
# copy the go source
COPY Makefile Makefile
COPY main.go main.go
COPY api/ api/
COPY pkg/ pkg/

# Build
RUN . ${ENVFILE}; CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=on make build
# compile for the TARGET_ARCH
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGET_ARCH} make build


FROM registry.access.redhat.com/ubi8/ubi-minimal
FROM --platform=linux/${TARGET_ARCH} registry.access.redhat.com/ubi8/ubi-micro

RUN microdnf update -y && microdnf clean all
# in multistage builds the ARG must be renewed
ARG TARGET_ARCH

# copy binary from builder image
WORKDIR /
COPY --from=builder /workspace/bin/console .
USER 1000

ENTRYPOINT ["/console"]
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ build: fmt vet

.PHONY: build-container
build-container: fmt vet test
podman build -t ${IMG} .
podman manifest rm ${IMG} || true && \
podman build --build-arg TARGET_ARCH=amd64 --manifest=${IMG} . && \
podman build --build-arg TARGET_ARCH=s390x --manifest=${IMG} . && \
podman build --build-arg TARGET_ARCH=arm64 --manifest=${IMG} .

.PHONY: push-container
push-container:
podman push ${IMG}
podman manifest push ${IMG}

.PHONY: manifests
manifests:
Expand Down

0 comments on commit db0c550

Please sign in to comment.