Last active
August 27, 2018 15:46
-
-
Save marcellodesales/56d2c121879211b47ae87e32d6ce158d to your computer and use it in GitHub Desktop.
Generic Multi-stage Dockerfile for Springboot Apps with Unit and Integration tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ##################################################################### | |
# Build stage for building the target directory before running tests | |
# ##################################################################### | |
FROM marcellodesales/gradle:2.13 as builder | |
MAINTAINER marcello.desales@gmail.com | |
USER root | |
#RUN apt-get update && apt-get install -y git | |
# Only copy the necessary to pull only the dependencies | |
# ADD ./.git /opt/build/.git | |
ADD ./src /opt/build/src | |
ADD ./build.gradle /opt/build/build.gradle | |
# ADD ./settings.gradle /opt/build/settings.gradle | |
# Add the gradle scripts to the right location | |
ADD ./src/build/gradle/ /opt/build/src/build/gradle | |
# ADD ./gradlew /opt/build/gradlew | |
# ADD ./gradle /opt/build/gradle | |
WORKDIR /opt/build/ | |
RUN gradle build bootRepackage -x test | |
# https://stackoverflow.com/questions/40122152/how-to-remove-entrypoint-from-parent-image-on-dockerfile/40122359#40122359 | |
ENTRYPOINT [] | |
# ##################################################################### | |
# Build stage for running the unit tests from the target directory | |
# ##################################################################### | |
FROM builder as unit-tests | |
WORKDIR /opt/build/ | |
# 1. Build: docker build -t config-server-unit-tests --target config-server-unit-tests . | |
# 2. Run: docker run --rm config-server-unit-tests | |
# Run the full packaging after copying the source | |
CMD gradle --stacktrace check test -x checkJavadoc | |
# ##################################################################### | |
# Build stage for running the unit tests from the target directory | |
# ##################################################################### | |
FROM builder as integration-tests | |
WORKDIR /opt/build/ | |
# 1. Build: docker build -t config-server-unit-tests --target config-server-unit-tests . | |
# 2. Run: docker run --rm config-server-unit-tests | |
# Run the full packaging after copying the source | |
CMD gradle --stacktrace check integrationTest | |
# ##################################################################### | |
# Runtime stage | |
# ##################################################################### | |
FROM openjdk:8-jre-slim | |
# Port used by the server | |
EXPOSE 8080 | |
# This is to support HTTPS calls to other services | |
RUN apt-get update && apt-get install -y curl ca-certificates | |
RUN update-ca-certificates && \ | |
mkdir -p /usr/share/ssl/certs && \ | |
chmod 755 /usr/share/ssl/certs | |
# Copy from the previous stage | |
# If this is enabled, http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_encryption_and_decryption | |
# COPY --from=unit-tests /opt/build/jce_policy-8.zip /tmp/ | |
COPY --from=unit-tests /opt/build/build/libs/*.war /tmp/ | |
COPY --from=unit-tests /opt/build/src/main/resources /runtime/resources | |
# Just rename the built version | |
RUN find /tmp -name "*.war" ! -name "*sources*" ! -name "*javadoc*" -exec cp -t /runtime {} + && \ | |
mv /runtime/*.war /runtime/server.jar && \ | |
rm -f /tmp/*.war | |
# What to execute on docker run | |
ENTRYPOINT sh -c "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom \ | |
$JAVA_PARAMS -jar /runtime/server.jar --server.port=8080 $SPRING_BOOT_APP_OPTS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generate a runtime image
docker build -t myservice .
docker run --rm -ti -p 8080:8080 myservice
Generate a test image
docker build -t myservice-unit-tests --target unit-tests .
docker run --rm myservice-unit-tests