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

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 <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
Deploy war file to docker image

21 thoughts on “Deploy war file to docker image

  • October 18, 2016 at 4:27 pm
    Permalink

    # 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
    • October 21, 2016 at 8:13 am
      Permalink

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

      Reply
  • July 2, 2017 at 12:21 pm
    Permalink

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

    Reply
    • July 3, 2017 at 4:25 am
      Permalink

      Sorry, i don’t quite understand your question.

      Reply
  • September 5, 2017 at 1:46 pm
    Permalink

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

    Reply
    • September 6, 2017 at 2:29 am
      Permalink

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

      Reply
  • October 2, 2017 at 12:48 pm
    Permalink

    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
    • October 2, 2017 at 4:49 pm
      Permalink

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

      Reply
  • October 3, 2017 at 6:07 am
    Permalink

    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
  • October 5, 2017 at 11:33 am
    Permalink

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

    Reply
  • April 3, 2018 at 7:07 am
    Permalink

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

    Reply
  • May 12, 2018 at 6:15 am
    Permalink

    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
    • May 12, 2018 at 12:24 pm
      Permalink

      You should change to your correspondence folders instead.

      Reply
  • July 19, 2018 at 2:57 pm
    Permalink

    Thank You so much.

    Very Helpful

    Reply
  • November 7, 2018 at 7:18 am
    Permalink

    Wow..Thank you very much!

    Reply
  • January 23, 2019 at 4:54 pm
    Permalink

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

    Reply
  • August 16, 2019 at 12:59 pm
    Permalink

    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.

    Reply
    • August 19, 2019 at 7:42 am
      Permalink

      Just build 2 docker images and run it.

      Reply
  • February 17, 2020 at 7:28 am
    Permalink

    I want to deploy the war file built by jenkins to tomcat docker container, so what will be the source path in docker file?

    Reply
    • February 17, 2020 at 12:07 pm
      Permalink

      You can double check the jenkins server, the war file should be somewhere and you can use this as the source path.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

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