Skip to content

Commit

Permalink
Persistence with volumes & multi-stage builds
Browse files Browse the repository at this point in the history
  • Loading branch information
ismisepaul authored and markdenihan committed Feb 1, 2021
1 parent fd5121f commit 8c55d29
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
57 changes: 31 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
ARG TOMCAT_DOCKER_VERSION
FROM tomcat:${TOMCAT_DOCKER_VERSION}

ENV RUN_USER tomcat

RUN apt-get -qq update && apt-get install -y patch

RUN adduser --system --group ${RUN_USER} --home ${CATALINA_HOME}
RUN chown -R ${RUN_USER}:${RUN_GROUP} $CATALINA_HOME
USER ${RUN_USER}

FROM docker AS builder
ARG TLS_KEYSTORE_FILE
ARG TLS_KEYSTORE_PASS
ARG ALIAS
ARG HTTPS_PORT
ARG DB_DRIVER=org.gjt.mm.mysql.Driver
ARG DB_SCHEMA=core
ARG PROPS_MYSQL=/usr/local/tomcat/conf/database.properties
ARG PROPS_MONGO=/usr/local/tomcat/conf/mongo.properties

ARG MYSQL_USER
ARG MYSQL_PASS
ARG MYSQL_URI

ARG MONGO_HOST
ARG MONGO_PORT
ARG MONGO_CONN_TIMEOUT
ARG MONGO_SOCK_TIMEOUT
ARG MONGO_SVR_TIMEOUT

ARG TLS_KEYSTORE_FILE
ARG TLS_KEYSTORE_PASS
ARG ALIAS
ARG HTTPS_PORT
USER root
WORKDIR /workdir

RUN printf "databaseConnectionURL=$MYSQL_URI/\nDriverType=$DB_DRIVER\ndatabaseSchema=$DB_SCHEMA\ndatabaseUsername=$MYSQL_USER\ndatabasePassword=$MYSQL_PASS\ndatabaseOptions=useUnicode=true&character_set_server=utf8mb4\n" >> $PROPS_MYSQL
RUN printf "connectionHost=$MONGO_HOST\nconnectionPort=$MONGO_PORT\ndatabaseName=shepherdGames\nconnectTimeout=$MONGO_CONN_TIMEOUT\nsocketTimeout=$MONGO_SOCK_TIMEOUT\nserverSelectionTimeout=$MONGO_SVR_TIMEOUT" >> $PROPS_MONGO
COPY target/owaspSecurityShepherd.war ROOT.war
COPY target/docker/tomcat/$TLS_KEYSTORE_FILE $TLS_KEYSTORE_FILE
COPY docker/tomcat/serverxml.patch serverxml.patch
COPY docker/tomcat/webxml.patch webxml.patch

RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY target/owaspSecurityShepherd.war /usr/local/tomcat/webapps/ROOT.war
COPY target/docker/tomcat/$TLS_KEYSTORE_FILE /usr/local/tomcat/conf/$TLS_KEYSTORE_FILE
RUN printf "databaseConnectionURL=$MYSQL_URI/\nDriverType=$DB_DRIVER\ndatabaseSchema=$DB_SCHEMA\ndatabaseUsername=$MYSQL_USER\ndatabasePassword=$MYSQL_PASS\ndatabaseOptions=useUnicode=true&character_set_server=utf8mb4\n" >> database.properties
RUN printf "connectionHost=$MONGO_HOST\nconnectionPort=$MONGO_PORT\ndatabaseName=shepherdGames\nconnectTimeout=$MONGO_CONN_TIMEOUT\nsocketTimeout=$MONGO_SOCK_TIMEOUT\nserverSelectionTimeout=$MONGO_SVR_TIMEOUT" >> mongo.properties
RUN sed -i 's/keystoreFile="conf\/TLS_KEYSTORE_FILE" keystorePass="TLS_KEYSTORE_PASS" keyAlias="ALIAS">/keystoreFile="conf\/'"$TLS_KEYSTORE_FILE"'" keystorePass="'"$TLS_KEYSTORE_PASS"'" keyAlias="'"$ALIAS"'">/g' serverxml.patch &&\
sed -i 's/redirectPort="HTTPS_PORT" \/>/redirectPort="'"$HTTPS_PORT"'" \/>/g' serverxml.patch


FROM tomcat:${TOMCAT_DOCKER_VERSION}
COPY --from=builder /workdir/ROOT.war /usr/local/tomcat/webapps/
COPY --from=builder /workdir/$TLS_KEYSTORE_FILE /usr/local/tomcat/conf/
COPY --from=builder /workdir/serverxml.patch /usr/local/tomcat/conf/
COPY --from=builder /workdir/webxml.patch /usr/local/tomcat/conf/
COPY --from=builder /workdir/database.properties /usr/local/tomcat/conf/
COPY --from=builder /workdir/mongo.properties /usr/local/tomcat/conf/

COPY docker/tomcat/serverxml.patch /usr/local/tomcat/conf/serverxml.patch
RUN sed -i 's/keystoreFile="conf\/TLS_KEYSTORE_FILE" keystorePass="TLS_KEYSTORE_PASS" keyAlias="ALIAS">/keystoreFile="conf\/'"$TLS_KEYSTORE_FILE"'" keystorePass="'"$TLS_KEYSTORE_PASS"'" keyAlias="'"$ALIAS"'">/g' /usr/local/tomcat/conf/serverxml.patch &&\
sed -i 's/redirectPort="HTTPS_PORT" \/>/redirectPort="'"$HTTPS_PORT"'" \/>/g' /usr/local/tomcat/conf/serverxml.patch &&\
patch /usr/local/tomcat/conf/server.xml /usr/local/tomcat/conf/serverxml.patch
ENV RUN_USER tomcat
RUN apt-get -qq update && apt-get install -y patch
RUN adduser --system --group ${RUN_USER} --home ${CATALINA_HOME}
RUN chown -R ${RUN_USER}:${RUN_GROUP} $CATALINA_HOME
USER ${RUN_USER}

COPY docker/tomcat/webxml.patch /usr/local/tomcat/conf/webxml.patch
RUN rm -rf /usr/local/tomcat/webapps/ROOT
RUN patch /usr/local/tomcat/conf/server.xml /usr/local/tomcat/conf/serverxml.patch
RUN patch /usr/local/tomcat/conf/web.xml /usr/local/tomcat/conf/webxml.patch

EXPOSE 8080 8443
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ services:
db:
image: ${IMAGE_MYSQL}
container_name: ${CONTAINER_MYSQL}
volumes:
- data:/var/lib/mysql
env_file:
- .env
build:
Expand All @@ -25,6 +27,8 @@ services:
web:
image: ${IMAGE_TOMCAT}
container_name: ${CONTAINER_TOMCAT}
volumes:
- conf:/usr/local/tomcat/conf
env_file:
- .env
build:
Expand All @@ -48,3 +52,6 @@ services:
- $HTTPS_PORT:8443
depends_on:
- db
volumes:
data:
conf:
10 changes: 7 additions & 3 deletions docker/mongo/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
ARG MONGODB_VERSION

FROM docker AS builder
USER root
WORKDIR /workdir
COPY target/moduleSchemas.js moduleSchemas.js

FROM mongo:${MONGODB_VERSION}
COPY --from=builder /workdir/moduleSchemas.js /docker-entrypoint-initdb.d/

ENV RUN_USER mongodb
ENV RUN_GROUP mongodb
Expand All @@ -8,9 +15,6 @@ RUN chown -R ${RUN_USER}:${RUN_GROUP} "/etc/mongod.conf.orig"
RUN chown -R ${RUN_USER}:${RUN_GROUP} "/docker-entrypoint-initdb.d"

USER ${RUN_USER}

COPY target/moduleSchemas.js /docker-entrypoint-initdb.d/moduleSchemas.js

RUN sed -i 's/\/\/REMOVE/ /g' /docker-entrypoint-initdb.d/moduleSchemas.js

ENTRYPOINT ["docker-entrypoint.sh"]
Expand Down
38 changes: 22 additions & 16 deletions docker/mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
ARG MYSQL_VERSION

FROM docker AS builder
USER root
WORKDIR /workdir
COPY target/coreSchema.sql coreSchema.sql
COPY target/moduleSchemas.sql moduleSchemas.sql
RUN { \
echo '[mysqld]'; \
echo 'skip-host-cache'; \
echo 'datadir = /var/lib/mysql'; \
echo '!includedir /etc/mysql/conf.d/'; \
} > my.cnf


FROM mysql:${MYSQL_VERSION}
ARG MYSQL_ROOT_PASSWORD=$MYSQL_PASS
ARG CONTAINER_TOMCAT
ARG DOCKER_NETWORK_NAME

COPY --from=builder /workdir/coreSchema.sql /docker-entrypoint-initdb.d/
COPY --from=builder /workdir/moduleSchemas.sql /docker-entrypoint-initdb.d/

ENV RUN_USER mysql
ENV RUN_GROUP mysql
Expand All @@ -9,23 +29,9 @@ RUN chown -R ${RUN_USER}:${RUN_GROUP} ${MYSQL_HOME}
RUN chown -R ${RUN_USER}:${RUN_GROUP} "/docker-entrypoint-initdb.d"

USER ${RUN_USER}

ARG MYSQL_ROOT_PASSWORD=$MYSQL_PASS
ARG CONTAINER_TOMCAT
ARG DOCKER_NETWORK_NAME

COPY target/coreSchema.sql /docker-entrypoint-initdb.d/coreSchema.sql
COPY target/moduleSchemas.sql /docker-entrypoint-initdb.d/moduleSchemas.sql

RUN sed -i 's/@'\''localhost'\''/@'\'''%''\''/g' /docker-entrypoint-initdb.d/moduleSchemas.sql

RUN mkdir -p /etc/mysql/conf.d \
&& { \
echo '[mysqld]'; \
echo 'skip-host-cache'; \
echo 'datadir = /var/lib/mysql'; \
echo '!includedir /etc/mysql/conf.d/'; \
} > /etc/mysql/my.cnf
RUN mkdir -p /etc/mysql/conf.d
COPY --from=builder /workdir/my.cnf /etc/mysql/

ENTRYPOINT ["docker-entrypoint.sh"]

Expand Down

0 comments on commit 8c55d29

Please sign in to comment.