26 Feb 2018

Running Tomcat and Oracle DB in a Docker container

In one of my previous posts I showed how to run an ADF essentials application on Tomcat in a docker container. I am using this approach primarily for sample applications as a convenient way to share a proof-of-concept. In this post I am going to describe how to enrich the docker container with Oracle DB so my samples can be DB aware.

The original Tomcat image that I am developing in these posts is based on Debian Linux. I really don't want to have fun with installing and configuring Oracle DB on Debian Linux, and, for sure, I am not going to describe that in this post. What I am going to do is to use Docker-in-Docker technique. So, I am going to take the container from the previous post with ADF-preconfigured Tomcat, install Docker runtime in that container, pull Oracle DB image and run it inside the container. There are plenty of discussions about the Docker-in-Docker technique arguing if it is effective enough or not. I think I wouldn't go with this approach in production, but for sample applications I am totally fine with it.

Let's start.

1. Run a new container from the image saved in the previous post:
docker run --privileged -it -p 8888:8080 -p 1521:1521 -p 5500:5500 --name adftomcatdb efedorenko/adftomcat bash

Mind the option privileged in the docker command. This option is needed to make the container able  to run Docker engine inside itself.

2.  Install Docker engine in the container:
curl -fsSL get.docker.com -o get-docker.sh

sh get-docker.sh
After successful installation Docker engine should start automatically. It can be checked by running a simple docker command:
docker ps
If the engine has not started (as it happened in my case), start it manually:
service docker start
3. Login to Docker Hub:
docker login
And provide your Docker Hub credentials.

4. Pull and run official Oracle DB Image:
docker run --detach=true --name ADFDB -p 1521:1521 -p 5500:5500  store/oracle/database-enterprise:

It's done!

Now we have a docker container with preconfigured Tomcat to run ADF applications and with Oracle DB running in a container inside the container. We can connect to the DB from both adftomcatdb container and the host machine as sys/Oradoc_db1@ as sysdba

Let's save our work to a docker image, so that we can reuse it later.

5. Create a start up shell script /user/local/tomcat/start.sh in the container with the following content:
service docker start
docker start ADFDB
catalina.sh start
exec "$@"
6. Remove Docker runtimes folder in the container:
rm -r /var/lib/docker/runtimes/
7. Stop the container from the host terminal:
 docker stop adftomcatdb
8. Create a new image:
docker commit adftomcatdb efedorenko/adftomcatdb:1.0
9. Run a new container out of the created image:
docker run --privileged -it -p 8888:8080 -p 1521:1521 -p 5500:5500 --name adftomcatdb_10 efedorenko/adftomcatdb:1.0 ./start.sh bash
10. Enjoy!

That's it!