Category Archives: Docker

Setting up a Docker container with Centos6 and Tomcat7

At work, all of our production machines are running Centos.  There are a few tutorials out there for setting up a Docker container with Tomcat/Ubuntu, but not many for Tomcat/Centos. I wanted to start using Docker containers for tomcat apps in development and test environments, but I wanted to use Centos 6 instead of Ubuntu since it more closely matches our production environment. Here’s what I did to quickly build and run a container with Tomcat 7 on top of Centos 6.

First, pull the Centos images from Docker with:

docker pull centos

You can use OpenJDK to run Tomcat, but we’ve standardized on the Oracle JDK, and prefer it since it has more community support and more options for GC and tuning.   The downside about Centos, unlike Ubuntu, is there are no standard packages for installing the the Oracle JDK and Tomcat. As a result, you’ll need to download those applications separately. While you can download those directly to the image as you build it, I prefer to put them on the host, as it makes debugging image builds faster, and gives me more control over what’s installed, and you don’t need to install tools like wget to download them if you don’t want that on your image.

First, download the Oracle JDK.  I’m using the latest JDK7 version.  Change JDK versions as needed.  Download with your web browser or use wget for command line sweetness like this:

% wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u72-b14/jdk-7u72-linux-x64.tar.gz"

Now download Tomcat7 with the following wget command or use your browser.  Change the Apache mirror host from apache.cs.utah.edu as needed.  You can get a list of other mirrors from https://tomcat.apache.org/download-70.cgi

wget http://apache.cs.utah.edu/tomcat/tomcat-7/v7.0.57/bin/apache-tomcat-7.0.57.tar.gz

Create a working folder and place both Tomcat tarball and Oracle JDK files in the same folder.

Finally copy this Dockerfile to the same folder where you put the JDK7 and Tomcat7 tarballs and tomcat7 script:

FROM centos:centos6
MAINTAINER lreeder

#Helpful utils, but only sudo is required
#RUN yum -y install tar
#RUN yum -y install vim
#RUN yum -y install nc
RUN yum -y install sudo

######## JDK7

#Note that ADD uncompresses this tarball automatically
ADD jdk-7u72-linux-x64.tar.gz /opt
WORKDIR /opt/jdk1.7.0_72
RUN alternatives --install /usr/bin/java java /opt/jdk1.7.0_72/bin/java 1
RUN alternatives --install /usr/bin/jar jar /opt/jdk1.7.0_72/bin/jar 1
RUN alternatives --install /usr/bin/javac javac /opt/jdk1.7.0_72/bin/javac 1
RUN echo "JAVA_HOME=/opt/jdk1.7.0_72" >> /etc/environment

######## TOMCAT

#Note that ADD uncompresses this tarball automatically
ADD apache-tomcat-7.0.57.tar.gz /usr/share
WORKDIR /usr/share/
RUN mv  apache-tomcat-7.0.57 tomcat7
RUN echo "JAVA_HOME=/opt/jdk1.7.0_72/" >> /etc/default/tomcat7
RUN groupadd tomcat
RUN useradd -s /bin/bash -g tomcat tomcat
RUN chown -Rf tomcat.tomcat /usr/share/tomcat7
EXPOSE 8080

Build your image from the Dockerfile with

docker build --rm=true -t centos6/tomcat7 .

And now start Tomcat with this, which names the container “tomcat7” and starts tomcat in the foreground so the container process doesn’t exit:

docker run  --rm=true -t -i --name tomcat7 centos6/tomcat7  /usr/bin/sudo -u tomcat /usr/share/tomcat7/bin/catalina.sh run

Find out the IP address for the running container:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' tomcat7
172.17.0.54

Assuming it’s “172.17.0.54” like mine was, you can point your browser to http://172.17.0.54:8080 and see the Tomcat welcome page.

Tomcat Welcome Page

Tomcat Welcome Page