Docker add warfile to official Tomcat image
Asked Answered
P

5

29

I pulled official Docker image for Tomcat by running this command.

docker run -it --rm tomcat:8.0

By using this as base image I need to build new image that contains my war file in the tomcat webapps folder. I created Dockerfile like this.

From tomcat8
ADD warfile /usr/local/tomcat

When I run this Dockerfile by building image I am not able to see Tomcat front page.

Can anybody tell me how to add my warfile to official Tomcat images webapp folder.

Pericarditis answered 7/1, 2015 at 11:48 Comment(0)
F
72

Reading from the documentation of the repo you would do something like that

FROM tomcat
MAINTAINER xyz

ADD your.war /usr/local/tomcat/webapps/

CMD ["catalina.sh", "run"]

Then build your image with docker build -t yourName <path-to-dockerfile>

And run it with:

docker run --rm -it -p 8080:8080 yourName
  • --rm removes the container as soon as you stop it
  • -p forwards the port to your host (or if you use boot2docker to this IP)
  • -it allows interactive mode, so you see if something get's deployed
Frederick answered 7/1, 2015 at 13:16 Comment(3)
I have one doubt.After running the image and when i check in tomcat webapps folder in ubuntu i dont see my war flile.But in browser when i enter into manager app i see my app.How it works?.If something goes wrong within the code i willl check for tomcat logs.But tomcat logs also empty herePericarditis
how to you check the webapps folder? the webapps folder is within the docker container. If you want to access your webapps container you could mount a host directory within your container to use it as webapps folder. That way you can access files without accessing docker. Details see hereFrederick
i can see the front folder , how to access that war file build, i tried with localhost:8080/buildName but it dont show up, tomcat is working on localhostVespiary
T
24

Building on @daniel's answer, if you want to deploy your WAR to the root of tomcat, I did this:

FROM tomcat:7-jre7
MAINTAINER xyz

RUN ["rm", "-fr", "/usr/local/tomcat/webapps/ROOT"]
COPY ./target/your-webapp-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war

CMD ["catalina.sh", "run"]

It deletes the existing root webapp, copies your WAR to the ROOT.war filename then executes tomcat.

Toga answered 27/3, 2015 at 0:42 Comment(1)
RUN rm -rf $CATALINA_HOME/webapps/ROOT COPY ./target/your-webapp-1.0-SNAPSHOT.war $CATALINA_HOME/webapps/ROOT.warAutopilot
M
3

docker run -it --rm --name MYTOMCAT -p 8080:8080 -v .../wars:/usr/local/tomcat/webapps/ tomcat:8.0

where wars folder contains war to deploy

Molehill answered 19/7, 2016 at 15:7 Comment(0)
F
2

How do you check the webapps folder?

The webapps folder is within the docker container. If you want to access your webapps container you could mount a host directory within your container to use it as webapps folder. That way you can access files without accessing docker. Details see here

To access your logs you could do that when you run your container e.g.

docker run -rm -it -p 8080:8080 **IMAGE_NAME** /path/to/tomcat/bin/catalina.sh  && tail -f /path/to/tomcat/logs

or you start your docker container and then do something like:

docker exec -it **CONTAINER_ID** tail -f /path/to/tomcat/logs
Frederick answered 23/1, 2015 at 7:58 Comment(0)
P
1

If you are using spring mvc project then you require server to run your application suppose you use tomcat then you need base image of tomcat that your application uses which you can specify through FROM command.

You can set environment variable using ENV command.

You can additionally use RUN command which executes during Docker Image buiding. eg to give read write execute permissions to webapps folder for tomcat to unzip war file

    RUN chmod -R 777 $CATALINA_HOME/webapps 

And one more command is CMD. Whatever you specifying in CMD command it will execute at a time of container running. You can specify options in CMD command using double quotes(" ") seperated by comma(,). eg

    CMD ["catalina.sh","start"]

(NOTE : Remember RUN command execute at a time of image building and CMD execute at a time of running container this is confusing for new users).

This is my Dockerfile -

    FROM tomcat:9.0.27-jdk8-openjdk
    VOLUME /tmp
    RUN chmod -R 777 $CATALINA_HOME/webapps 
    ENV CATALINA_HOME /usr/local/tomcat
    COPY target/*.war $CATALINA_HOME/webapps/myapp.war
    EXPOSE 8080
    CMD ["catalina.sh","run"]

Build your image using command

    docker build -t imageName <path_of_Dockerfile>

check your docker image using command

    docker images

Run image using command

    docker run -p 9999:8080 imageName

here 8080 is tomcat port and application can access on 9999 port

Try accessing your application on

    localhost:9999/myapp/
Playful answered 31/1, 2020 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.