Deploy Java WAR file in Docker Tomcat Container Step by Step Guide

Last modified date

We will be following a step by step guide on how to deploy a WAR file in docker tomcat

Step by Step

  1. Pull Tomcat image with specific version from Docker hub
  2. Create Tomcat Container using docker image
  3. Deploy sample.war file in docker tomcat /usr/local/tomcat/webapps/ folder
  4. Check if application is working

 

1. Pull Tomcat image with specific version from Docker hub

You must create a docker hub account in order to download the latest version or custom version of tomcat from docker hub, in my case I wanted to download tomcat with jdk8 version.

Use this command to pull tomcat with jdk8 version from docker quickstart terminal

docker pull tomcat:jdk8

If you have very good internet speed the file will download in less than a minute.

Use this command to check you docker images

docker images

After executing the command if you see tomcat with tag jdk8 that means the image was pulled successfully from docker hub to you docker machine.

2. Create Tomcat Container using docker Image

Now using the tomcat:jdk image we need to create a container.

 

Using the following command we can start the container.

docker run -p 9292:8080  882487b8be1d

The above command will create a container for the specified image-id and start the tomcat server. Go to browser and check if the tomcat is up with the port 9292.

http://<your-default-ip>:9292

3. Deploy sample.war file in docker tomcat /usr/local/tomcat/webapps/ folder

Once we are done with the above two steps, we must copy war file in the docker container  tomcat path. If you have your WAR file then directly copy the file in docker tomcat, if you don’t have one then you can download sample war file from tomcat website for testing purpose.

Download sample.war file from tomcat website:

https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/

After I downloaded the sample.war file, I have copied the file in “ D:/ ” drive in my computer.

Now open new Docker Quickstart Terminal window.

 

Use the command to check all your container

docker container ls

using the following command copy the sample.war file in docker container tomcat folder.

docker cp 'D:\sample.war'  10b6562ff760:'/usr/local/tomcat/webapps/'

Use the following command to check the sample.war file is copied in tomcat webapps folder successfully

docker container exec -it 10b6562ff760 bash
cd webapps
ls

If you can see the sample.war file in the webapps in tomcat webapps folder, this means you have successfully copied the sample.war file.

4. Check if application is working

Once you are done with the above 3 steps successfully, just go to your browser open the url to check the application is working.

In my case my default machine IP is 192.168.99.100

So my URL is : http://192.168.99.100:9292/sample/

If you are able to see this, that means you have successfully deployed war file in docker tomcat container.

Watch video:

intactabode