64

I am pretty new to Tomcat and Docker - so I am probably missing a Tomcat fundamental somewhere in this question.

What I am trying to do is build a Docker container that runs a SpringBoot Restful web service that just returns some static data. This is all running on OSX so I am using Boot2Docker as well.

I've written my own Dockerfile to build the container that my app runs in:

FROM tomcat:8.0.20-jre8

RUN mkdir /usr/local/tomcat/webapps/myapp

COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp/

This Dockerfile works fine and I am able to start the container from the created image.

docker build -t myapp .

docker run -it --rm -p 8888:8080 myapp

This container starts correctly and outputs no errors and displays the message saying my app was deployed.

22-Mar-2015 23:07:21.217 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 
Deploying web application directory /usr/local/tomcat/webapps/myapp

The container also correctly has the myapp.war copied to the path described in the Dockerfile. Moreover I am able to navigate to Tomcat default page to confirm that Tomcat is running, I can also hit all the examples, etc.

To the problem, when I navigate to http://192.168.59.103:8888/myapp/getData I get a 404. I can't quite figure out why. Am I missing something regarding a .war deploy to Tomcat?

Chris
  • 919
  • 2
  • 7
  • 11
  • Where are you getting that IP address from? – Garry Cairns Mar 22 '15 at 23:46
  • It's the one assigned to my Boot2Docker VM. Hitting that IP:port without the /myapp displays the default Tomcat page. – Chris Mar 22 '15 at 23:51
  • I'm a Linux guy so behavior may differ but try visiting localhost:8080 instead. – Garry Cairns Mar 23 '15 at 00:01
  • That doesn't work, as the container is running in a VM managed by Boot2Docker, it's a bit different than running Docker natively on Linux. – Chris Mar 23 '15 at 00:14
  • check the logs of you tomcat, I don't think tomcat was able to load your app as you're not putting the war inside the `webapps` but inside a subfolder. check this [article](http://elsoufy.blogspot.fr/2014/03/build-your-own-saas-with-docker.html) it may help. – bachr Mar 27 '15 at 10:12
  • If you already use spring-boot you can also use a plain java docker image and build & run the fat jar that already contains tomcat. ```java -jar your-server.jar``` – Jan Apr 17 '15 at 11:51
  • if its not necessary to create every time new container, you can just copy file to existing container. Example is the answear from Krishna Chaitanya `docker run -v ./1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war:/usr/local/tomcat/webapps/myapp.war -it -p 8080:8080 tomcat` – Mark Oct 28 '17 at 13:10

3 Answers3

71

You are trying to copy the war file to a directory below webapps. The war file should be copied into the webapps directory.

Remove the mkdir command, and copy the war file like this:

COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp.war

Tomcat will extract the war if autodeploy is turned on.

Tobias
  • 4,999
  • 7
  • 34
  • 40
crazyman
  • 979
  • 8
  • 8
23

There's a oneliner for this one.

You can simply run,

docker run -v /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war:/usr/local/tomcat/webapps/myapp.war -it -p 8080:8080 tomcat

This will copy the war file to webapps directory and get your app running in no time.

  • This will replace any existing webapps within the tomcat directory and just copy one war? If this is not desired you may have to use a staging area and copy wars from there to tomcat/webapps – Winter Soldier Jul 19 '18 at 19:58
10

Tomcat will only extract the war which is copied to webapps directory. Change Dockerfile as below:

FROM tomcat:8.0.20-jre8
COPY /1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/myapp.war

You might need to access the url as below unless you have specified the webroot

http://192.168.59.103:8888/myapp/getData

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Saril Sudhakaran
  • 1,109
  • 9
  • 17