Search code examples
javatomcatdockerdockerfile

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


Solution

  • 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