Deploy war file to docker image
This tutorial demonstrate how to deploy a war file into docker image.
I’ll be using 2 approach:
- Embedded war file to build into docker image.
- Externalize war file by mounting with docker tomcat path.
Structure
Approach 1
Let’s get started.
Step 1) Prepare a Dockerfile
Step 2) Run build custom image base on docker hub tomcat image
Step 3) Start docker containers
Dockerfile
- Prepare a Dockerfile with the following content.
- Copy the war file from out from the target folder.
# Pull base image From tomcat:8-jre8 # Maintainer MAINTAINER "xxx <xxx@gmail.com"> # Copy to images tomcat path ADD dockerwar.war /usr/local/tomcat/webapps/
Build docker image
Now run the follow command to docker image name webserver with your Dockerfile in current directory. First time to build docker image will require download and may take longer times.
docker build -t webserver .
Run docker container
Run docker container with interactive mode.
docker run -it --rm -p 8080:8080 --name dockerwar webserver
Test your container
Open a browser with URL http://192.168.59.103:8080/dockerwar/
Approach 2
Approach 2 are slightly different, let’s modified the Dockerfile, we just extends the tomcat based image.
Dockerfile
# Pull base image From tomcat:8-jre8 # Maintainer MAINTAINER "xxx <xxx@gmail.com">
Build docker file
docker build -t webserver .
Eclipse classpath
In this example, I’m using eclipse classpath “target” folder to mount with docker container’s /webapps/ directory.
Delete all files in “target folder”
Before you start the container run “mvn clean package” to your project.
Run docker
Run the following command in interactive mode, mount your eclipse build path to your docker container tomcat webapps folder.
docker run -it --rm -p 8080:8080 -v /Users/mingch/workspace/dockerwar/target:/usr/local/tomcat/webapps/ --name dockerwar webserver
Test your container
Open a browser with URL http://192.168.59.103:8080/dockerwar/
Tails container logs
You can either start your container without interactive mode -it or run another terminal to interact with your container. Eg, my container instance is e6b2.
To list all the log files:
docker exec -it e6b2 ls /usr/local/tomcat/logs/
To tail the log file:
docker exec -it e6b2 tail -f /usr/local/tomcat/logs/localhost_access_log.2015-10-07.txt
October 18, 2016
# Pull base image
From tomcat:8-jre8
# Maintainer
MAINTAINER “xxx
# Copy to images tomcat path
ADD dockerwar.war /usr/local/tomcat/webapps/
In this, ” /usr/local/tomcat/webapps/” what path it is?
is it that directory where i put my war file?
please suggest , i am new in this Techno.
October 21, 2016
Yes, this is the location for me to put my war file, but it could be different from your environment.
ADD [source] [destination]
July 2, 2017
How to replace war path /usr/local/tomcat/webapps/ with git repo??
July 3, 2017
Sorry, i don’t quite understand your question.
September 5, 2017
how can i change some files in my app, specialy in WEB-INF i need to add a txt file?
September 6, 2017
You can access to your docker image with the interactive mode and add the text files you want.
October 2, 2017
Hi
Once I run this I cannot access it as http://localhost/dockerwar getting 404 error but still, i can access the http://localhost/ and I get the Tomcat starting page. i checked the war file in the /usr/local/tomcat/webapps/ and its there . Please help me.
October 2, 2017
When running docker, you shouldn’t used localhost. You need to check what is your containers ip address.
October 3, 2017
Thanks a lot for the reply …but the issue is i checked my container ip with the docker command: docker container inspect –format ‘{{ .NetworkSettings.IPAddress }}’ web
out put is 172.17.0.2 and try to access the tomcat page as follows.
http://172.17.0.2:8080 got the error as follows ERROR The requested URL could not be retrieved. really appreciate your help on this.
October 5, 2017
I am actually using docker for windows does this has to do something with that ???
April 3, 2018
how can i write a docker file for generating logs whenever my services restarts?
May 12, 2018
I try the below command and when I’m mounting the folder to webapps I’m unable to access the tomcat page. It’s working fine once I remove the mount option I’m not sure what’s wrong in it.
docker run -it –rm -p 8080:8080 -v /Users/mingch/workspace/dockerwar/target:/usr/local/tomcat/webapps/ –name dockerwar webserver
May 12, 2018
You should change to your correspondence folders instead.
July 1, 2018
It’s very helpful. Appreciated.
Thank you..
July 19, 2018
Thank You so much.
Very Helpful
November 7, 2018
Wow..Thank you very much!
January 23, 2019
Thanks a ton man!!! Was struggling with docker since last week.
August 16, 2019
Hello sir,
First I would like to say big thanks to you for this wonderful tutorial. I m going through it a successfully deploy the war file.
Now I have one query I want to host two different war files on two different containers i.e
container 1 container 2
war1 war2
tomcat tomcat
Like above how we achieved this using docker on centos. Kindly provide way or solution for this.
Awaiting your reply.
August 19, 2019
Just build 2 docker images and run it.
February 17, 2020
I want to deploy the war file built by jenkins to tomcat docker container, so what will be the source path in docker file?
February 17, 2020
You can double check the jenkins server, the war file should be somewhere and you can use this as the source path.