Deploying your first web application to Tomcat on Docker

Print Friendly, PDF & Email

Before reading this post, you are expected to be familiar with deploying a web-based application to Tomcat or any similar web container. To add another way of deploying your web applications, I am going to show you how to deploy a war file to Tomcat running on Docker.

Deploy web apps on Tomcat running in a Docker container
Deploy web apps on Tomcat running in a Docker container

Introduction

Instead of running your web server, i.e. Apache Tomcat, in your laptop, we can try to run it on a docker container. Why do we need to do so? As the first aim of Docker design, we want to shift our current development to teammates as soon as possible. Therefore, having your web application deployed on a docker container is probably easy to share the result to your colleagues to make sure they can see what you have mentioned. Let’s give an example.

Your work is in progress. You want to show the current state of your work to your inline manager so that he can understand your progress. If he is a technical lead, the easy way of running your application on his laptop is to download or git your project and run the application on a docker container.

[box type=”warning” align=”aligncenter” width=”5″ ]In this post, I don’t want to go deeply how to create a web application which is deployable on Tomcat. The demo project will give you a war file to practice deployment. If you want to learn more about how to create war files and deploy them on Tomcat, I recommend you to read a series of weblogs about building web-based applications with Grails, Spring Boot on this forum.[/box]

Pre-requisites

To complete the demo project addressed below, your system has to be installed the following tools:

  1. Docker, off course!!!
  2. Git, the version control system, to check out the demo project

Baby steps and demo project

Don’t want to waste your priceless time, let’s get started now.

Step 1: Write a Dockerfile file

This step will help us to perform the second step below.  Naturally, your web application has to be deployed on Tomcat so the docker image must be built from Tomcat image or a similar one. In this demo, we will see how a Grails-based application deployed on Docker Tomcat container. The war coming along with the demo is generated in Grails. The Dockerfile sample looks like the one below. 

#FROM tomcat:8.0-alpine
FROM tomcat:7-jdk8-openjdk
LABEL maintainer="admin@itersdesktop.com"

ADD grailsapp.war /usr/local/tomcat/webapps/

EXPOSE 8080

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

The key thing here is to determine in which Tomcat version will be pulled to make your apps working properly. You can use either the docker hub or docker command to search your images of interest.

Search tomcat images of interest on docker hub
Search tomcat images of interest on docker hub

The command to search images is 

docker search tomcat
# search docker images which name has tomcat

This command will result in the available images which you can refer to your Dockerfile. The result is shown on the screenshot below.

Results docker search tomcat command returns
Results docker search tomcat command returns

Step 2: Build a docker image for your deployment

To build a docker image, you have to run the command docker buildthat Docker engine provides to their users. The following command will build a docker image tagged tnguyenv/grails-based-app

docker build -t tnguyenv/grails-based-app .

Sending build context to Docker daemon  101.2MB
Step 1/5 : FROM tomcat:8.0-alpine
8.0-alpine: Pulling from library/tomcat
4fe2ade4980c: Pulling fs layer
6fc58a8d4ae4: Pulling fs layer
7d9bd64c803b: Pulling fs layer
a22aedc5ac11: Pulling fs layer
5bde63ae3587: Pulling fs layer
69cb0c9b940a: Pulling fs layer
5bde63ae3587: Waiting
69cb0c9b940a: Waiting
a22aedc5ac11: Waiting
6fc58a8d4ae4: Verifying Checksum
6fc58a8d4ae4: Download complete
a22aedc5ac11: Verifying Checksum
a22aedc5ac11: Download complete
4fe2ade4980c: Verifying Checksum
4fe2ade4980c: Download complete
4fe2ade4980c: Pull complete
6fc58a8d4ae4: Pull complete
69cb0c9b940a: Verifying Checksum
69cb0c9b940a: Download complete
5bde63ae3587: Verifying Checksum
5bde63ae3587: Download complete
7d9bd64c803b: Verifying Checksum
7d9bd64c803b: Download complete
7d9bd64c803b: Pull complete
a22aedc5ac11: Pull complete
5bde63ae3587: Pull complete
69cb0c9b940a: Pull complete
Digest: sha256:d02a16c0147fcae13d812fa670a4b3c9944f5328b10a5a463ad697d2aa5bb063
Status: Downloaded newer image for tomcat:8.0-alpine
 ---> 624fb61775c3
Step 2/5 : LABEL maintainer="admin@itersdesktop.com"
 ---> Running in 06c6f350d2c0
Removing intermediate container 06c6f350d2c0
 ---> b7bf5680010e
Step 3/5 : ADD grailsapp.war /usr/local/tomcat/webapps/
 ---> 930955a34be6
Step 4/5 : EXPOSE 8080
 ---> Running in fe8724d59ede
Removing intermediate container fe8724d59ede
 ---> 142d947a275d
Step 5/5 : CMD ["catalina.sh", "run"]
 ---> Running in 2c3e5b962fbf
Removing intermediate container 2c3e5b962fbf
 ---> 6ea6ea2f999f
Successfully built 6ea6ea2f999f
Successfully tagged tnguyenv/grails-based-app:latest

To check whether your built image is successful or not, run the following docker command

PS C:\Users\tnguyen> docker image ls -a
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
tnguyenv/grails-based-app      latest              6ea6ea2f999f        5 seconds ago       197MB
<none>                         <none>              142d947a275d        6 seconds ago       197MB
<none>                         <none>              930955a34be6        8 seconds ago       197MB
<none>                         <none>              b7bf5680010e        9 seconds ago       147MB

Step 3: Run and test

Step 3.1: Run the docker image

As usual, you can test your docker image by using the command docker run like the following one

docker run -it 
  --rm tnguyenv/grails-based-app \
  --name mywebapp --expose 1982:8080
# this command runs the image tagged tnguyenv/grails-based-app and sets the container name as mywebapp on the port 1982 mapping to the internal 8080

Run the docker image means you start your web application server. In this context, that is an Apache Tomcat server. Do you know how to test it?

Step 3.2: Test the application deployed in Tomcat

Because I exposed the port 1982, you have to access this link http://localhost:1982/grails-based-app to land on the home page of the application.

Another strict way to test your deployment is to log in into the docker container by running the following command

docker exec -it mywebapp /bin/bash
# in which mywebapp is the name of the docker container set in the running command above

That’s it! 

Source code

If you have any problem of building your files, feel free to go to my GitHub repository where I published all my work on this post. 

Summary

Eventually, you have just succeeded in setting up an Apache Tomcat Web Server running on a Docker container. This is a very simple web application that you don’t have to set up environment variables, copy source codes, grant permissions, add non-root users, etc. All these things are completely done via Dockerfile. I hope you can learn more about DevOps from my blog.

References

  1. Docker Hub: Tomcat Images
  2. Deploying Your First Web App to Tomcat on Docker

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.