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
# 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.
Yes, this is the location for me to put my war file, but it could be different from your environment.
ADD [source] [destination]
How to replace war path /usr/local/tomcat/webapps/ with git repo??
Sorry, i don’t quite understand your question.
how can i change some files in my app, specialy in WEB-INF i need to add a txt file?
You can access to your docker image with the interactive mode and add the text files you want.
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.
When running docker, you shouldn’t used localhost. You need to check what is your containers ip address.
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.
I am actually using docker for windows does this has to do something with that ???
how can i write a docker file for generating logs whenever my services restarts?
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
You should change to your correspondence folders instead.
It’s very helpful. Appreciated.
Thank you..
Thank You so much.
Very Helpful
Wow..Thank you very much!
Thanks a ton man!!! Was struggling with docker since last week.
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.
Just build 2 docker images and run it.
I want to deploy the war file built by jenkins to tomcat docker container, so what will be the source path in docker file?
You can double check the jenkins server, the war file should be somewhere and you can use this as the source path.