简体   繁体   中英

Permanently change the tomcat port from Dockerfile

I want to run two containers inside a k8s pod.

  1. tomcat exporter ( which runs on port 8080 )
  2. tomcat application ( which also runs on the port 8080 )

As multiple running containers inside a pod cant share a same port, I am looking forward to build a custom tomcat image with a different port ( say 9090 ( default tomcat port is: 8080 ))

This is what the Dockerfile I have used.

cat Dockerfile 
FROM tomcat:9.0.34
RUN sed -i 's/8080/9090/' /usr/local/tomcat/conf/server.xml
EXPOSE 9090

After building that image and running a container, I see that 9090 port has been assigned, but I also see 8080 is also still existing.

CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS              PORTS                              NAMES
b66e1e9c3db8        chakilams3/tomcatchangedport:v1   "catalina.sh run"   3 seconds ago       Up 2 seconds        8080/tcp, 0.0.0.0:9090->9090/tcp   test

I am wondering from where does this 8080/tcp port comes from, even after I have changed all refferences of 8080 to 9090 in the server.xml file

Any thoughts are appreciated.

Checking the tomcat:9.0.34 Dockerfile in Dockerhub , we can see that it is exposing port 8080 . What happens when you use this image as your parent image, is that you inherit this EXPOSE instruction from that image.

Searching through the documentation , there does not seem to exist an "unexpose" instruction in the Dockerfile to undo the EXPOSE 8080 instruction of the parent image.

This should not cause any issue, but if you would like to eliminate it, you could fork the tomcat Dockerfile, remove the EXPOSE instruction and build your own tomcat image.

With lots of effort, I found the solution to change the internal port of tomcat container

my Dockerfile is

FROM tomcat:7.0.107
RUN sed -i 's/port="8080"/port="4287"/' ${CATALINA_HOME}/conf/server.xml
ADD ./tomcat-cas/war/ ${CATALINA_HOME}/webapps/
CMD ["catalina.sh", "run"]

Here ADD./tomcat-cas/war/ ${CATALINA_HOME}/webapps/ part is not necessary unless you want to initially deploy some war files. And also I don't add EXPOSE 4287 , because if I did so, the tomcat server not binding to the port 4287 then it always binding to the 8080 default port.

Just build the image and run
docker build -f Dockerfile -t test/tomcat-test:1.0.
docker run -d -p 4287:4287 --name tomcat-test test/tomcat-test:1.0

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