Skip to content

Commit

Permalink
TRUNK-6083 Build and deploy docker image for openmrs-core, improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rkorytkowski committed Nov 10, 2022
1 parent 268708f commit ced56ca
Show file tree
Hide file tree
Showing 11 changed files with 5,332 additions and 217 deletions.
176 changes: 91 additions & 85 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,67 @@
# graphic logo is a trademark of OpenMRS Inc.

### Development Stage
FROM maven:3.8-jdk-11 as dev
WORKDIR /app

ENV DEPENDENCY_PLUGIN="org.apache.maven.plugins:maven-dependency-plugin:3.3.0"
FROM maven:3.8-amazoncorretto-8 as dev

RUN yum -y update && yum -y install tar gzip && yum clean all

# Setup Tini
ARG TARGETARCH
ARG TINI_VERSION=v0.19.0
ARG TINI_URL="https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini"
ARG TINI_SHA="93dcc18adc78c65a028a84799ecf8ad40c936fdfc5f2a57b1acda5a8117fa82c"
ARG TINI_SHA_ARM64="07952557df20bfd2a95f9bef198b445e006171969499a1d361bd9e6f8e5e0e81"
RUN if [ "$TARGETARCH" = "arm64" ] ; then TINI_URL="${TINI_URL}-arm64" TINI_SHA=${TINI_SHA_ARM64} ; fi \
&& curl -fsSL -o /usr/bin/tini ${TINI_URL} \
&& echo "${TINI_SHA} /usr/bin/tini" | sha256sum -c \
&& chmod +x /usr/bin/tini

# Setup Tomcat for development
ARG TOMCAT_VERSION=8.5.83
ARG TOMCAT_SHA="57cbe9608a9c4e88135e5f5480812e8d57690d5f3f6c43a7c05fe647bddb7c3b684bf0fc0efebad399d05e80c6d20c43d5ecdf38ec58f123e6653e443f9054e3"
RUN curl -o /tmp/apache-tomcat.tar.gz \
https://dlcdn.apache.org/tomcat/tomcat-8/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz \
&& echo "${TOMCAT_SHA} /tmp/apache-tomcat.tar.gz" | sha512sum -c \
&& mkdir -p /usr/local/tomcat && tar -xvzf /tmp/apache-tomcat.tar.gz -C /usr/local/tomcat/ --strip-components=1 \
&& rm -rf /tmp/apache-tomcat.tar.gz /usr/local/tomcat/webapps/*

WORKDIR /openmrs_core

ENV OPENMRS_SDK_PLUGIN="org.openmrs.maven.plugins:openmrs-sdk-maven-plugin:4.5.0"
ENV OPENMRS_SDK_PLUGIN_VERSION="4.5.0"
ENV MVN_ARGS_SETTINGS="-s /usr/share/maven/ref/settings-docker.xml"

# Setup SDK
RUN mvn org.openmrs.maven.plugins:openmrs-sdk-maven-plugin:setup-sdk -DbatchAnswers=n -B $MVN_ARGS_SETTINGS
COPY checkstyle.xml checkstyle-suppressions.xml CONTRIBUTING.md findbugs-include.xml LICENSE license-header.txt \
NOTICE.md README.md ruleset.xml SECURITY.md ./

# Copy poms to resolve dependencies
COPY pom.xml .

# Setup and cache SDK
RUN mvn $OPENMRS_SDK_PLUGIN:setup-sdk -N -DbatchAnswers=n $MVN_ARGS_SETTINGS

# Store dependencies in /usr/share/maven/ref/repository for re-use when running
# If mounting ~/.m2:/root/.m2 then the /usr/share/maven/ref content will be copied over from the image to /root/.m2
RUN mvn --non-recursive dependency:go-offline $MVN_ARGS_SETTINGS

# Copy remainig poms to satisfy dependencies
COPY liquibase/pom.xml ./liquibase/
COPY tools/pom.xml ./tools/
COPY test/pom.xml ./test/
COPY web/pom.xml ./web/
COPY api/pom.xml ./api/
COPY web/pom.xml ./web/
COPY webapp/pom.xml ./webapp/

# Exclude tools as it fails trying to fetch tools.jar
RUN mvn -pl !tools dependency:go-offline $MVN_ARGS_SETTINGS

# Resolve dependencies in order to cache them and run offline builds
# Store dependencies in /usr/share/maven/ref/repository for re-use when running
# If mounting ~/.m2:/root/.m2 then the m2 repo content will be copied over from the image
RUN mvn $DEPENDENCY_PLUGIN:resolve-plugins $DEPENDENCY_PLUGIN:resolve $MVN_ARGS_SETTINGS

ARG MVN_ARGS='install'

# Build the app using cached dependencies
# Append --build-arg MVN_ARGS='install -o' to change default maven arguments
# Append --build-arg MVN_ARGS='install' to change default maven arguments
# Build modules individually to benefit from caching
COPY checkstyle.xml checkstyle-suppressions.xml CONTRIBUTING.md findbugs-include.xml LICENSE license-header.txt \
NOTICE.md README.md ruleset.xml SECURITY.md ./
ARG MVN_ARGS='install'

# build the parent project first
# Build the parent project
RUN mvn --non-recursive $MVN_ARGS_SETTINGS $MVN_ARGS

# Build individually to benefit from caching
COPY liquibase ./liquibase/
RUN mvn -pl liquibase $MVN_ARGS_SETTINGS $MVN_ARGS

Expand All @@ -61,84 +88,63 @@ RUN mvn -pl web $MVN_ARGS_SETTINGS $MVN_ARGS
COPY webapp/ ./webapp/
RUN mvn -pl webapp $MVN_ARGS_SETTINGS $MVN_ARGS

WORKDIR /app/webapp
RUN mkdir -p /openmrs/distribution/openmrs_core/ \
&& cp /openmrs_core/webapp/target/openmrs.war /openmrs/distribution/openmrs_core/openmrs.war

# Clean up after the build to save space
RUN mvn clean $MVN_ARGS_SETTINGS

# Copy in the start-up scripts
COPY wait-for-it.sh startup-init.sh startup.sh startup-dev.sh /openmrs/
RUN chmod +x /openmrs/wait-for-it.sh && chmod +x /openmrs/startup-init.sh && chmod +x /openmrs/startup.sh \
&& chmod +x /openmrs/startup-dev.sh

EXPOSE 8080

# Startup jetty by default for the dev image
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/mvn-entrypoint.sh"]

# See startup-init.sh for all configurable environment variables
# TODO: Use Tomcat with spring devtools instead
CMD ["mvn", "jetty:run", "-o"]
CMD ["/openmrs/startup-dev.sh"]

### Production Stage
FROM tomcat:8.5-jdk8-adoptopenjdk-hotspot
FROM tomcat:8.5-jdk8-corretto

RUN apt-get update && apt-get install -y zip dumb-init \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/local/tomcat/webapps/*
RUN yum -y update && yum -y install shadow-utils && yum clean all && rm -rf /usr/local/tomcat/webapps/*

RUN groupadd -r openmrs \
&& useradd --no-log-init -r -g openmrs openmrs \
&& chown -R openmrs $CATALINA_HOME \
&& mkdir -p /openmrs/data/modules \
&& mkdir -p /openmrs/data/owa \
&& mkdir -p /openmrs/data/configuration \
&& chown -R openmrs /openmrs
# Setup Tini
ARG TARGETARCH
ARG TINI_VERSION=v0.19.0
ARG TINI_URL="https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini"
ARG TINI_SHA="93dcc18adc78c65a028a84799ecf8ad40c936fdfc5f2a57b1acda5a8117fa82c"
ARG TINI_SHA_ARM64="07952557df20bfd2a95f9bef198b445e006171969499a1d361bd9e6f8e5e0e81"
RUN if [ "$TARGETARCH" = "arm64" ] ; then TINI_URL="${TINI_URL}-arm64" TINI_SHA=${TINI_SHA_ARM64} ; fi \
&& curl -fsSL -o /usr/bin/tini ${TINI_URL} \
&& echo "${TINI_SHA} /usr/bin/tini" | sha256sum -c \
&& chmod +x /usr/bin/tini

# Copy in the start-up scripts
COPY wait-for-it.sh startup.sh /usr/local/tomcat/
RUN chmod -R 755 /usr/local/tomcat/wait-for-it.sh && chmod -R 755 /usr/local/tomcat/startup.sh
RUN sed -i '/Connector port="8080"/a URIEncoding="UTF-8" relaxedPathChars="[]|" relaxedQueryChars="[]|{}^\`"<>"' \
/usr/local/tomcat/conf/server.xml

USER openmrs
RUN adduser openmrs && mkdir -p /openmrs/data/modules \
&& mkdir -p /openmrs/data/owa \
&& mkdir -p /openmrs/data/configuration \
&& chown -R openmrs /openmrs

# Copy in the start-up scripts
COPY wait-for-it.sh startup-init.sh startup.sh /openmrs/
RUN chmod +x /openmrs/wait-for-it.sh && chmod +x /openmrs/startup-init.sh && chmod +x /openmrs/startup.sh

WORKDIR /openmrs

# All environment variables that are available to configure on this container are listed here
# for clarity. These list the variables supported, and the default values if not overridden

# These environment variables are appended to configure the Tomcat JAVA_OPTS
ENV OMRS_JAVA_MEMORY_OPTS="-XX:NewSize=128m"
ENV OMRS_JAVA_SERVER_OPTS="-Dfile.encoding=UTF-8 -server -Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true -Djava.awt.headlesslib=true"

# These environment variables are used to create the openmrs-server.properties file, which controls how OpenMRS initializes
ENV OMRS_CONFIG_ADD_DEMO_DATA="false"
ENV OMRS_CONFIG_ADMIN_USER_PASSWORD="Admin123"
ENV OMRS_CONFIG_AUTO_UPDATE_DATABASE="true"
ENV OMRS_CONFIG_CREATE_DATABASE_USER="false"
ENV OMRS_CONFIG_CREATE_TABLES="false"
ENV OMRS_CONFIG_HAS_CURRENT_OPENMRS_DATABASE="true"
ENV OMRS_CONFIG_INSTALL_METHOD="auto"
ENV OMRS_CONFIG_MODULE_WEB_ADMIN="true"

# These variables are specific to database connections
# Supported values for OMRS_CONFIG_CONNECTION_TYPE are "mysql" and "postgresql"
# other values are treated as MySQL
ENV OMRS_CONFIG_CONNECTION_TYPE="mysql"
ENV OMRS_CONFIG_CONNECTION_USERNAME="openmrs"
ENV OMRS_CONFIG_CONNECTION_PASSWORD="openmrs"
ENV OMRS_CONFIG_CONNECTION_SERVER="localhost"
ENV OMRS_CONFIG_CONNECTION_DATABASE="openmrs"

# These environment variables can be used to customise the database connection.
# Their default values depend on which database you are using.
# OMRS_CONFIG_CONNECTION_DRIVER_CLASS
# OMRS_CONFIG_CONNECTION_PORT
# OMRS_CONFIG_CONNECTION_ARGS
# OMRS_CONFIG_CONNECTION_EXTRA_ARGS
#
# If you really need complete control, you can just set
# OMRS_CONFIG_CONNECTION_URL to whatever the URL should be

# These environment variables are meant to enable developer settings
# OMRS_DEV_DEBUG_PORT

# Additional environment variables as needed. This should match the name of the distribution supplied OpenMRS war file
ENV OMRS_WEBAPP_NAME="openmrs"

RUN sed -i '/Connector port="8080"/a URIEncoding="UTF-8" relaxedPathChars="[]|" relaxedQueryChars="[]|{}^\`"<>"' /usr/local/tomcat/conf/server.xml

COPY --from=dev /app/LICENSE LICENSE
COPY --from=dev /openmrs_core/LICENSE LICENSE
# Copy the app
COPY --from=dev /app/webapp/target/openmrs.war /openmrs/distribution/openmrs_core/openmrs.war
COPY --from=dev /openmrs/distribution/openmrs_core/openmrs.war /openmrs/distribution/openmrs_core/openmrs.war

EXPOSE 8080

CMD ["dumb-init", "/usr/local/tomcat/startup.sh"]
ENTRYPOINT ["/usr/bin/tini", "--"]

# See startup-init.sh for all configurable environment variables
CMD ["/openmrs/startup.sh"]

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public SortedMap<String, String> getSystemVariables() throws APIException {
catch (UnknownHostException e) {
systemVariables.put("OPENMRS_HOSTNAME", "Unknown host: " + e.getMessage());
}

systemVariables.put("OPENMRS_VERSION", String.valueOf(OpenmrsConstants.OPENMRS_VERSION));
systemVariables.put("DATABASE_NAME", OpenmrsConstants.DATABASE_NAME);
systemVariables.put("DATABASE_BUSINESS_NAME", OpenmrsConstants.DATABASE_BUSINESS_NAME);
Expand Down
15 changes: 13 additions & 2 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ services:
ports:
- "3306:3306"
restart: "no"
volumes:
# Comment out if DB should be created from scratch
- ./initial_test_db.sql:/docker-entrypoint-initdb.d/initial_test_db.sql

openmrs-core:
api:
image: openmrs/openmrs-core:${TAG:-dev}
build:
target: dev
context: .
context: .
ports:
- "8080:8080"
- "8000:8000"
environment:
OMRS_DEV_DEBUG_PORT: ${OMRS_DEV_DEBUG_PORT:-8000}
OMRS_CREATE_TABLES: ${OMRS_CREATE_TABLES:-false}

OMRS_BUILD: ${OMRS_BUILD:-true}
OMRS_BUILD_GOALS: ${OMRS_BUILD_GOALS:-}
OMRS_BUILD_ARGS: ${OMRS_BUILD_ARGS:-}
volumes:
- ~/.m2:/root/.m2
- ./:/openmrs_core
restart: "no"
32 changes: 18 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,36 @@ services:
image: mariadb:10.3
command: "mysqld --character-set-server=utf8 --collation-server=utf8_general_ci"
environment:
MYSQL_DATABASE: ${OPENMRS_DB_NAME:-openmrs}
MYSQL_USER: ${OPENMRS_DB_USER:-openmrs}
MYSQL_PASSWORD: ${OPENMRS_DB_PASSWORD:-openmrs}
MYSQL_ROOT_PASSWORD: ${OPENMRS_DB_ROOT_PASSWORD:-openmrs}
MYSQL_DATABASE: ${OMRS_DB_NAME:-openmrs}
MYSQL_USER: ${OMRS_DB_USER:-openmrs}
MYSQL_PASSWORD: ${OMRS_DB_PASSWORD:-openmrs}
MYSQL_ROOT_PASSWORD: ${OMRS_DB_ROOT_PASSWORD:-openmrs}
healthcheck:
test: "mysql --user=${OMRS_DB_USER:-openmrs} --password=${OMRS_DB_PASSWORD:-openmrs} --execute \"SHOW DATABASES;\""
interval: 3s
timeout: 5s
retries: 3
volumes:
- db-data:/var/lib/mysql

openmrs-core:
image: openmrs/openmrs-core:${TAG:-nightly}
api:
image: openmrs/openmrs-core:${TAG:-head}
build: .
depends_on:
- db
ports:
- "8080:8080"
environment:
OMRS_CONFIG_MODULE_WEB_ADMIN: "true"
OMRS_CONFIG_AUTO_UPDATE_DATABASE: "true"
OMRS_CONFIG_CREATE_TABLES: "true"
OMRS_CONFIG_CONNECTION_URL: ${OPENMRS_DB_URL}
OMRS_CONFIG_CONNECTION_SERVER: db
OMRS_CONFIG_CONNECTION_DATABASE: ${OPENMRS_DB_NAME:-openmrs}
OMRS_CONFIG_CONNECTION_USERNAME: ${OPENMRS_DB_USER:-openmrs}
OMRS_CONFIG_CONNECTION_PASSWORD: ${OPENMRS_DB_PASSWORD:-openmrs}
OMRS_DB_HOSTNAME: ${OMRS_DB_HOSTNAME:-db}
OMRS_DB_NAME: ${OMRS_DB_NAME:-openmrs}
OMRS_DB_USERNAME: ${OMRS_DB_USERNAME:-openmrs}
OMRS_DB_PASSWORD: ${OMRS_DB_PASSWORD:-openmrs}
OMRS_ADMIN_USER_PASSWORD: ${OMRS_ADMIN_USER_PASSWORD-Admin123}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/openmrs"]
interval: 3s
timeout: 5s
retries: 3
volumes:
- openmrs-data:/openmrs/data/

Expand Down
Loading

0 comments on commit ced56ca

Please sign in to comment.