Skip to content

Build multi-arch images locally, including ARM and RISC-V #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-and-publish-container-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

env:
CONTAINER_REGISTRY: docker.io
CONTAINER_IMAGE: paulbouwer/hello-kubernetes
CONTAINER_IMAGE: ${{ github.repository }}

steps:

Expand Down
38 changes: 29 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,46 @@ IMAGE_VERSION ?= $(shell cat src/app/package.json | jq -r .version)
IMAGE_MAJOR_VERSION = $(shell echo "$(IMAGE_VERSION)" | cut -d '.' -f1 )
IMAGE_MINOR_VERSION = $(shell echo "$(IMAGE_VERSION)" | cut -d '.' -f2 )
IMAGE = $(REGISTRY)/$(REPOSITORY)/hello-kubernetes
BUILD_ARCH ?= amd64 arm arm64 riscv64
BUILD_IMAGE_LIST:=$(shell echo $(BUILD_ARCH) | sed -e "s~[^ ]*~$(IMAGE):$(IMAGE_VERSION)\-&~g")

.PHONY: scan-for-vulns
scan-for-vulns:
trivy image --format template --template "@/trivy/contrib/sarif.tpl" $(IMAGE):$(IMAGE_VERSION)
for arch in $(BUILD_ARCH); do \
if [ "$${arch}" = $$(uname -m) ]; then \
trivy image --format template --template "@/trivy/contrib/sarif.tpl" $(IMAGE):$(IMAGE_VERSION)-$${arch}; \
fi; \
done

.PHONY: build-images
build-images: build-image-linux

.PHONY: build-image-linux
build-image-linux:
docker build --no-cache \
build-image-linux:
cp Dockerfile build/docker/$${arch} ; \
DOCKER_ARGS=""; \
for arch in $(BUILD_ARCH); do \
if [ "$${arch}" = "riscv64" ]; then \
DOCKER_ARGS="--build-arg=BASE=riscv64/alpine:edge"; \
fi; \
DOCKER_BUILDKIT=1 \
docker build --no-cache \
--platform $${arch} \
--build-arg IMAGE_VERSION="$(IMAGE_VERSION)" \
--build-arg IMAGE_CREATE_DATE="`date -u +"%Y-%m-%dT%H:%M:%SZ"`" \
--build-arg IMAGE_SOURCE_REVISION="`git rev-parse HEAD`" \
-f src/app/Dockerfile -t "$(IMAGE):$(IMAGE_VERSION)" src/app;
$${DOCKER_ARGS} \
-f src/app/Dockerfile -t "$(IMAGE):$(IMAGE_VERSION)-$${arch}" src/app; \
done

.PHONY: push-image
push-image:
docker tag $(IMAGE):$(IMAGE_VERSION) $(IMAGE):$(IMAGE_MAJOR_VERSION); \
docker tag $(IMAGE):$(IMAGE_VERSION) $(IMAGE):$(IMAGE_MAJOR_VERSION).$(IMAGE_MINOR_VERSION); \
docker push $(IMAGE):$(IMAGE_VERSION); \
docker push $(IMAGE):$(IMAGE_MAJOR_VERSION); \
docker push $(IMAGE):$(IMAGE_MAJOR_VERSION).$(IMAGE_MINOR_VERSION)
for arch in $(BUILD_ARCH); do \
docker push $(IMAGE):$(IMAGE_VERSION)-$${arch}; \
done; \
docker manifest create --amend $(IMAGE):$(IMAGE_VERSION) $(BUILD_IMAGE_LIST); \
docker manifest create --amend $(IMAGE):$(IMAGE_MAJOR_VERSION) $(BUILD_IMAGE_LIST); \
docker manifest create --amend $(IMAGE):$(IMAGE_MAJOR_VERSION).$(IMAGE_MINOR_VERSION) $(BUILD_IMAGE_LIST); \
docker manifest push --purge $(IMAGE):$(IMAGE_VERSION); \
docker manifest push --purge $(IMAGE):$(IMAGE_MAJOR_VERSION); \
docker manifest push --purge $(IMAGE):$(IMAGE_MAJOR_VERSION).$(IMAGE_MINOR_VERSION); \
7 changes: 6 additions & 1 deletion src/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM node:16-alpine3.13
ARG BASE=alpine:3.18
FROM ${BASE}

ARG IMAGE_CREATE_DATE
ARG IMAGE_VERSION
Expand All @@ -17,6 +18,10 @@ LABEL org.opencontainers.image.title="Hello Kubernetes!" \
org.opencontainers.image.source="https://github.com/paulbouwer/hello-kubernetes.git" \
org.opencontainers.image.revision=$IMAGE_SOURCE_REVISION

RUN addgroup -g 1000 node && \
adduser -u 1000 -G node -s /bin/sh -D node && \
apk add -U --no-cache nodejs npm

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
Expand Down
1 change: 0 additions & 1 deletion src/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ The application relies on the following files for configuration and operational
| File | Required | Information | Description |
| ---- | -------- | ----------- | ----------- |
| package.json | Yes | `.version` | The release version is used when the CONTAINER_IMAGE env is not provided. |
| info.json | Yes | `.containerImageArch` | The container image architecture is used for display. This file will be overwritten in future versions as part of the container image build process when multi-arch images are supported. |
3 changes: 0 additions & 3 deletions src/app/info.json

This file was deleted.

6 changes: 3 additions & 3 deletions src/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var podName = process.env.KUBERNETES_POD_NAME || os.hostname();
var nodeName = process.env.KUBERNETES_NODE_NAME || '-';
var nodeOS = os.type() + ' ' + os.release();
var applicationVersion = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
var containerImage = process.env.CONTAINER_IMAGE || 'paulbouwer/hello-kubernetes:' + applicationVersion
var containerImageArch = JSON.parse(fs.readFileSync('info.json', 'utf8')).containerImageArch;
var containerImage = process.env.CONTAINER_IMAGE || 'paulbouwer/hello-kubernetes:' + applicationVersion;
var containerImageArch = os.platform() + '/' + os.arch();

logger.debug();
logger.debug('Configuration');
Expand Down Expand Up @@ -79,4 +79,4 @@ logger.debug('-----------------------------------------------------');

app.listen(port, function () {
logger.info("Listening on: http://%s:%s", podName, port);
});
});