Skip to content

Commit 28aa2a7

Browse files
committed
fix(Dockerfile): #106 use non root user for building container image
1 parent 151aa7c commit 28aa2a7

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Dockerfile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
# The below Docker code creates an executable jar and then creates an Docker Image out of it.
1+
# Stage 1: Build the jar
22
FROM gradle:8.5-jdk21 AS build
3+
# Copy source code into the container and set the ownership to 'gradle' user
34
COPY --chown=gradle:gradle . /home/gradle/src
45
WORKDIR /home/gradle/src
56
RUN gradle build -x test --no-daemon
67

8+
# Stage 2: Production image
79
FROM openjdk:21-slim AS production
810
EXPOSE 8080
9-
RUN mkdir /app
11+
12+
# Create a non-root user and group (using 'appuser' as an example)
13+
RUN groupadd -r appgroup && useradd -r -g appgroup -m appuser
14+
15+
# Create the /app directory and set permissions
16+
RUN mkdir /app && chown appuser:appgroup /app
17+
18+
# Copy the jar file from the build stage into the production image
1019
COPY --from=build /home/gradle/src/build/libs/*.jar /app/companieshouse-*.jar
11-
ENTRYPOINT ["java","-jar","app/companieshouse-*.jar"]
20+
21+
# Change to non-root user
22+
USER appuser
23+
24+
# Set the entrypoint to run the Java application
25+
ENTRYPOINT ["java", "-jar", "/app/companieshouse-*.jar"]

0 commit comments

Comments
 (0)