Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Official Tomcat Image Modify Server.xml and add jar to lib folder

I want to added MySQL jar file in lib folder and need to add some JNDI setting in server.xml file of tomcat conf folder of official tomcat docker image. But i am not sure how to make change to conf folder files of tomcat. I am using below Dockerfile for building tomcat image

FROM tomcat
ADD ./test.war /usr/local/tomcat/webapps/

RUN sh -c 'touch /usr/local/tomcat/webapps/test.war'
VOLUME /usr/local/tomcat/webapps/
EXPOSE 8080

Docker Compose File is

version: '2'
services:
  app-tomcat:
    container_name: app-server-tomcat
    image: symserver:latest
    external_links:
            - app-mysql:app-mysql
    ports:
    - 8080:8080
    environment:
    - "JAVA_OPTS=-Ddatabase.url=192.168.99.100"

I have tried COPY ./server.xml /usr/local/tomcat/conf/ in above file but i am getting below error.

docker-compose -f app.yml up

ERROR: for xboard-tomcat  No such image: sha256:34aefb95b68da96e5a5a6cb8b12bb85d926d9d1e5bea2c6c13d1ff9c00d4426d
←[31mERROR←[0m: Encountered errors while bringing up the project.
like image 804
Anchit Pancholi Avatar asked Oct 02 '16 12:10

Anchit Pancholi


2 Answers

The OP is old, but I came across it on google searching for a way to update the server.xml from the official docker image for tomcat. I would like to add my solution.

Use Case: My company has static html files that are generated from another application (running under Websphere Liberty) and saved to a shared directory. The shared directory is also mounted in our Tomcat container, but is not mounted in any of the web application directories, so tomcat does not have access to it by default. The official Tomcat docker image contains a default server.xml file, so I need to update this file to specify additional directories where Tomcat can search for content.

Initial Thoughts: Initially, I thought it would be best to write a bash script that simply applied my changes to the server.xml without overwriting the entire file. After contemplating this, I came to the conclusion if the default server.xml file changed (due to an image update) then unexpected behavior could occur which would require updates to the bash script, therefore, I dropped this idea.

Solution: Make a copy of the image's server.xml file, make my changes and add a COPY directive in the Dockerfile to overwrite the default server.xml with my changes. If the image is updated and the default server.xml file changes, then I will need to update my copy as well, but I would much rather do that than introduce more code to our repo.

Dockerfile:

FROM tomcat:7-jre8

Build and run official Tomcat image:

docker build -t tomcat:official .

Copy the server.xml to the local filesystem:

docker run tomcat:official tar -c -C /usr/local/tomcat/conf server.xml | tar x

Make changes to the local copy of the server.xml file and make sure that it's in the same directory as the Dockerfile.

In my case, I had to add the following line between the "Host" tags

<Context docBase="/mnt/html/Auto/Reports" path="/Auto/Reports" />

Update the Dockerfile:

FROM tomcat:7-jre8
COPY server.xml /usr/local/tomcat/conf/

Notes: My company is currently using the 'tomcat:7-jre8' version of the Tomcat image, but a list of all versions can be found here.

Hopfully this helps someone else.

like image 91
mattmc Avatar answered Nov 15 '22 10:11

mattmc


Can't this also be done with something like this, directly mapping file via 'docker volume'? I was trying to get it working here https://github.com/djangofan/docker-cluster . (but as of March-2018, I don't quite have it working)

  image: tomcat
  ports:
    - "10001:10080"
  expose:
    - "10001"
  volumes:
   - ./cluster/server.xml:/usr/local/tomcat/conf/server.xml
   - ./cluster/ROOT:/usr/local/tomcat/webapps/ROOT
   - ./cluster/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml
like image 34
djangofan Avatar answered Nov 15 '22 11:11

djangofan