Skip to content

Commit 425c19d

Browse files
Change to use unprivileged user and consitent uid/gid for docker images
Updates all dockerfiles with: * Create dspace user and group with consistent UID and GID * Use numeric USER ID * Use number ID > 10000 * Add chown using UID:GID to DOCKER COPY and ADD instructions * Use consistent casings in Docker instructions
1 parent 579491e commit 425c19d

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

Dockerfile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
ARG JDK_VERSION=11
99

1010
# Step 1 - Run Maven Build
11-
FROM ufal/dspace-dependencies:dspace-7_x as build
11+
FROM ufal/dspace-dependencies:dspace-7_x AS build
1212
ARG TARGET_DIR=dspace-installer
1313
WORKDIR /app
1414
# The dspace-installer directory will be written to /install
1515
RUN mkdir /install \
1616
&& chown -Rv dspace: /install \
1717
&& chown -Rv dspace: /app
18-
USER dspace
18+
USER 10001
1919
# Copy the DSpace source code (from local machine) into the workdir (excluding .dockerignore contents)
2020
ADD --chown=dspace . /app/
2121
# Build DSpace (note: this build doesn't include the optional, deprecated "dspace-rest" webapp)
@@ -25,7 +25,7 @@ RUN mvn --no-transfer-progress package && \
2525
mvn clean
2626

2727
# Step 2 - Run Ant Deploy
28-
FROM openjdk:${JDK_VERSION}-slim as ant_build
28+
FROM openjdk:${JDK_VERSION}-slim AS ant_build
2929
ARG TARGET_DIR=dspace-installer
3030
# COPY the /install directory from 'build' container to /dspace-src in this container
3131
COPY --from=build /install /dspace-src
@@ -48,16 +48,19 @@ RUN ant init_installation update_configs update_code update_webapps
4848
# Step 3 - Run tomcat
4949
# Create a new tomcat image that does not retain the the build directory contents
5050
FROM tomcat:9-jdk${JDK_VERSION}
51+
# Create a custom dspace user with same gid/uid as last stage
52+
RUN groupadd -g 10002 dspace && \
53+
useradd -u 10001 -g dspace dspace
5154
# NOTE: DSPACE_INSTALL must align with the "dspace.dir" default configuration.
5255
ENV DSPACE_INSTALL=/dspace
5356
# Copy the /dspace directory from 'ant_build' container to /dspace in this container
54-
COPY --from=ant_build /dspace $DSPACE_INSTALL
57+
COPY --from=ant_build --chown=10001:10002 /dspace $DSPACE_INSTALL
5558
# Expose Tomcat port and AJP port
5659
EXPOSE 8080 8009 8000
5760
# Give java extra memory (2GB)
5861
ENV JAVA_OPTS=-Xmx2000m
59-
COPY scripts/restart_debug/* /usr/local/tomcat/bin
60-
COPY scripts/index-scripts/* /dspace/bin
62+
COPY --chown=10001:10002 scripts/restart_debug/* /usr/local/tomcat/bin
63+
COPY --chown=10001:10002 scripts/index-scripts/* /dspace/bin
6164
# Link the DSpace 'server' webapp into Tomcat's webapps directory.
6265
# This ensures that when we start Tomcat, it runs from /server path (e.g. http://localhost:8080/server/)
6366
RUN ln -s $DSPACE_INSTALL/webapps/server /usr/local/tomcat/webapps/server
@@ -66,6 +69,8 @@ RUN ln -s $DSPACE_INSTALL/webapps/server /usr/local/tomcat/webapps/server
6669
# Please note that server webapp should only run on one path at a time.
6770
#RUN mv /usr/local/tomcat/webapps/ROOT /usr/local/tomcat/webapps/ROOT.bk && \
6871
# ln -s $DSPACE_INSTALL/webapps/server /usr/local/tomcat/webapps/ROOT
72+
# Run as dspace user
73+
USER 10001
6974

7075
WORKDIR /usr/local/tomcat/bin
7176
RUN chmod u+x redebug.sh undebug.sh custom_run.sh

Dockerfile.cli

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
ARG JDK_VERSION=11
99

1010
# Step 1 - Run Maven Build
11-
FROM ufal/dspace-dependencies:dspace-7_x as build
11+
FROM ufal/dspace-dependencies:dspace-7_x AS build
1212
ARG TARGET_DIR=dspace-installer
1313
WORKDIR /app
1414
# The dspace-installer directory will be written to /install
1515
RUN mkdir /install \
1616
&& chown -Rv dspace: /install \
1717
&& chown -Rv dspace: /app
18-
USER dspace
18+
USER 10001
1919
# Copy the DSpace source code (from local machine) into the workdir (excluding .dockerignore contents)
2020
ADD --chown=dspace . /app/
2121
# Build DSpace. Copy the dspace-installer directory to /install. Clean up the build to keep the docker image small
@@ -48,7 +48,10 @@ RUN ant init_installation update_configs update_code
4848
FROM openjdk:${JDK_VERSION}
4949
# NOTE: DSPACE_INSTALL must align with the "dspace.dir" default configuration.
5050
ENV DSPACE_INSTALL=/dspace
51+
RUN groupadd -g 10002 dspace && \
52+
useradd -u 10001 -g dspace dspace
5153
# Copy the /dspace directory from 'ant_build' container to /dspace in this container
52-
COPY --from=ant_build /dspace $DSPACE_INSTALL
54+
COPY --from=ant_build --chown=10001:10002 /dspace $DSPACE_INSTALL
5355
# Give java extra memory (1GB)
5456
ENV JAVA_OPTS=-Xmx1000m
57+
USER 10001

Dockerfile.dependencies

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ FROM maven:3-openjdk-${JDK_VERSION}-slim as build
1111
ARG TARGET_DIR=dspace-installer
1212
WORKDIR /app
1313
# Create the 'dspace' user account & home directory
14-
RUN useradd dspace \
15-
&& mkdir -p /home/dspace \
16-
&& chown -Rv dspace: /home/dspace
14+
RUN groupadd -g 10002 dspace && \
15+
useradd -u 10001 -g dspace dspace
1716
RUN chown -Rv dspace: /app
1817
# Need git to support buildnumber-maven-plugin, which lets us know what version of DSpace is being run.
1918
RUN apt-get update \
@@ -22,10 +21,10 @@ RUN apt-get update \
2221
&& rm -rf /var/lib/apt/lists/*
2322

2423
# Switch to dspace user & run below commands as that user
25-
USER dspace
24+
USER 10001
2625

2726
# Copy the DSpace source code (from local machine) into the workdir (excluding .dockerignore contents)
28-
ADD --chown=dspace . /app/
27+
ADD --chown=10001:10002 . /app/
2928

3029
# Trigger the installation of all maven dependencies (hide download progress messages)
3130
RUN mvn --no-transfer-progress package

Dockerfile.test

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ARG JDK_VERSION=11
1111

1212
# Step 1 - Run Maven Build
13-
FROM ufal/dspace-dependencies:dspace-7_x as build
13+
FROM ufal/dspace-dependencies:dspace-7_x AS build
1414
ARG TARGET_DIR=dspace-installer
1515
WORKDIR /app
1616
# The dspace-installer directory will be written to /install
@@ -26,8 +26,8 @@ RUN mvn --no-transfer-progress package -Pdspace-rest && \
2626
mv /app/dspace/target/${TARGET_DIR}/* /install && \
2727
mvn clean
2828

29-
# Step 2 - Run Ant Deploy
30-
FROM openjdk:${JDK_VERSION}-slim as ant_build
29+
# Step 2 - Run Ant Deploy
30+
FROM openjdk:${JDK_VERSION}-slim AS ant_build
3131
ARG TARGET_DIR=dspace-installer
3232
# COPY the /install directory from 'build' container to /dspace-src in this container
3333
COPY --from=build /install /dspace-src
@@ -52,8 +52,11 @@ RUN ant init_installation update_configs update_code update_webapps
5252
FROM tomcat:9-jdk${JDK_VERSION}
5353
ENV DSPACE_INSTALL=/dspace
5454
ENV TOMCAT_INSTALL=/usr/local/tomcat
55+
# Create a custom dspace user with same gid/uid as last stage
56+
RUN groupadd -g 10002 dspace && \
57+
useradd -u 10001 -g dspace dspace
5558
# Copy the /dspace directory from 'ant_build' containger to /dspace in this container
56-
COPY --from=ant_build /dspace $DSPACE_INSTALL
59+
COPY --from=ant_build --chown=10001:10002 /dspace $DSPACE_INSTALL
5760
# Enable the AJP connector in Tomcat's server.xml
5861
# NOTE: secretRequired="false" should only be used when AJP is NOT accessible from an external network. But, secretRequired="true" isn't supported by mod_proxy_ajp until Apache 2.5
5962
RUN sed -i '/Service name="Catalina".*/a \\n <Connector protocol="AJP/1.3" port="8009" address="0.0.0.0" redirectPort="8443" URIEncoding="UTF-8" secretRequired="false" />' $TOMCAT_INSTALL/conf/server.xml
@@ -78,5 +81,5 @@ RUN ln -s $DSPACE_INSTALL/webapps/server /usr/local/tomcat/webapps/server &&
7881

7982
# Overwrite the v6.x (deprecated) REST API's web.xml, so that we can run it on HTTP (defaults to requiring HTTPS)
8083
# WARNING: THIS IS OBVIOUSLY INSECURE. NEVER DO THIS IN PRODUCTION.
81-
COPY dspace/src/main/docker/test/rest_web.xml $DSPACE_INSTALL/webapps/rest/WEB-INF/web.xml
84+
COPY --chown=10001:10002 dspace/src/main/docker/test/rest_web.xml $DSPACE_INSTALL/webapps/rest/WEB-INF/web.xml
8285
RUN sed -i -e "s|\${dspace.dir}|$DSPACE_INSTALL|" $DSPACE_INSTALL/webapps/rest/WEB-INF/web.xml

0 commit comments

Comments
 (0)