Welcome!

Blog Feed Post

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 applications improves developer productivity it’s the portable characteristics and by 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
StoreWeb.war
Credit Card
CreditService.war
Order
OrderService.war  
Shipping
DeliveryService.war
Catalog
Catalog.war


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/pdb1.domain.com "
                  username="orderapp"
                  password="mypassword"
/>


Also Tomcat needs Oracle JDBC drive to connect to Oracle database and hence you need to deploy the JDBC driver (ojdbc6-11.2.0.3.jar) 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

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

RUN set -xe \
     for key in $VALID_GPG_KEYS; do \
        gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
    done

RUN set -x \
     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-11.2.0.3.jar /usr/local/tomee7/lib/
EXPOSE 8080
CMD ["catalina.sh", "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-11.2.0.3.jar /usr/local/tomee7/lib/

EXPOSE 8080
CMD ["startup.sh", "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 ha.pool.sks-keyservers.net --recv-keys "$key";     done
 ---> Using cache
 ---> ea35e9b7b56c
Step 7/16 : RUN set -x && curl -fSL https://repo.maven.apache.org/maven2/org/apache/tomee7/apache-tomee7/7.0... -o tomee7.tar.gz.asc    && curl -fSL https://repo.maven.apache.org/maven2/org/apache/tomee7/apache-tomee7/7.0... -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 logging.properties /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-11.2.0.3.jar /usr/local/tomee7/lib/
 ---> Using cache
 ---> c9ec6ee71283
Step 14/16 : COPY setenv.sh /usr/local/tomee7/bin
 ---> Using cache
 ---> cab1d0202a73
Step 15/16 : EXPOSE 8080
 ---> Using cache
 ---> 83a3d3559705
Step 16/16 : CMD catalina.sh 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 startup.sh instead of catalina.sh as follows:

CMD ["startup.sh", "run"]

Your Container in Action


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


Apache Tom EE Home page: http://localhost:8080

Application Home page: http://localhost:8080/yourAppURI


Hope this blog helps to get your application Dockerized!




Read the original blog entry...

More Stories By Debu Panda

Debu Panda is a Director of Product Management at Oracle Corporation. He is lead author of the EJB 3 in Action (Manning Publications) and Middleware Management (Packt). He has more than 20 years of experience in the IT industry and has published numerous articles on enterprise Java technologies and has presented at many conferences. Debu maintains an active blog on enterprise Java at http://debupanda.blogspot.com.

Latest Stories
DX World EXPO, LLC, a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Conference Guru has been named “Media Sponsor” of the 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. A valuable conference experience generates new contacts, sales leads, potential strategic partners and potential investors; helps gather competitive intelligence and even provides inspiration for new products and services. Conference Guru works with conference organizers to pass great deals to gre...
DevOps is under attack because developers don’t want to mess with infrastructure. They will happily own their code into production, but want to use platforms instead of raw automation. That’s changing the landscape that we understand as DevOps with both architecture concepts (CloudNative) and process redefinition (SRE). Rob Hirschfeld’s recent work in Kubernetes operations has led to the conclusion that containers and related platforms have changed the way we should be thinking about DevOps and...
The Internet of Things will challenge the status quo of how IT and development organizations operate. Or will it? Certainly the fog layer of IoT requires special insights about data ontology, security and transactional integrity. But the developmental challenges are the same: People, Process and Platform. In his session at @ThingsExpo, Craig Sproule, CEO of Metavine, demonstrated how to move beyond today's coding paradigm and shared the must-have mindsets for removing complexity from the develop...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
The next XaaS is CICDaaS. Why? Because CICD saves developers a huge amount of time. CD is an especially great option for projects that require multiple and frequent contributions to be integrated. But… securing CICD best practices is an emerging, essential, yet little understood practice for DevOps teams and their Cloud Service Providers. The only way to get CICD to work in a highly secure environment takes collaboration, patience and persistence. Building CICD in the cloud requires rigorous ar...
Companies are harnessing data in ways we once associated with science fiction. Analysts have access to a plethora of visualization and reporting tools, but considering the vast amount of data businesses collect and limitations of CPUs, end users are forced to design their structures and systems with limitations. Until now. As the cloud toolkit to analyze data has evolved, GPUs have stepped in to massively parallel SQL, visualization and machine learning.
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
"ZeroStack is a startup in Silicon Valley. We're solving a very interesting problem around bringing public cloud convenience with private cloud control for enterprises and mid-size companies," explained Kamesh Pemmaraju, VP of Product Management at ZeroStack, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Enterprises are adopting Kubernetes to accelerate the development and the delivery of cloud-native applications. However, sharing a Kubernetes cluster between members of the same team can be challenging. And, sharing clusters across multiple teams is even harder. Kubernetes offers several constructs to help implement segmentation and isolation. However, these primitives can be complex to understand and apply. As a result, it’s becoming common for enterprises to end up with several clusters. Thi...