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
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
No comments:
Post a Comment