From a13f18f093a3dc6d0cb6a1d49fc96c00186fca11 Mon Sep 17 00:00:00 2001 From: Terry Brady Date: Thu, 20 Dec 2018 21:16:05 -0800 Subject: [PATCH] Optimize docker builds --- Dockerfile.dependencies | 25 +++++++++++++++++++++++++ Dockerfile.jdk8 | 20 ++++++++++++-------- Dockerfile.jdk8-test | 18 ++++++++++-------- 3 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 Dockerfile.dependencies diff --git a/Dockerfile.dependencies b/Dockerfile.dependencies new file mode 100644 index 0000000000..5b7ae3a793 --- /dev/null +++ b/Dockerfile.dependencies @@ -0,0 +1,25 @@ +# This image will be published as dspace/dspace-dependencies +# The purpose of this image is to make the build for dspace/dspace run faster + +# Step 1 - Run Maven Build +FROM maven as build +ARG TARGET_DIR=dspace-installer +WORKDIR /app + +# The Mirage2 build cannot run as root. Setting the user to dspace. +RUN useradd dspace \ + && mkdir /home/dspace \ + && chown -Rv dspace: /home/dspace +USER dspace + +# Copy the DSpace source code into the workdir (excluding .dockerignore contents) +ADD --chown=dspace . /app/ +COPY dspace/src/main/docker/local.cfg /app/local.cfg + +# Trigger the installation of all maven dependencies including the Mirage2 dependencies +# Clean up the built artifacts in the same step to keep the docker image small +RUN mvn package -Dmirage2.on=true && mvn clean + +# clear the contents of the /app directory so no artifacts are left when dspace:dspace is built +USER root +RUN rm -rf /app/* \ No newline at end of file diff --git a/Dockerfile.jdk8 b/Dockerfile.jdk8 index 7f691739de..7daab3ff04 100644 --- a/Dockerfile.jdk8 +++ b/Dockerfile.jdk8 @@ -9,26 +9,30 @@ # - default tag for branch: dspace/dspace: dspace/dspace:dspace-6_x-jdk8 # Step 1 - Run Maven Build -FROM maven as build +FROM dspace/dspace-dependencies:dspace-6_x as build +ARG TARGET_DIR=dspace-installer WORKDIR /app -# The Mirage2 build cannot run as root. Setting the user to dspace. -RUN useradd dspace \ - && mkdir /home/dspace \ - && chown -Rv dspace: /home/dspace +# The dspace-install directory will be written to /install +RUN mkdir /install \ + && chown -Rv dspace: /install + USER dspace # Copy the DSpace source code into the workdir (excluding .dockerignore contents) ADD --chown=dspace . /app/ COPY dspace/src/main/docker/local.cfg /app/local.cfg -RUN mvn package -Dmirage2.on=true +# Build DSpace. Copy the dspace-install directory to /install. Clean up the build to keep the docker image small +RUN mvn package -Dmirage2.on=true && \ + mv /app/dspace/target/${TARGET_DIR}/* /install && \ + mvn clean # Step 2 - Run Ant Deploy FROM tomcat:8-jre8 as ant_build ARG TARGET_DIR=dspace-installer -COPY --from=build /app /dspace-src -WORKDIR /dspace-src/dspace/target/${TARGET_DIR} +COPY --from=build /install /dspace-src +WORKDIR /dspace-src # Create the initial install deployment using ANT ENV ANT_VERSION 1.10.5 diff --git a/Dockerfile.jdk8-test b/Dockerfile.jdk8-test index bb7cbd32a7..f0a8a240e5 100644 --- a/Dockerfile.jdk8-test +++ b/Dockerfile.jdk8-test @@ -9,13 +9,13 @@ # - default tag for branch: dspace/dspace: dspace/dspace:dspace-6_x-jdk8 # Step 1 - Run Maven Build -FROM maven as build +FROM dspace/dspace-dependencies:dspace-6_x as build +ARG TARGET_DIR=dspace-installer WORKDIR /app -# The Mirage2 build cannot run as root. Setting the user to dspace. -RUN useradd dspace \ - && mkdir /home/dspace \ - && chown -Rv dspace: /home/dspace +RUN mkdir /install \ + && chown -Rv dspace: /install + USER dspace # Copy the DSpace source code into the workdir (excluding .dockerignore contents) @@ -26,13 +26,15 @@ COPY dspace/src/main/docker/local.cfg /app/local.cfg COPY dspace/src/main/docker/test/solr_web.xml /app/dspace-solr/src/main/webapp/WEB-INF/web.xml COPY dspace/src/main/docker/test/rest_web.xml /app/dspace-rest/src/main/webapp/WEB-INF/web.xml -RUN mvn package -Dmirage2.on=true +RUN mvn package -Dmirage2.on=true && \ + mv /app/dspace/target/${TARGET_DIR}/* /install && \ + mvn clean # Step 2 - Run Ant Deploy FROM tomcat:8-jre8 as ant_build ARG TARGET_DIR=dspace-installer -COPY --from=build /app /dspace-src -WORKDIR /dspace-src/dspace/target/${TARGET_DIR} +COPY --from=build /install /dspace-src +WORKDIR /dspace-src # Create the initial install deployment using ANT ENV ANT_VERSION 1.10.5