Deploy war file to docker image

Deploy war file to docker image

This tutorial demonstrate how to deploy a war file into docker image.

I’ll be using 2 approach:

  1. Embedded war file to build into docker image.
  2. Externalize war file by mounting with docker tomcat path.

Structure

Screen Shot 2015-10-07 at 12.56.08 AM

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

  1. Prepare a Dockerfile with the following content.
  2. Copy the war file from out from the target folder.
# Pull base image
From tomcat:8-jre8

# Maintainer
MAINTAINER "xxx <[email protected]">

# 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 .

docker-build-1

Run docker container

Run docker container with interactive mode.

docker run -it --rm -p 8080:8080 --name dockerwar webserver

docker-build-2

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 <[email protected]">

 

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

17 comments

  1. # 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.

    Reply
    1. Yes, this is the location for me to put my war file, but it could be different from your environment.
      ADD [source] [destination]

      Reply
  2. How to replace war path /usr/local/tomcat/webapps/ with git repo??

    Reply
    1. Sorry, i don’t quite understand your question.

      Reply
  3. how can i change some files in my app, specialy in WEB-INF i need to add a txt file?

    Reply
    1. You can access to your docker image with the interactive mode and add the text files you want.

      Reply
  4. 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.

    Reply
    1. When running docker, you shouldn’t used localhost. You need to check what is your containers ip address.

      Reply
  5. 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.

    Reply
  6. I am actually using docker for windows does this has to do something with that ???

    Reply
  7. how can i write a docker file for generating logs whenever my services restarts?

    Reply
  8. 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

    Reply
    1. You should change to your correspondence folders instead.

      Reply
  9. It’s very helpful. Appreciated.

    Thank you..

    Reply
  10. Thank You so much.

    Very Helpful

    Reply
  11. Wow..Thank you very much!

    Reply
  12. Thanks a ton man!!! Was struggling with docker since last week.

    Reply

Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.