Skip to content

Instantly share code, notes, and snippets.

@jakubigla
Created December 10, 2018 14:36
Show Gist options
  • Save jakubigla/de9d8f3b35931f0a24fedcf0a3d165fa to your computer and use it in GitHub Desktop.
Save jakubigla/de9d8f3b35931f0a24fedcf0a3d165fa to your computer and use it in GitHub Desktop.
Dockerfile for multistage build of spring boot application using maven with SonarQube and proxy support
ARG BUILD_IMAGE=maven:3.5-jdk-11
ARG RUNTIME_IMAGE=openjdk:11-jdk-slim
#############################################################################################
### Stage where Docker is pulling all maven dependencies ###
#############################################################################################
FROM ${BUILD_IMAGE} as dependencies
ARG PROXY_SET=false
ARG PROXY_HOST=
ARG PROXY_PORT=
COPY pom.xml ./
RUN 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 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/eventstore-*.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