Skip to content

Commit be5add9

Browse files
committed
Run the Gradle build as a non-root user
The builder stage ran Gradle as root, so Log4jMigrationsTest's read-only file assertion failed - root ignores POSIX permission bits. Building the Dockerfile from scratch therefore either failed on that test or had to skip the test suite entirely. Drop to the ubuntu user (uid 1000) that the base image already ships - the same account the runner stages rename to engine.
1 parent 762e072 commit be5add9

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

Dockerfile

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,31 @@ SHELL ["/bin/bash", "-c"]
1616
ARG GRADLE_BUILD_ARGS="-PdisableSigning=true"
1717

1818
# Stage 1a: Install dependencies
19-
# Install necessary tools
20-
COPY .sdkmanrc .
19+
# Install necessary tools, then drop root. The base image ships a
20+
# pre-created "ubuntu" user (uid 1000, the same one the runner stage
21+
# below renames to "engine") — reuse it so Gradle doesn't run as root,
22+
# which ignores POSIX permission bits and breaks tests that depend on
23+
# them (e.g. Log4jMigrationsTest's read-only-file assertion).
2124
RUN apt-get update\
2225
&& apt-get install -y zip curl\
23-
&& curl -s "https://get.sdkman.io?ci=true" | bash \
24-
&& source "$HOME/.sdkman/bin/sdkman-init.sh" && sdk env install \
25-
&& rm -rf /var/lib/apt/lists/*
26+
&& rm -rf /var/lib/apt/lists/* \
27+
&& chown ubuntu:ubuntu /app
28+
USER ubuntu
29+
30+
COPY --chown=ubuntu:ubuntu .sdkmanrc .
31+
RUN curl -s "https://get.sdkman.io?ci=true" | bash \
32+
&& source "$HOME/.sdkman/bin/sdkman-init.sh" && sdk env install
2633

2734
# Stage 1b: Build the application
2835
# Copy the entire source tree (excluding .dockerignore files), and build
2936
# (file encoding is pinned to UTF-8 in gradle.properties)
30-
COPY . .
31-
RUN --mount=type=cache,target=/root/.gradle/caches,sharing=locked \
32-
--mount=type=cache,target=/root/.gradle/wrapper,sharing=locked \
37+
COPY --chown=ubuntu:ubuntu . .
38+
# The cache mounts below own their own target dirs (uid/gid), but not the
39+
# ~/.gradle parent itself — Gradle also writes ~/.gradle/native (extracted
40+
# native-platform lib) outside either mount, so create the parent up front.
41+
RUN mkdir -p /home/ubuntu/.gradle
42+
RUN --mount=type=cache,target=/home/ubuntu/.gradle/caches,sharing=locked,uid=1000,gid=1000 \
43+
--mount=type=cache,target=/home/ubuntu/.gradle/wrapper,sharing=locked,uid=1000,gid=1000 \
3344
source "$HOME/.sdkman/bin/sdkman-init.sh" \
3445
&& ./gradlew --no-daemon build ${GRADLE_BUILD_ARGS}
3546

0 commit comments

Comments
 (0)