cerberus-core icon indicating copy to clipboard operation
cerberus-core copied to clipboard

Cleaner docker image

Open GregoireW opened this issue 5 years ago • 1 comments

I just check the Dockerfile for tomcat and there is some thing you can do much better.

As one of the thing need a jar file to be created, I do not make this via PR.

Tomcat can replace placeholder: Default is per properties

Why not create a small jar file with the code:

class ReplaceFromEnvironment implements IntrospectionUtils.PropertySource{
    public String getProperty(String key) {
        return System.getenv().get(key);
    }
}

Adding a line in the $CATALINA_BASE/conf/catalina.properties to enable this class

org.apache.tomcat.util.digester.PROPERTY_SOURCE=my.package.ReplaceFromEnvironment

With that, there is no need for your launcher.

Then you can optimize your image:

  • Replacing ENV by ARG for building variable
  • Clubing RUN to prevent working file in the final image ( your current image have ~ 150Mb of unneeded bloat file)
  • Removing the non needed entrypoint, to keep the logging to stdout and not in file

Your Dockerfile could look like:

FROM tomcat:8-jre8-alpine

ENV CATALINA_OPTS="-Dorg.cerberus.environment=prd -Dorg.cerberus.authentification=none -Xmx1024m"

ENV DATABASE_HOST cerberus-db-mysql
ENV DATABASE_PORT 3306
ENV DATABASE_NAME cerberus
ENV DATABASE_USER cerberus
ENV DATABASE_PASSWORD toto

ARG CERBERUS_NAME=Cerberus
ARG CERBERUS_VERSION=4.0
ARG CERBERUS_PACKAGE_NAME=${CERBERUS_NAME}-${CERBERUS_VERSION}

ARG MYSQL_JAVA_CONNECTOR_VERSION=5.1.20
ARG MYSQL_JAVA_CONNECTOR_NAME=mysql-connector-java-${MYSQL_JAVA_CONNECTOR_VERSION}

COPY *.xml /usr/local/tomcat/conf/

RUN echo "Tomcat placeholder from env and not properties" && \
    wget -P /usr/local/tomcat/lib https://..../..../placeholder-env.jar && \
    echo "" >> /usr/local/tomcat/conf/catalina.properties && \
    echo "org.apache.tomcat.util.digester.PROPERTY_SOURCE=my.package.ReplaceFromEnvironment" >> /usr/local/tomcat/conf/catalina.properties

RUN echo "Download and install MySQL JDBC Drivers" && \
    wget -P /tmp/ https://downloads.mysql.com/archives/get/file/${MYSQL_JAVA_CONNECTOR_NAME}.zip && \
    unzip -q -d /tmp/ /tmp/${MYSQL_JAVA_CONNECTOR_NAME}.zip && \
    mv /tmp/${MYSQL_JAVA_CONNECTOR_NAME}/${MYSQL_JAVA_CONNECTOR_NAME}-bin.jar  /usr/local/tomcat/lib/ && \
    rm /tmp/* -rf

RUN echo "Download & install Cerberus Application" && \
    wget -P /tmp/ https://github.com/cerberustesting/cerberus-source/releases/download/cerberus-testing-${CERBERUS_VERSION}/Cerberus-${CERBERUS_VERSION}.zip && \
    unzip -q -d /tmp /tmp/${CERBERUS_PACKAGE_NAME}.zip && \
    rm -rf /usr/local/tomcat/webapps/* && \
    cp /tmp/${CERBERUS_PACKAGE_NAME}/${CERBERUS_PACKAGE_NAME}.war /usr/local/tomcat/webapps/ROOT.war && \
    echo "clean temp directory" && \
    rm /tmp/* -rf

GregoireW avatar Jul 02 '19 11:07 GregoireW

Thanks a lot for the feedback. We will integrate those improvements ASAP.

vertigo17 avatar Aug 13 '19 19:08 vertigo17