Docker tomcat edit expanded war files

2019-06-26 03:44发布

I am using docker to deploy a tomcat container running a third party war file.

My Dockerfile looks something like this

FROM tomcat:7-jre8

ADD my.war ${CATALINA_HOME}/webapps/my.war 

When I run the container tomcat expands my war at runtime and I can happily access my app at http://my.ip.addr:8080/mywar/.

However my problem is that I want to edit a couple of the config files within the war. I don't really want to unpack and repack the war file as that seems messy and hard to maintain.

I was hoping to be able to tell tomcat to expand the war as part of my RUN steps and then use ADD to put in my custom files but I can't seem to find a way of doing this. The war only gets expanded when the CMD executes and then I can't edit the files after that.

4条回答
Luminary・发光体
2楼-- · 2019-06-26 04:08

You can use Run command to start the tomcat container and then immediately stop it. This will cause the war to be expanded and then you can go ahead and modify the config files.

Ex:

RUN Start_tomcat.sh && \
    stop_tomcat.sh && \
    edit_1_config_file.sh && \
    edit_2_config_file.sh

Note: The you need to do all the steps in one run command. As Docker build will take each command and run it on the image generated from the previous command and commit. And incase of services, depending on a service that has been started in the previous RUN command will not work.

查看更多
淡お忘
3楼-- · 2019-06-26 04:23

I am not sure exactly how you would achieve it with docker or anything else, as i dont see anyway to ask tomcat to just expand the war before it actually starts. But as per standard practices its not a good idea to explode a war and tweak the contents. It kills the entire purpose of making a war.

Rather you should make changes to the app to read configuration from << TOMCAT_HOME >>/conf.

If you achieve this the only thing you will need to tell Docker is to ADD your configuration file to the containers tomcat conf folder.

Or if it is a must for you to tamper with war file this is what you can do: Explode the war manually (or by script) on your build machine and instead of adding war directly to the docker image, map the folder. Something like this

ADD ./target/app-0.1.0.BUILD-SNAPSHOT /var/lib/jetty/webapps/ROOT.

And then manually add all your files to desired destinations.

ADD login.jsp /var/lib/jetty/webapps/ROOT/Webapps, so on and so forth.

查看更多
爷的心禁止访问
4楼-- · 2019-06-26 04:29

Just add a RUN instruction which launches the server in the background, gives it a few seconds then exits i.e.

RUN bash -c "catalina.sh start; sleep 5; catalina.sh stop;"

You will need to give it long enough to expand the war file.

查看更多
5楼-- · 2019-06-26 04:31

How about unzipping your war manually, before starting tomcat?

ADD my.war /tmp/my.war 
RUN unzip /tmp/my.war -d /usr/local/tomcat/webapps/mywar

# Edit config files

# Now you can start tomcat...
CMD ["catalina.sh", "run"]
查看更多
登录 后发表回答