Skip to content

Commit

Permalink
feat(jar): Migrate the dockerfile to jar
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelsJP committed Oct 12, 2023
1 parent 95b4014 commit 7ffca83
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 94 deletions.
85 changes: 26 additions & 59 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,64 +1,28 @@
# Image is reused in the workflow builds for master and the latest version
FROM maven:3.8-openjdk-17-slim as base

FROM docker.io/maven:3.8-openjdk-17-slim as build
ARG DEBIAN_FRONTEND=noninteractive


# hadolint ignore=DL3002
USER root

# Install dependencies and locales
# hadolint ignore=DL3008
RUN apt-get update -qq && \
apt-get install -qq -y --no-install-recommends nano moreutils jq wget && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

FROM base as tomcat
ARG TOMCAT_MAJOR=10
ARG TOMCAT_VERSION=10.1.11

# hadolint ignore=DL3002
USER root

WORKDIR /tmp
# Prepare tomcat
RUN wget -q https://archive.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR}/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz -O /tmp/tomcat.tar.gz && \
tar xf tomcat.tar.gz && \
mv /tmp/apache-tomcat-${TOMCAT_VERSION}/ /tmp/tomcat && \
echo "org.apache.catalina.level = WARNING" >> /tmp/tomcat/conf/logging.properties
WORKDIR /tmp/ors

FROM base as build

# hadolint ignore=DL3002
USER root

ENV MAVEN_OPTS="-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
ENV MAVEN_CLI_OPTS="--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"

WORKDIR /ors-core

COPY ors-api /ors-core/ors-api
COPY ors-engine /ors-core/ors-engine
COPY pom.xml /ors-core/pom.xml
COPY ors-report-aggregation /ors-core/ors-report-aggregation
COPY ors-api /tmp/ors/ors-api
COPY ors-engine /tmp/ors/ors-engine
COPY pom.xml /tmp/ors/pom.xml
COPY ors-report-aggregation /tmp/ors/ors-report-aggregation

# Build the project and ignore the report aggregation module as not needed for the API
RUN mvn package -DskipTests -pl '!ors-report-aggregation'
RUN mvn clean package -DskipTests -pl '!ors-report-aggregation' -P buildFatJar

# build final image, just copying stuff inside
FROM amazoncorretto:17.0.7-alpine3.17 as publish
FROM docker.io/amazoncorretto:17.0.8-alpine3.18 as publish

# Build ARGS
ARG UID=1000
ARG GID=1000
ARG UID=1234
ARG GID=1234
ARG OSM_FILE=./ors-api/src/test/files/heidelberg.osm.gz
ARG BASE_FOLDER=/home/ors

# Runtime ENVs for tomcat
ENV CATALINA_BASE=${BASE_FOLDER}/tomcat
ENV CATALINA_HOME=${BASE_FOLDER}/tomcat
ENV CATALINA_PID=${BASE_FOLDER}/tomcat/temp/tomcat.pid
ARG BASE_FOLDER=/opt/openrouteservice

# Set the default language
ENV LANG='en_US' LANGUAGE='en_US' LC_ALL='en_US'
Expand All @@ -67,24 +31,27 @@ ENV LANG='en_US' LANGUAGE='en_US' LC_ALL='en_US'
RUN apk add --no-cache bash=~'5' openssl=~'3' && \
addgroup -g ${GID} ors && \
adduser -D -h ${BASE_FOLDER} -u ${UID} -G ors ors && \
mkdir -p ${BASE_FOLDER}/ors-core/logs ${BASE_FOLDER}/ors-conf ${BASE_FOLDER}/ors-core/data ${BASE_FOLDER}/tomcat/logs && \
chown -R ors ${BASE_FOLDER}/tomcat ${BASE_FOLDER}/ors-core/logs ${BASE_FOLDER}/ors-conf ${BASE_FOLDER}/ors-core/data ${BASE_FOLDER}/tomcat/logs
mkdir -p ${BASE_FOLDER}/logs ${BASE_FOLDER}/conf ${BASE_FOLDER}/data ${BASE_FOLDER}/graphs && \
chown -R ors ${BASE_FOLDER}

WORKDIR ${BASE_FOLDER}

# Copy over the needed bits and pieces from the other stages.
COPY --chown=ors:ors --from=tomcat /tmp/tomcat ${BASE_FOLDER}/tomcat
COPY --chown=ors:ors --from=build /ors-core/ors-api/target/ors.war ${BASE_FOLDER}/tomcat/webapps/ors.war
COPY --chown=ors:ors --from=build /ors-core/ors-api/src/main/resources/log4j.properties ${BASE_FOLDER}/tomcat/conf/logging.properties
COPY --chown=ors:ors ./docker-entrypoint.sh ${BASE_FOLDER}/docker-entrypoint.sh
COPY --chown=ors:ors ./ors-api/ors-config.yml ${BASE_FOLDER}/tmp/ors-config.yml
COPY --chown=ors:ors ./$OSM_FILE ${BASE_FOLDER}/tmp/osm_file.pbf
COPY ./docker-entrypoint.sh /entrypoint.sh
COPY --chown=ors:ors --from=build /tmp/ors/ors-api/target/ors.jar ${BASE_FOLDER}/lib/ors.jar
COPY --chown=ors:ors ./ors-api/ors-config.yml ${BASE_FOLDER}/config/example-ors-config.yml
COPY --chown=ors:ors ./$OSM_FILE ${BASE_FOLDER}/files/example_osm_file.pbf

# Install libxext, clean up and set permissions
RUN apk add --no-cache libxext=~'1.3' && \
rm -rf /var/cache/apk/* && \
chmod +x ${BASE_FOLDER}/lib/ors.jar && chown -R ors:ors ${BASE_FOLDER}

USER ${UID}:${GID}
USER ors

ENV BUILD_GRAPHS="False"
ENV ORS_CONFIG_LOCATION=ors-conf/ors-config.yml
ENV BASE_FOLDER=${BASE_FOLDER}
ENV ORS_CONFIG_LOCATION=${BASE_FOLDER}/config/example-ors-config.yml

# Start the container
ENTRYPOINT ["/home/ors/docker-entrypoint.sh"]
CMD ["/home/ors"]
ENTRYPOINT ["/entrypoint.sh"]
126 changes: 91 additions & 35 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,116 @@

echo "Running container as user $(whoami) with id $(id -u)"

echo "###### ORS pre-start checks ######"
# Check for old .json configs
JSON_FILES=$(ls -d -- "${ORS_HOME}/config/"*.json 2>/dev/null)
if [ -n "$JSON_FILES" ]; then
echo "Old .json config found. They're deprecated and will be replaced in ORS version 8."
echo "Please migrate to the new .yml example."
fi

if [[ -d /ors-core ]] || [[ -d /ors-conf ]]; then
echo "You're mounting old paths. Remove them and migrate to the new docker setup: https://giscience.github.io/openrouteservice/installation/Running-with-Docker.html"
echo "Exit setup due to old folders /ors-core or /ors-conf being mounted"
sleep 5
exit 1
fi

ors_base=${1}
catalina_base=${ors_base}/tomcat
graphs=${ors_base}/ors-core/data/graphs

echo "ORS Path: ${ors_base}"
echo "Catalina Path: ${catalina_base}"
# Fail if BASE_FOLDER is not set
if [ -z "${BASE_FOLDER}" ]; then
echo "BASE_FOLDER not set. This shouldn't happening. Exiting."
exit 1
fi

echo "###### Set ORS environment variables ######"
jar_file=${BASE_FOLDER}/lib/ors.jar
graphs=${BASE_FOLDER}/graphs

if [ -z "${CATALINA_OPTS}" ]; then
export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9001 -Dcom.sun.management.jmxremote.rmi.port=9001 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost"
# Users can define their own ORS_HOME. If not, use the default.
if [ -z "${ORS_HOME}" ]; then
echo "ORS_HOME not found. Using default: ${BASE_FOLDER}"
export ORS_HOME=${BASE_FOLDER}
fi

if [ -z "${JAVA_OPTS}" ]; then
export JAVA_OPTS="-Djava.awt.headless=true -server -XX:TargetSurvivorRatio=75 -XX:SurvivorRatio=64 -XX:MaxTenuringThreshold=3 -XX:+UseG1GC -XX:+ScavengeBeforeFullGC -XX:ParallelGCThreads=4 -Xms1g -Xmx2g"
fi
ors_config_location=${ORS_CONFIG_LOCATION:-"${ORS_HOME}/config/ors-config.yml"}
ors_build_graphs=${BUILD_GRAPHS:-"False"}

{
echo "CATALINA_BASE=\"${catalina_base}\""
echo "CATALINA_HOME=\"${catalina_base}\""
echo "CATALINA_PID=\"${catalina_base}/temp/tomcat.pid\""
echo "CATALINA_OPTS=\"${CATALINA_OPTS}\""
echo "JAVA_OPTS=\"${JAVA_OPTS}\""
} >"${catalina_base}"/bin/setenv.sh
# Let the user define every parameter via env vars if not, default to the values below
management_jmxremote_port=${MANAGEMENT_JMXREMOTE_PORT:-9001}
management_jmxremote_rmi_port=${MANAGEMENT_JMXREMOTE_RMI_PORT:-9001}
management_jmxremote_authenticate=${MANAGEMENT_JMXREMOTE_AUTHENTICATE:-false}
management_jmxremote_ssl=${MANAGEMENT_JMXREMOTE_SSL:-false}
java_rmi_server_hostname=${JAVA_RMI_SERVER_HOSTNAME:-localhost}
additional_catalina_opts=${ADDITIONAL_CATALINA_OPTS:-""}
# Let the user define every parameter via env vars if not, default to the values below
target_survivor_ratio=${TARGET_SURVIVOR_RATIO:-75}
survivor_ratio=${SURVIVOR_RATIO:-64}
max_tenuring_threshold=${MAX_TENURING_THRESHOLD:-3}
parallel_gc_threads=${PARALLEL_GC_THREADS:-4}
xms=${XMS:-1g}
xmx=${XMX:-2g}
additional_java_opts=${ADDITIONAL_JAVA_OPTS:-""}

if [ "${BUILD_GRAPHS}" = "True" ]; then
echo "###### Construct CATALINA_OPTS and JAVA_OPTS ######"
CATALINA_OPTS="-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=${management_jmxremote_port} \
-Dcom.sun.management.jmxremote.rmi.port=${management_jmxremote_rmi_port} \
-Dcom.sun.management.jmxremote.authenticate=${management_jmxremote_authenticate} \
-Dcom.sun.management.jmxremote.ssl=${management_jmxremote_ssl} \
-Djava.rmi.server.hostname=${java_rmi_server_hostname} \
${additional_catalina_opts}"
echo "CATALINA_OPTS: ${CATALINA_OPTS}"

JAVA_OPTS="-Djava.awt.headless=true \
-server -XX:TargetSurvivorRatio=${target_survivor_ratio} \
-XX:SurvivorRatio=${survivor_ratio} \
-XX:MaxTenuringThreshold=${max_tenuring_threshold} \
-XX:+UseG1GC \
-XX:+ScavengeBeforeFullGC \
-XX:ParallelGCThreads=${parallel_gc_threads} \
-Xms${xms} \
-Xmx${xmx} \
${additional_java_opts}"
echo "JAVA_OPTS: ${JAVA_OPTS}"



echo "###### ORS data preparation ######"
echo "Populating ORS_HOME with default files and folders"
mkdir -p "${ORS_HOME}"/{data,logs,graphs,conf} || echo "Could not create ${ORS_HOME} and folders"
chown -R "$(whoami)" "${ORS_HOME}" "${BASE_FOLDER}" || echo "Could not chown ${ORS_HOME} and ${BASE_FOLDER}"

# Remove existing graphs if BUILD_GRAPHS is set to True
if [ "${ors_build_graphs}" = "True" ]; then
echo "BUILD_GRAPHS set to True. Removing existing graphs"
rm -rf "${graphs:?}"/*
fi

echo "### openrouteservice configuration ###"
# Always overwrite the example config in case another one is present
cp -f "${ors_base}/tmp/ors-config.yml" "${ors_base}/ors-conf/ors-config-example.yml"
# Check for old .json configs
JSON_FILES=$(ls -d -- "${ors_base}/ors-conf/"*.json 2>/dev/null)
if [ -n "$JSON_FILES" ]; then
echo "Old .json config found. They're deprecated and will be replaced in ORS version 8."
echo "Please migrate to the new .yml example."
# If BASE_FOLDER is not the same as ORS_HOME, copy the default config to ORS_HOME
if [ "${BASE_FOLDER}" != "${ORS_HOME}" ]; then
echo "Updating the example config in ${ORS_HOME}/conf"
cp -f "${BASE_FOLDER}/config/example-ors-config.yml" "${ORS_HOME}/config/example-ors-config.yml"
fi

echo "###### ORS configuration ######"
# No config found. Use the base config
if [ ! -f "${ors_base}/ors-conf/ors-config.yml" ]; then
echo "Copy ors-config.yml"
cp -f "${ors_base}/tmp/ors-config.yml" "${ors_base}/ors-conf/ors-config.yml"
if [ ! -f "${ors_config_location}" ]; then
echo "No ors-config.yml found. Using default: ${ORS_HOME}/config/example-ors-config.yml"
cp -f "${ORS_HOME}/config/example-ors-config.yml" "${ORS_HOME}/config/ors-config.yml"
else
echo "Using custom ors-config.yml: ${ors_config_location}"
ORS_CONFIG_LOCATION=${ors_config_location}
fi

if [ ! -f "${ors_base}/ors-core/data/osm_file.pbf" ]; then
echo "Copy osm_file.pbf"
cp -f "${ors_base}/tmp/osm_file.pbf" "${ors_base}/ors-core/data/osm_file.pbf"
# No osm file found. Use the base osm file
if [ ! -f "${ORS_HOME}/files/osm-file.osm.gz" ]; then
echo "No osm-file.pbf found. Using default: ${ORS_HOME}/files/example_osm_file.osm.gz"
cp -f "${ORS_HOME}/files/example_osm_file.pbf" "${ORS_HOME}/files/osm-file.osm.gz"
else
echo "Using custom osm-file.osm.gz: ${ORS_HOME}/files/osm-file.osm.gz"
fi

# so docker can stop the process gracefully
exec "${catalina_base}"/bin/catalina.sh run
# Start the jar with the given arguments and add the ORS_HOME env var
echo "Starting ORS with ${jar_file} and ORS_HOME=${ORS_HOME}"
# shellcheck disable=SC2086 # we need word splitting here
exec java ${JAVA_OPTS} ${CATALINA_OPTS} -DORS_HOME="${ORS_HOME}" -DORS_CONFIG_LOCATION="${ORS_CONFIG_LOCATION}" -jar "${jar_file}" "$@"

0 comments on commit 7ffca83

Please sign in to comment.