Skip to content

Instantly share code, notes, and snippets.

@rainerh
Last active November 8, 2021 14:38
Show Gist options
  • Save rainerh/91ff93d84bd4b3a4d94a49e04d64ad35 to your computer and use it in GitHub Desktop.
Save rainerh/91ff93d84bd4b3a4d94a49e04d64ad35 to your computer and use it in GitHub Desktop.
multistage Dockerfile for maven/npm/karma
ARG BUILD_IMAGE=maven:3.8.3-eclipse-temurin-11
ARG RUNTIME_IMAGE=eclipse-temurin:11-jre-focal
#############################################################################################
### Stage where Docker is preparing the build base image ###
#############################################################################################
FROM --platform=linux/amd64 ${BUILD_IMAGE} AS base
# Install Chrome/Chromium Browser for tests
RUN apt-get update \
&& apt-get install -y \
xvfb \
libxss1 \
libosmesa6 \
libgconf-2-4 \
wget \
apt-transport-https \
&& wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
&& (dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install; rm google-chrome-stable_current_amd64.deb; apt-get clean; rm -rf /var/lib/apt/lists/* )
ENV CHROME_BIN='/usr/bin/google-chrome'
#############################################################################################
### Stage where Docker is pulling all maven dependencies ###
#############################################################################################
FROM base as dependencies
ARG PROXY_SET=false
ARG PROXY_HOST=
ARG PROXY_PORT=
COPY pom.xml ./
RUN ulimit -c unlimited && mvn -B dependency:go-offline \
-DproxySet=${PROXY_SET} \
-DproxyHost=${PROXY_HOST} \
-DproxyPort=${PROXY_PORT}
#############################################################################################
### Stage where Docker is building spring boot app using maven ###
#############################################################################################
FROM dependencies as build
ARG PROXY_SET=false
ARG PROXY_HOST=
ARG PROXY_PORT=
COPY src ./src
RUN ulimit -c unlimited && mvn -B clean package \
-DproxySet=${PROXY_SET} \
-DproxyHost=${PROXY_HOST} \
-DproxyPort=${PROXY_PORT}
#############################################################################################
### Optional stage where Docker is running Sonar analysis ###
#############################################################################################
FROM build
ARG SONAR_ENABLED=false
ARG SONAR_URL=
ARG SONAR_ORGANIZATION=
ARG SONAR_USERNAME=
ARG SONAR_PASSWORD=
ARG SONAR_BRANCH=
RUN if [ "$SONAR_ENABLED" = "true" ] ; \
then mvn -B sonar:sonar \
-Dsonar.host.url=${SONAR_URL} \
-Dsonar.organization=${SONAR_ORGANIZATION} \
-Dsonar.branch.name=${SONAR_BRANCH} \
-Dsonar.login=${SONAR_USERNAME} \
-Dsonar.password=${SONAR_PASSWORD}; \
fi
#############################################################################################
### Stage where Docker is running a java process to run a service built in previous stage ###
#############################################################################################
FROM ${RUNTIME_IMAGE}
COPY --from=build /app/target/my-service-*.jar /app/service.jar
CMD ["/usr/bin/java", "-jar", "/app/service.jar"]
#############################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment