简体   繁体   中英

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

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/

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

I don't use docker, I use a similar AWS product called codedeploy for provisioning instances, so I tell you what I do for Tomcat setup in my provisioning scripts. Should be easy to port to docker as just bash comands.

1) Build the WAR Most java applications these days are built using Maven but Gradle is catching up. Maven and the WAR plugin are used to turn java code into a WAR file which you can deploy on Tomcat. But it looks like you already have the WAR built by someone else? Either way, you dont run the war directly, you put it in Tomcat, unless you've bundled Tomcat into the app, in which case it would be a JAR but lets not talk about that....The simple solution is build the war from java code using a build tool like Maven or Gradle. By build, I mean turn it from source code to binary.

2) Install Tomcat

yum install tomcat6,7,8 etc etc (Whichever version you need)

Then turn it on

service start tomcat8

3) Deploy the war To run the war place it in the webapps folder of the Tomcat installation. I generally like to shut tomcat off when I do this but you can do it while its running. After a few seconds the WAR, which is really just a zip file, will be exploded/unzipped to create a directory.

4) Accessing the application/site If you rename your war to ROOT.war then you can access the applicaition at http://localhost:8080 if your configuration is to have it listen on 8080. If you war is named pets.war then your webapp URL would be http://localhost:8080/pets . You configure which port for Tomcat to listen on in the server.xml file in its conf folder.

Most Important Tomcat documentation is very good once you know what to look for. The primary configuration files are web.xml, context.xml, and server.xml. The central tomcat guides explain each component https://tomcat.apache.org/tomcat-7.0-doc/setup.html you just need to find the doc that corresponds to your version of Tomcat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM