Deploy WAR in Docker tomcat container

Pramesh Bhattarai
2 min readJan 27, 2019

--

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

This blog is about running tomcat instance in a docker container and deploying .war in the container.

First, download the Docker in your local machine from the Docker page. We will focus on deploying your .war in docker container only… we can find the docker installation process on the Docker page.

So if you are here… it means you have successfully installed Docker in your local machine. So let’s get started… Open your terminal and start typing commands….

Link for some helpful docker commands https://github.com/prameshbhattarai/docker-commands

Let’s clone a tomcat image in your local machine from Docker Hub.

docker image pull tomcat:8.0
docker image ls # it will list all images in your docker

Now let’s create and start a tomcat container from the image

docker container create --publish 8082:8080 --name my-tomcat-container tomcat:8.0
docker container ls -a # it will list all the containers
docker container start my-tomcat-container

if you look into tomcat dockerfile, you will find tomcat expose port 8080, so while creating container we map tomcat container port to local machine port by using --publish <local_machine_port>:<tomcat_expose_port>

Tomcat application can be accessed in http://localhost:8082

# to get inside your docker tomcat container directory...
docker container exec -it my-tomcat-container bash
# it will list tomcat directory inside your docker as
# :/usr/local/tomcat# ls
# LICENSE NOTICE RELEASE-NOTES RUNNING.txt bin conf include lib # logs native-jni-lib temp webapps work

Now let’s create a docker image file for your .war and deploy it in docker…

Create a file with name Dockerfile in root directory of your application without any extension and add following lines in it.

# we are extending everything from tomcat:8.0 image ...
FROM tomcat:8.0
MAINTAINER your_name# COPY path-to-your-application-war path-to-webapps-in-docker-tomcatCOPY some-app/target/some-app.war /usr/local/tomcat/webapps/

Now build docker image file for your application from Dockerfile
open a terminal in root of your application directory

# docker image build -t your_name/some-app location_of_dockerfile
docker image build -t your_name/some-app-image ./
# now if you check list of images in your docker, you will see as
# your_name/some-app-image image in it...
docker image ls# REPOSITORY TAG IMAGE ID CREATED SIZE
# your_name/some-app-image latest 5154ca16b1dd 1 hours ago 421MB

You can create and start your new container for your application as describe above or you can simply run by using following command.

# creating and running a container
docker container run -it --publish 8081:8080 your_name/some-app-image

Your application can be accessed in http://localhost:8081

--

--