Skip to content

Commit dde5ca8

Browse files
authored
Fix multi-platform build (opendatahub-io#92)
Don't use "default" values for ARGs TARGETOS and TARGETARCH in Dockerfile. Signed-off-by: Christian Kadner <[email protected]>
1 parent dde110e commit dde5ca8

File tree

4 files changed

+125
-64
lines changed

4 files changed

+125
-64
lines changed

.dockerignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
**/*.class
2+
*.jar
3+
*.md
4+
.DS_Store
5+
.cache
6+
.dockerignore
17
.env
8+
.git
9+
.github
10+
.gitignore
11+
.idea
12+
.vscode
13+
default.etcd
14+
target
15+
temp
Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
1-
name: Build and Push
1+
name: Build
22

33
on:
4+
pull_request:
5+
branches:
6+
- main
7+
- "release-[0-9].[0-9]+"
8+
paths-ignore:
9+
- "**.md"
410
push:
5-
branches: [ main ]
11+
branches:
12+
- main
13+
- "release-[0-9].[0-9]+"
614
tags:
7-
- v*
8-
pull_request:
15+
- "v*"
16+
paths-ignore:
17+
- "**.md"
18+
19+
env:
20+
IMAGE_NAME: "kserve/modelmesh"
921

1022
jobs:
1123
test:
1224
runs-on: ubuntu-latest
1325
steps:
14-
- uses: actions/checkout@v2
15-
- name: Set up JDK 17
26+
- name: Checkout
27+
uses: actions/checkout@v3
28+
29+
- name: Set up Java 17
1630
uses: actions/[email protected]
1731
with:
1832
java-version: '17'
1933
distribution: 'temurin'
34+
2035
- name: Install etcd
2136
run: sudo ./.github/install-etcd.sh
22-
- name: Build with Maven
37+
38+
- name: Build and Test with Maven
2339
run: mvn -B package --file pom.xml
2440

2541
build:
2642
needs: test
2743
runs-on: ubuntu-latest
28-
29-
env:
30-
IMAGE_NAME: kserve/modelmesh
31-
3244
steps:
33-
- uses: actions/checkout@v2
45+
- name: Checkout
46+
uses: actions/checkout@v3
3447

3548
- name: Setup QEMU
3649
uses: docker/setup-qemu-action@v2
@@ -45,32 +58,38 @@ jobs:
4558
username: ${{ secrets.DOCKER_USER }}
4659
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
4760

48-
- name: Export version variables
61+
- name: Export docker build args
4962
run: |
5063
GIT_COMMIT=$(git rev-parse HEAD)
5164
BUILD_ID=$(date '+%Y%m%d')-$(git rev-parse HEAD | cut -c -5)
5265
5366
# Strip git ref prefix from version
5467
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
5568
69+
# Generate PR tag from github.ref == "refs/pull/123/merge"
70+
[ "$VERSION" == "merge" ] && VERSION=$(echo "${{ github.ref }}" | sed -e 's,refs/pull/\(.*\)/merge,pr-\1,')
71+
5672
# Use Docker `latest` tag convention
5773
[ "$VERSION" == "main" ] && VERSION=latest
5874
5975
echo "GIT_COMMIT=$GIT_COMMIT" >> $GITHUB_ENV
60-
echo "BUILD_ID=$BUILD_ID" >> $GITHUB_ENV
61-
echo "VERSION=$VERSION" >> $GITHUB_ENV
76+
echo "BUILD_ID=$BUILD_ID" >> $GITHUB_ENV
77+
echo "VERSION=$VERSION" >> $GITHUB_ENV
6278
63-
- name: Build and push
64-
uses: docker/build-push-action@v3
79+
# print env vars for debugging
80+
cat "$GITHUB_ENV"
81+
82+
- name: Build and push runtime image
83+
uses: docker/build-push-action@v4
6584
with:
6685
# for linux/s390x, maven errors due to missing io.grpc:protoc-gen-grpc-java:exe:linux-s390_64:1.51.1
6786
platforms: linux/amd64,linux/arm64/v8,linux/ppc64le
68-
pull: true
69-
cache-from: type=gha
70-
cache-to: type=gha,mode=max
87+
target: runtime
7188
push: ${{ github.event_name == 'push' }}
7289
tags: ${{ env.IMAGE_NAME }}:${{ env.VERSION }}
7390
build-args: |
7491
imageVersion=${{ env.VERSION }}
7592
buildId=${{ env.BUILD_ID }}
7693
commitSha=${{ env.GIT_COMMIT }}
94+
cache-from: type=gha
95+
cache-to: type=gha,mode=max

Dockerfile

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,54 @@
1414

1515
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.6 as build_base
1616

17-
# https://blog.thesparktree.com/docker-multi-arch-github-actions#architecture-specific-dockerfile-instructions
18-
ARG TARGETARCH=amd64
17+
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
18+
# don't provide "default" values (e.g. 'ARG TARGETARCH=amd64') for non-buildx environments,
19+
# see https://github.com/docker/buildx/issues/510
20+
ARG TARGETOS
21+
ARG TARGETARCH
1922

2023
ARG ETCD_VERSION=v3.5.4
2124

2225
LABEL image="build_base"
2326

2427
USER root
2528

26-
RUN true \
27-
&& microdnf --nodocs install java-17-openjdk-devel nss \
29+
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk
30+
31+
RUN --mount=type=cache,target=/root/.cache/microdnf:rw \
32+
microdnf --setopt=cachedir=/root/.cache/microdnf --nodocs install \
33+
java-17-openjdk-devel \
34+
nss \
2835
&& microdnf update --nodocs \
29-
&& microdnf clean all \
3036
&& sed -i 's:security.provider.12=SunPKCS11:#security.provider.12=SunPKCS11:g' /usr/lib/jvm/java-17-openjdk-*/conf/security/java.security \
3137
&& sed -i 's:#security.provider.1=SunPKCS11 ${java.home}/lib/security/nss.cfg:security.provider.12=SunPKCS11 ${java.home}/lib/security/nss.cfg:g' /usr/lib/jvm/java-17-openjdk-*/conf/security/java.security \
38+
&& java -version \
3239
&& true
3340

34-
RUN microdnf install wget tar gzip maven
35-
36-
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk
41+
RUN --mount=type=cache,target=/root/.cache/microdnf:rw \
42+
microdnf --setopt=cachedir=/root/.cache/microdnf --nodocs install \
43+
wget \
44+
tar \
45+
gzip \
46+
maven \
47+
&& true
3748

3849
# Install etcd -- used for CI tests
39-
RUN wget -q https://github.com/etcd-io/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-${TARGETARCH}.tar.gz && \
40-
mkdir -p /usr/lib/etcd && \
41-
tar xzf etcd-*-linux-${TARGETARCH}.tar.gz -C /usr/lib/etcd --strip-components=1 --no-same-owner && \
42-
rm -rf etcd*.gz
43-
4450
ENV PATH="/usr/lib/etcd:$PATH"
51+
RUN true \
52+
&& wget -q https://github.com/etcd-io/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-${TARGETOS:-linux}-${TARGETARCH:-amd64}.tar.gz \
53+
&& mkdir -p /usr/lib/etcd \
54+
&& tar xzf etcd-*-${TARGETOS:-linux}-${TARGETARCH:-amd64}.tar.gz -C /usr/lib/etcd --strip-components=1 --no-same-owner \
55+
&& rm -rf etcd*.gz \
56+
&& etcd -version \
57+
&& true
4558

4659
# Copy in code
4760
RUN mkdir /build
4861

4962
WORKDIR /build
5063

64+
5165
###############################################################################
5266
FROM build_base AS build
5367

@@ -57,57 +71,71 @@ COPY / /build
5771

5872
ENV MAVEN_OPTS="-Dfile.encoding=UTF8"
5973

60-
RUN mvn -B package -DskipTests=true --file pom.xml
74+
RUN --mount=type=cache,target=/root/.m2 \
75+
mvn -B package -DskipTests=true --file pom.xml
76+
6177

6278
###############################################################################
63-
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.6
79+
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.6 AS runtime
6480

65-
ARG imageVersion
66-
ARG buildId
67-
ARG commitSha
68-
ARG USER=2000
81+
# TODO: FROM registry.access.redhat.com/ubi8/openjdk-17-runtime:1.15
6982

70-
LABEL name="model-mesh" \
71-
vendor="KServe" \
72-
version="${imageVersion}" \
73-
summary="Core model-mesh sidecar image" \
74-
description="Model-mesh is a distributed LRU cache for serving runtime models" \
75-
release="${commitSha}"
76-
83+
ARG USER=2000
7784

7885
USER root
7986

80-
RUN true \
81-
&& microdnf --nodocs install java-17-openjdk-headless nss \
87+
ENV JAVA_HOME=/usr/lib/jvm/jre-17-openjdk
88+
89+
RUN --mount=type=cache,target=/root/.cache/microdnf:rw \
90+
microdnf --setopt=cachedir=/root/.cache/microdnf --nodocs install \
91+
java-17-openjdk-headless \
92+
nss \
8293
&& microdnf update --nodocs \
83-
&& microdnf clean all \
8494
&& sed -i 's:security.provider.12=SunPKCS11:#security.provider.12=SunPKCS11:g' /usr/lib/jvm/java-17-openjdk-*/conf/security/java.security \
8595
&& sed -i 's:#security.provider.1=SunPKCS11 ${java.home}/lib/security/nss.cfg:security.provider.12=SunPKCS11 ${java.home}/lib/security/nss.cfg:g' /usr/lib/jvm/java-17-openjdk-*/conf/security/java.security \
96+
&& java -version \
8697
&& true
8798

88-
ENV JAVA_HOME=/usr/lib/jvm/jre-17-openjdk
89-
9099
COPY --from=build /build/target/dockerhome/ /opt/kserve/mmesh/
91100

92101
# Make this the current directory when starting the container
93102
WORKDIR /opt/kserve/mmesh
94103

95-
RUN microdnf install shadow-utils hostname && \
104+
RUN --mount=type=cache,target=/root/.cache/microdnf:rw \
105+
microdnf --setopt=cachedir=/root/.cache/microdnf --nodocs install \
106+
shadow-utils \
107+
hostname \
96108
# Create app user
97-
useradd -c "Application User" -U -u ${USER} -m app && \
98-
chown -R app:0 /home/app && \
109+
&& useradd -c "Application User" -U -u ${USER} -m app \
110+
&& chown -R app:0 /home/app \
99111
# Adjust permissions on /etc/passwd to be writable by group root.
100112
# The user app is replaced by the assigned UID on OpenShift.
101-
chmod g+w /etc/passwd && \
113+
&& chmod g+w /etc/passwd \
102114
# In newer Docker there is a --chown option for the COPY command
103-
ln -s /opt/kserve/mmesh /opt/kserve/tas && \
104-
mkdir -p log && \
105-
chown -R app:0 . && \
106-
chmod -R 771 . && chmod 775 *.sh *.py && \
107-
echo "${buildId}" > /opt/kserve/mmesh/build-version && \
108-
\
115+
&& ln -s /opt/kserve/mmesh /opt/kserve/tas \
116+
&& mkdir -p log \
117+
&& chown -R app:0 . \
118+
&& chmod -R 771 . \
119+
&& chmod 775 *.sh *.py \
109120
# Disable java FIPS - see https://access.redhat.com/documentation/en-us/openjdk/17/html-single/configuring_openjdk_17_on_rhel_with_fips/index#config-fips-in-openjdk
110-
sed -i 's/security.useSystemPropertiesFile=true/security.useSystemPropertiesFile=false/g' $JAVA_HOME/conf/security/java.security
121+
&& sed -i 's/security.useSystemPropertiesFile=true/security.useSystemPropertiesFile=false/g' $JAVA_HOME/conf/security/java.security \
122+
&& true
123+
124+
# wait to create commit-specific LABEL until end of the build to not unnecessarily
125+
# invalidate the cached image layers
126+
ARG imageVersion
127+
ARG buildId
128+
ARG commitSha
129+
130+
RUN echo "${buildId}" > /opt/kserve/mmesh/build-version
131+
132+
LABEL name="model-mesh" \
133+
vendor="KServe" \
134+
version="${imageVersion}" \
135+
summary="Core model-mesh sidecar image" \
136+
description="Model-mesh is a distributed LRU cache for serving runtime models" \
137+
release="${commitSha}" \
138+
maintainer="[email protected]"
111139

112140
EXPOSE 8080
113141

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Sample build:
4040
```bash
4141
GIT_COMMIT=$(git rev-parse HEAD)
4242
BUILD_ID=$(date '+%Y%m%d')-$(git rev-parse HEAD | cut -c -5)
43-
IMAGE_TAG_VERSION=0.0.1
43+
IMAGE_TAG_VERSION="dev"
4444
IMAGE_TAG=${IMAGE_TAG_VERSION}-$(git branch --show-current)_${BUILD_ID}
4545

46-
docker build -t model-mesh:${IMAGE_TAG} \
46+
docker build -t modelmesh:${IMAGE_TAG} \
4747
--build-arg imageVersion=${IMAGE_TAG} \
4848
--build-arg buildId=${BUILD_ID} \
4949
--build-arg commitSha=${GIT_COMMIT} .

0 commit comments

Comments
 (0)