Deploying a sample Java application to Tomcat with Docker

Now let's review the steps involved in deploying a simple Java application that runs on Tomcat with the help of Docker. For this example, consider an Ubuntu OS and review the commands required for the setup.

The first step is to install Docker on your system that has an Ubuntu OS. Do this with the following commands:

//re synchronize the package index files from their sources
sudo apt-get update
//install the package specified
sudo apt-get install linux-image-extra-'uname -r'
//pass advanced options to gpg. With adv --recv-key you can download the public key.
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <hashkey>
//execute the following command as interpreted by this program.
sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
//re synchronize the package index files from their sources
sudo apt-get update
//install the package specified
sudo apt-get install lxc-docker
//docker run keeping the stdin open even if not attached and allocate a pseudo-TTY
sudo docker run -i -t ubuntu /bin/bash

The preceding steps should install and start the Docker container on your Ubuntu operating system with script.

After the installation, you could add the user to the Docker group with the following commands:

sudo groupadd docker
sudo gpasswd -a your_username docker
sudo service docker restart

The next step is to set up Java and Tomcat on your Docker container with the following content on the docker file:

FROM ubuntu:saucy
# Update the Ubuntu with run command
RUN apt-get update && apt-get -y upgrade
# Adding java repository to the container
RUN apt-get -y install software-properties-common
RUN add-apt-repository ppa:webupd9team/java
RUN apt-get -y update
# Accept the Java license for the install
RUN echo "oracle-java9-installer shared/accepted-oracle-license-v1-1 boolean true" | debconf-set-selections
# Install the Java software to the container
RUN apt-get -y install oracle-java9-installer
# Install the tomcat software to the container
RUN apt-get -y install tomcat9
RUN echo "JAVA_HOME=/usr/lib/jvm/java-9-oracle" >> /etc/default/tomcat9EXPOSE 8080
# Download tomcat homepage
RUN mkdir /var/lib/tomcat9/webapps/ tomcat
RUN wget http://tomcat.apache.org -P /var/lib/tomcat9/webapps/tomcat
# Start Tomcat, after starting Tomcat the container will stop. By using the below technique, it can be keep running.
CMD service tomcat9 start && tail -f /var/lib/tomcat9/logs/catalina.out

Now you can build the container with the docker build –t tomcat9 . command. Note . at the end of the command.

The container is now ready to start using the docker run -p 8080:8080 -d tomcat9 command. This command should ensure port 8080 of the container gets forwarded to the system's local port 8080. After a few seconds, the tomcat page should be available at http://localhost:8080/tomcat/.

If you want to see the list of Docker containers, run the following command:

$ docker ps -a -s

Similarly, you can see the image tree of Docker using the following command:

$ docker images -tree

The final step is to deploy your sample Java application WAR file through Docker, which can be done using the wget command. For the following example, we are using the sample.war project available on Tomcat 9's sample projects:

RUN wget http://tomcat.apache.org/tomcat-9.0-doc/appdev/sample/sample.war -P /var/lib/tomcat9/webapps

Through these steps, you have built a Docker container application using the Docker file, and it should deploy the application that would be accessible through http://localhost:8080/sample/.

Similarly, Docker can be integrated into various infrastructure tools, including AWS, Google Cloud Platform, IBM Bluemix, Jenkins, Microsoft Azure, OpenSVC, Oracle Container Cloud Service, and VMware vSphere Integrated Containers.

The big leap of these integrated systems is the ability to build microservices, a variant of the service-oriented architecture (SOA) style that structures an application as a collection of loosely coupled services. Refer to http://microservices.io/ for more details about microservices; it has a number of patterns that will provide you information on how to build microservices.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset