Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Dockerize a tomcat app

I am trying to dockerize some Tomcat application but I never touch Java application before so the lack of understand it makes it really hard to understand what should I do.

So far I have this but it doesn't work and I don't if it's the correct path as well

FROM tomcat:6

ENV APP_ROOT /app_name

RUN apt-get update && apt-get install -y default-jdk

COPY . $APP_ROOT/
WORKDIR $APP_ROOT

RUN jar -cvf app_name.war *

# this fail for some reason, when I do `ls` the file is there but if fail to copy it
COPY app_name.war $CATALINA_BASE/webapps/app_name.war

I am just going on loop on this because I don't understand and Google Search do not help me that much (I don't know how to ask).

Should I use the jar command in the build? If not, I guess I have to build it locally and just make sure that the .war is there right?!

How the building of the Java with Tomcat app works? and How to integrate with Docker?

Sorry for being too generic but I don't understand anything about Java

like image 956
Yordis Prieto Lazo Avatar asked Oct 27 '25 08:10

Yordis Prieto Lazo


2 Answers

Looking at your code this is what I could gleam:

  • You have some java files stored in current directory (.)
  • When you call COPY you copy all these contents to /app_name
  • You create a .war on the file

There are some things to note, first is that the app_name.war is not on the host disk, it is currently inside of the docker file system. What this means is that you cannot COPY the .war.

What you are really after is this: RUN cp app_name.war $CATALINA_BASE/webapps/app_name.war

This would look like the following: Dockerfile

FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk
COPY . $APP_ROOT/
WORKDIR $APP_ROOT
RUN jar -cvf app_name.war *
RUN cp app_name.war $CATALINA_BASE/webapps/app_name.war

Adding the docker COPY reference here as it explains the command in detail. It might also be helpful for you to make a script called provision.sh, then do something like:

COPY provision.sh /tmp/provision.sh
RUN sh /tmp/provision.sh

That way you can put all your building, configuring and other in a single script that you can test locally (again if it helps)

EDIT: Adding mention about building locally and copying into dockerfile

You can build the .war on your machine, use COPY to put is on the machine. Dockerfile

FROM tomcat:6
ENV APP_ROOT /app_name
RUN apt-get update && apt-get install -y default-jdk

COPY app_name.war $CATALINA_BASE/webapps/app_name.war
WORKDIR $APP_ROOT

The above copies the file app_name.war then add it to the filesystem of the container at the path $CATALINA_BASE/webapps/app_name.war. So for that you do this:

  1. Build the .war on your machine with java
  2. Put .war in directory with Dockerfile
  3. COPY app_name.war into the container's filesystem
like image 59
jrbeverly Avatar answered Oct 28 '25 21:10

jrbeverly


You can try to do this "by hand" before trying to automate it, it should help to understand the process. You don't need to extend a tomcat official image to be able to deploy a war on a dockerized tomcat, you can use the image directly if you don't need to customize permissions and users (in production, you need).

If you need Tomcat 6.x because your webapp implements servlet API < 3, do this :

sudo docker run --name tomcat --detach --port 8080:8080 tomcat:6

Now, your Tomcat is running in background (--detach), waiting for a deployment. You've exported port 8080 from the container and mapped it to port 8080 from you host, so the app will be available at http://localhost:8080/ on your host.

From now if you copy your .war in /usr/local/tomcat/webapps into the container, the app will be deployed :

sudo docker cp app_name.war tomcat:/usr/local/tomcat/webapps/
like image 34
Tristan Avatar answered Oct 28 '25 22:10

Tristan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!