0

I am running tomcat in a docker container like this

FROM myregistry/cfw-deploy-base

COPY ./my.war /opt/tomcat/webapps/

ENV JAVA_OPTS=""

CMD ["/opt/tomcat/bin/catalina.sh", "run"]

However, the context path is set based on the war file name (my in this case) I want to set the context path from an environment variable configured inside the container. Can I set it when using catalina.sh?

kosta
  • 4,302
  • 10
  • 50
  • 104

1 Answers1

0

The easiest way would be copy your *.war file to webapps folder with desired name. It can be easily done, by modifying line:

COPY ./my.war /opt/tomcat/webapps/

to

COPY ./my.war /opt/tomcat/webapps/myContext.war

This change would make your app available under <server>:<port>/myContext path. If you wish to have context path specified under environment variable, you can modify your script to:

ENV MY_CONTEXT=contextName
COPY ./my.war /opt/tomcat/webapps/${MY_CONTEXT}.war

Setting up application context explicitly with Tomcat means can be done with these steps:

  1. Go to your application META-INF folder (in your case: /opt/tomcat/webapps/my/META-INF)

  2. Create XML file: context.xml and paste this content:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myContext"/>
  1. Restart Tomcat

With some effort you should be able to automate these steps in your Dockerfile.

Tinki
  • 1,486
  • 1
  • 21
  • 32
  • I'm trying the `context.xml` approach (using tomcat:9-jre8-alpine) but TomCat is still setting the context path based on the .war file name. Any ideas as to why? – Maurice Jul 17 '18 at 16:57