Friday, January 12, 2018

Dockerize Tomcat/Tom EE and Oracle database applications

Docker ... Docker -- everywhere ..

Docker has gained quite a bit of popularity in simplifying operations for micro services applications. It also helps reduce cost of development and testing of  applications. It improves developer productivity by it’s  portable characteristics and  allowing them to create local development environment that mimics production.

In this article, I will show how you can containerize or Dockerize a simple web application application that uses Apache Tomcat / Tom EE and Oracle database. This article assumes that you have Docker installed.

If you want to get started with Docker, review their getting started guide.

The Application

The following diagram shows the architecture for my application. I have several micro-services those are deployed in a separate Tom EE clusters and they depend on Oracle Database.  

In this blog, I will deploy all my micro services into a single container and these micro services depend on an Oracle database that is running in a separate container.  In my next blog, I will write about how to configure Oracle database in Docker.

In a future blog, I will show how you can deploy multiple services in different containers and how they can communicate with each other. 

Application Content 

Here are the war files that make different micro-services for my application.

Micro service
WAR running in Tomcat
Web front
Credit Card

Database Configuration

The micro services depend on a database running on a separate container named orcldb.  The database is connected to a Docker bridgenetwork named appnet.

Here is the content of the context.xml (DataSource configuration) that enabled Tom EE to connect to Oracle Database.

="jdbc/HRDS" type="javax.sql.DataSource"
          driverClassName="    "
          url="jdbc:oracle:thin:@//orcldb:1521/ "

Also Tomcat needs Oracle JDBC drive to connect to Oracle database and hence you need to deploy the JDBC driver (ojdbc6- in the $CATALINA_HOME/lib of the Tom EE server.

The Dockerfile – Building your Docker image

Let’s see how you can use Docker to build an image that contains all artifacts you need to run your application.

You can download the Dockerfile from GitHub

Following is the content of my Dockerfile. This downloads JRE image from Docker repository, Tom EE from Apache Maven and copies all application contents (WAR files), JDBC driver, context files. You have to make appropriate changes for your environment that includes the values in the context.xml and additional JARs you might need.

FROM java:8-jre

ENV PATH /usr/local/tomee7/bin:$PATH
RUN mkdir -p /usr/local/tomee7

WORKDIR /usr/local/tomee7

223D3A74B068ECA354DC385CE126833F9CF64915 \
    678F2D98F1FD9643811639FB622B8F2D043F71D8 \
    7A2744A8A9AAF063C23EB7868EBE7DBE8D050EEF \
    82D8419BA697F0E7FB85916EE91287822FDB81B1 \
    9056B710F1E332780DE7AF34CBAEBE39A46C4CA1 \
    A57DAF81C1B69921F4BA8723A8DE0A4DB863A7C1 \
    B7574789F5018690043E6DD9C212662E12F3E1DD \
    B8B301E6105DF628076BD92C5483E55897ABD9B9 \
    BDD0BBEB753192957EFC5F896A62FC8EF17D8FEF \
    C23A3F6F595EBD0F960270CC997C8F1A5BE6E4C1 \
    D11DF12CC2CA4894BDE638B967C1227A2678363C \
    DBCCD103B8B24F86FFAAB025C8BB472CD297D428 \
    F067B8140F5DD80E1D3B5D92318242FE9A0B1183 \

RUN set -xe \
     for key in $VALID_GPG_KEYS; do \
        gpg --keyserver --recv-keys "$key"; \

RUN set -x \
       curl -fSL -o tomee.tar.gz.asc \
         curl -fSL -o tomee.tar.gz \
     gpg --batch --verify tomee.tar.gz.asc tomee.tar.gz \
         tar -zxf tomee.tar.gz \
         mv apache-tomee-plus-7.0.4/* /usr/local/tomee7 \
         rm -Rf apache-tomee-plus-7.0.4 \
         rm bin/*.bat \
         rm tomee.tar.gz*
COPY *.war /usr/local/tomee7/webapps/
COPY context.xml /usr/local/tomee7/conf
COPY ojdbc6- /usr/local/tomee7/lib/
CMD ["", "run"]

Alternate Dockerfile with my local tar of TomEE

Here is an alternative Dockerfile that uses a Tar file that contains binary files for Tom EE version. 

FROM java:8-jre
ENV PATH /usr/local/tomee7/bin:$PATH
RUN mkdir -p /usr/local/tomee7

WORKDIR /usr/local/tomee7
COPY tomee7.tar /usr/local/tomee7
RUN tar xvf tomee7.tar
COPY *.war /usr/local/tomee7/webapps/
COPY context.xml /usr/local/tomee7/conf
RUN mkdir -p /usr/local/$host_name
COPY ojdbc6- /usr/local/tomee7/lib/

CMD ["", "run"]

Build Your Image

You have to change to your directory that has the Dockerfile and you can use the following command to build your image. This assumes that all files such as all .war modules, context.xml, JDBC JAR files are 

docker build . -t orderapp

You will output like this. This might take few minutes to pull/download images for JRE and Tom EE from Apache Maven.

Sending build context to Docker daemon  106.4MB
Step 1/16 : FROM java:8-jre
 ---> e44d62cf8862
Step 2/16 : ENV PATH /usr/local/tomee7/bin:$PATH
 ---> Using cache
 ---> 1f4e8721dfa6
Step 3/16 : RUN mkdir -p /usr/local/tomee7
 ---> Using cache
 ---> 353199e9f026
Step 4/16 : WORKDIR /usr/local/tomee7
 ---> Using cache
 ---> bc846fadf403
Step 5/16 : ENV GPG_KEYS 223D3A74B068ECA354DC385CE126833F9CF64915     678F2D98F1FD9643811639FB622B8F2D043F71D8     7A2744A8A9AAF063C23EB7868EBE7DBE8D050EEF     82D8419BA697F0E7FB85916EE91287822FDB81B1     9056B710F1E332780DE7AF34CBAEBE39A46C4CA1     A57DAF81C1B69921F4BA8723A8DE0A4DB863A7C1     B7574789F5018690043E6DD9C212662E12F3E1DD     B8B301E6105DF628076BD92C5483E55897ABD9B9     BDD0BBEB753192957EFC5F896A62FC8EF17D8FEF     C23A3F6F595EBD0F960270CC997C8F1A5BE6E4C1     D11DF12CC2CA4894BDE638B967C1227A2678363C     DBCCD103B8B24F86FFAAB025C8BB472CD297D428     F067B8140F5DD80E1D3B5D92318242FE9A0B1183     FAA603D58B1BA4EDF65896D0ED340E0E6D545F97
 ---> Using cache
 ---> a8c51cf94777
Step 6/16 : RUN set -xe     && for key in $GPG_KEYS; do         gpg --keyserver --recv-keys "$key";     done
 ---> Using cache
 ---> ea35e9b7b56c
Step 7/16 : RUN set -x && curl -fSL -o tomee7.tar.gz.asc    && curl -fSL -o tomee7.tar.gz     && gpg --batch --verify tomee7.tar.gz.asc tomee7.tar.gz     && tar -zxf tomee7.tar.gz   && mv apache-tomee7-plus-7.0.4/* /usr/local/tomee7   && rm -Rf apache-tomee7-plus-7.0.4 && rm bin/*.bat     && rm tomee7.tar.gz*
 ---> Using cache
 ---> 26089304122d
Step 8/16 : COPY *.war /usr/local/tomee7/webapps/
 ---> Using cache
 ---> d69955ca9ee3
Step 9/16 : COPY context.xml /usr/local/tomee7/conf
 ---> Using cache
 ---> fb8e27bf03b0
Step 10/16 : COPY server.xml /usr/local/tomee7/conf
 ---> Using cache
 ---> 2f613f7784a4
Step 11/16 : COPY /usr/local/tomee7/conf
 ---> Using cache
 ---> 5643d6775598
Step 12/16 : RUN mkdir -p /usr/local/$host_name
 ---> Using cache
 ---> acd8a6c8c765
Step 13/16 : COPY ojdbc6- /usr/local/tomee7/lib/
 ---> Using cache
 ---> c9ec6ee71283
Step 14/16 : COPY /usr/local/tomee7/bin
 ---> Using cache
 ---> cab1d0202a73
Step 15/16 : EXPOSE 8080
 ---> Using cache
 ---> 83a3d3559705
Step 16/16 : CMD run
 ---> Using cache
 ---> eab0027728a7
Successfully built eab0027728a7
Successfully tagged orderapp:latest

Starting Your Container

My image is now ready! You can see your images by using the following command:

docker images

The application connects to an Oracle database and the database is connected to a Docker bridge network named appnet and hence I have to connect the order app container I created in the previous step to the same bridge network. 

To create a bridge network, you have to use the following command

docker network create appnet

Let’s now start the container using the docker run command. Ensure that you have the –p parameter to map your container port to the port of your host where you are running container.

docker run -p 8080:8080 --net appnet  orderapp

Your container should start as below and you will see the standard output in the console.

11-Jan-2018 07:31:09.529 INFO [main] sun.reflect.DelegatingMethodAccessorImpl.invoke Starting ProtocolHandler ["http-nio-8080"]
11-Jan-2018 07:31:09.547 INFO [main] sun.reflect.DelegatingMethodAccessorImpl.invoke Starting ProtocolHandler ["ajp-nio-8009"]
11-Jan-2018 07:31:09.558 INFO [main] sun.reflect.DelegatingMethodAccessorImpl.invoke Server startup in 12272 ms

If you don’t want to see the standard output then ensure that you change the command parameter in the Docker file to instead of as follows:

CMD ["", "run"]

Your Container in Action

Now you should be able to access your application as below:

Apache Tom EE Home page: http://localhost:8080 and you will get to the home page of Apache Tom EE.

Now try to access your application Home page: http://localhost:8080/yourAppURI

Hope this blog helps to get your application Dockerized!

No comments: