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

17 thoughts on “Setting up a Docker container with Centos6 and Tomcat7

  1. Jen

    “…with Tomcat/Ubuntu, but not many for Centos/Ubuntu”
    Do you mean Tomcat/Centos instead of Centos/Ubuntu?

    Reply
    1. lreeder Post author

      @Jen – yes, thanks, good catch. I updated the post to read “Tomcat/Centos”.

      Thanks….. lreeder

      Reply
    1. aP

      What changes did you need to make to the docker file?
      When I execute I get the following error:

      /bin/sh: -c: line 0: syntax error near unexpected token ‘;&’
      /bin/sh: -c: line 0: ‘echo “JAVA_HOME=/opt/jdk1.7.0_72” >> /etc/environment’
      INFO[0131] The command [/bin/sh -c echo “”JAVA_HOME=/opt/jdk1.7.0_72” >> /etc/environment] returned a non-zero code: 1

      Reply
      1. aP

        I got around this issue by changing “>>” to >>
        Next issue I found was this error:

        Step 11 : ADD tomcat7 /etc/rd.d/init.d/
        INFO[0043] tomcat7: no such file or directory

        Reply
        1. lreeder Post author

          aP, sorry, “>>” was converted to escaped HTML >> at some point in the editing process. As you guessed, it should be “>>”. Also, step 11 “ADD tomcat7 /etc/rd.d/init.d/” is not necessary to get tomcat running. You can just remove that step from your Dockerfile. I fixed my example.

          Reply
  2. adeel

    wanted to ask 2 things..
    1- docker pull centos
    what this command will do?? shouldn’t we go with the instructions of this link?
    http://www.server-world.info/en/note?os=CentOS_6&p=docker&f=1

    2- 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
    ……
    …..
    RUN chown -Rf tomcat.tomcat /usr/share/tomcat7
    EXPOSE 8080

    where we will run these commands??

    Reply
    1. lreeder Post author

      adeel,

      Question #1: “docker pull centos” explicitly pulls the centos image down to your docker instance. The command on the server-world link you provided implicitly pulls the image when you try “docker run centos”. Both approaches result in a downloading a centos image.

      Question #2. “where will we run these commands?”. You don’t run those commands. You put them in a Dockerfile, and docker runs them when you create a new image with “docker build”. See https://docs.docker.com/reference/builder/ for more info on Dockerfiles.

      Reply
  3. adeel

    Thanx lreeder,
    i am able to create it on my local machine, and was also able to push it on my repo, but the issue i did face when i pull it to any other machine it gets pull and container does also crated but when i run mentioend command i find “” instead of IP.

    docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ docker.io/adeel7454/adeel_jdk_tomcat

    [root@docker ~]# docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ a4fb4b61f55a

    how can i fix this issue?

    Reply
  4. adeel

    “” means “no value”
    docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ docker.io/adeel7454/adeel_jdk_tomcat
    // here on my machine it shows no value
    [root@docker ~]# docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ a4fb4b61f55a
    // here on my machine it shows no value

    Reply
    1. lreeder Post author

      adeel, is your container running? If it’s not running, it won’t have an IP address, and “docker inspect …” will return an empty string. Use “docker ps” to verify the container is running.

      Reply
  5. Roche

    I uses Docker on window 8.1 with Virtual Box 5.0.9 r103669 and Boot2Docker. The container is running (checked with “docker ps”), and also the tomcat server. There is also port forwarding setting on VirtualBox. But when browse the server, connection timeout error occurs. I’ve already check with Firewall turned off. But still not ok. But on Ubuntu 14.04 environment, it is quite ok, speedy response. Pls help somebody. Thanks

    Reply
  6. Ash

    Hi,
    I have followed this approach and created a container. But I cannot access http://container-ip:8080 from the windows machine am using. I can ping container ip from the host linux machine where container is running. What am I missing? The ports are exposed and container is running but still cant access Tomcat page.

    Reply
  7. suranjan

    I have copied my application folder in webapps through Dockerfile. Though tomcat home /welcome page is accessible through browser. Application not running showing error 404. How can I solve it ? Thanks.

    Reply
  8. Manjeet

    i am able to create image and also able to run all the commands successfully.
    my generated ip is 172.17.0.2
    tomcat is started at port 8080

    but i am not able to ping the generated ip and tomcat is not running at http://172.17.0.2:8080

    thanks and Regards

    Reply
  9. David Parry

    I followed the instructions everything went smooth and tomcat is started INFO: Server startup in 666 ms
    I ran docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ tomcat7 returned
    172.17.0.2
    I type in http://172.17.0.2:8080 browser spins for sometime then get This site can’t be reached

    I ran docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    38809ecc07f3 centos6/tomcat7 “/usr/bin/sudo -u tom” 4 minutes ago Up 4 minutes 8080/tcp tomcat7

    Any help would be excellent newbie trying to learn

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *