mlee_jordan
mlee_jordan

Reputation: 842

Changing Tomcat config - context.xml through dockerfile

I have developed a web app (maven, tomcat) in Intellij and managed to create a container through 'Services' tab in Intellij which was straightforward thanks to easy deployment config. During the process, I encountered that the cache size was not enough so I manually changed the context.xml (added <Resources cacheMaxSize="51200" />) file of tomcat manually locally after which the app ran smoothly.

To summarize the container creation in Intellij under services tab (see the bottom for the image):

 - pulling an image: tomcat:9.0.65-jre8
 - container name
 - bind ports: 127.0.0.1:8080:8080
 - bind mounts: mount host path which contains the war WITH /usr/local/tomcat/webapps

Though not sure, I guess the war file I created took already into account the change I made in the context.xml file since my application server is the tomcat I downloaded and made the change on its context.xml.

However, I also need to create a container with a dockerfile:

My dockerfile is:

FROM maven:3.8.4-jdk-8 as maven_builder
COPY . /usr/src/maven_pdfparse
WORKDIR /usr/src/maven_pdfparse
RUN mvn clean install -f /usr/src/maven_pdfparse && mkdir /usr/src/wars/
RUN find /usr/src/maven_pdfparse/ -iname '*.war' -exec cp {} /usr/src/wars/ \;
ADD pom.xml .

FROM tomcat:9.0.65-jre8
COPY --from=maven_builder /usr/src/wars/* /usr/local/tomcat/webapps

When I ran this on docker, I again got the 'insufficient cache' issue.

So how can I make the same change on context.xml when creating a dockerfile?

Or is there a way to get the dockerfile automatically when I create the container through deployment configuration?

enter image description here

Upvotes: 0

Views: 3484

Answers (3)

Sven
Sven

Reputation: 21

Rather than modifying the Tomcat context at build time an interesting option could be to expose some parameters for modification at runtime.

 <Resources cacheMaxSize="${CACHE_SIZE:-51200}" />

Exposing these parameter for the runtime would allow you not to have to rebuild and deploy an image every time you need to fine tune a setting.

Those settings can be then defined via the environment variables passed to the container image at startup.

# docker run --env CACHE_SIZE=8192 localhost/my-tomcat

Variable which will be accessible to the application running within the container, in your case Tomcat

Recent versions (since latest v7.0) of Tomcat can handle that quite. See the properties replacement feature, described in the Tomcat documentation here

Two ways to enable this feature:

  • either by passing -Dorg.apache.tomcat.util.digester. PROPERTY_SOURCE=org.apache.tomcat.util.digester.EnvironmentPropertySource system variable to the JVM.
  • or by adding the property to the catalina.properties file:
    org.apache.tomcat.util.digester. PROPERTY_SOURCE=org.apache.tomcat.util.digester.EnvironmentPropertySource
    

Upvotes: 2

JohnXF
JohnXF

Reputation: 1097

You have a few options to choose from if you wish to add to or modify the context.xml file.

  1. You can modify the context.xml file already within the image as part of the image build. Add a RUN command using a command line tool like sed to add the required <Resources> element to the file.
  2. You could also have a pre-modified version of the file and just copy it into the image to overwrite the existing one.
  3. You could add a custom startup command that modifies the context.xml (e.g. using sed as per option 1) before invoking the usual tomcat startup script. Using this mechanism you could also get the cacheMaxSize value to use from an environment variable and thus allow run-time control of the value.

Upvotes: 4

Reda E.
Reda E.

Reputation: 868

Please check this answer

Tomcat 8 throwing - org.apache.catalina.webresources.Cache.getResource Unable to add the resource

Where you'll need to update context.xml and rebuild your image with copying context.xml

Upvotes: 0

Related Questions