Skip to content

Instantly share code, notes, and snippets.

@alessandromaggio
Last active February 21, 2022 11:57
Show Gist options
  • Save alessandromaggio/7a05f90c1cf1c579d0630288e0ca14b6 to your computer and use it in GitHub Desktop.
Save alessandromaggio/7a05f90c1cf1c579d0630288e0ca14b6 to your computer and use it in GitHub Desktop.
A dockerfile to run a Tomcat Java application
# Author: Alessandro Maggio
# See more on https://ictshore.com
# A Java Docker Tutorial and more explanation here: https://www.ictshore.com/devops/java-docker-tutorial-tomcat/
# IMPORTANT:
# 1. Replace my-package-name.war with the name/path of your tomcat package
# 2. Replace tomcat-conf.xml with name/path of your custom tomcat configuration file
FROM debian:stretch
# Install dependencies need
# - default-jre: Java Runtime
# - libtcnative-1: to optimize tomcat for production (optional)
# - wget: to download tomcat
# - curl: to dynamically identify the latest version of tomcat
# - unzip: if you want to unzip your war package (otherwise remove it)
RUN \
apt-get update && \
apt-get upgrade -y && \
apt-get install -y default-jre && \
apt-get install -y libtcnative-1 && \
apt-get install -y wget && \
apt-get install -y curl && \
apt-get install -y unzip
# We set the environment variable JAVA_HOME, this allow us to run Java
ENV JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre"
# All this junk to install tomcat
# Line by line:
# 1. Identify the latest release of Tomcat 9 (see "greep v9" part)
# 2. Actually download the .tar.gz of tomcat
# 3. Extract it
# 4. Remove the compressed file
# 5. Move the extracted folder to the tomcat root folder (where it should be)
RUN \
TOMCAT_VER=`curl --silent http://mirror.vorboss.net/apache/tomcat/tomcat-9/ | grep v9 -m 1 | awk '{split($5,c,">v") ; split(c[2],d,"/") ; print d[1]}'` && \
wget -N http://mirror.vorboss.net/apache/tomcat/tomcat-9/v${TOMCAT_VER}/bin/apache-tomcat-${TOMCAT_VER}.tar.gz &&\
tar xzf apache-tomcat-${TOMCAT_VER}.tar.gz && \
rm -f apache-tomcat-${TOMCAT_VER}.tar.gz && \
mv apache-tomcat-${TOMCAT_VER}/ /opt/tomcat
# We set CATALINA_HOME and add it to PATH
# Allow us to run tomcat, and enable to launch tomcat anywhere in the CLI
ENV CATALINA_HOME="/opt/tomcat" \
PATH="$PATH:/opt/tomcat/bin"
# We remove any unwanted default webapp
RUN rm -fr /opt/tomcat/webapps/*
# We copy our WAR application inside tomcat webapps
COPY target/my-package-name.war /opt/tomcat/webapps/my-package-name.war
# We copy our custom tomcat configuration
COPY tomcat-conf.xml /opt/tomcat/conf/server.xml
# We say that, by default, this container will launch tomcat on startup
# As the way docker works, if tomcat stops to run or crashes, the container crashes as well
# In this way, if you see the container running, you know tomcat must be running
CMD ["catalina.sh", "run"]
# By default, we expose HTTP (port 80) and HTTPS (port 443)
EXPOSE 80 443
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment