Permanently change the tomcat port from Dockerfile

4/11/2020

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.

-- santhu
docker
dockerfile
kubernetes
tomcat
tomcat9

1 Answer

4/12/2020

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.

-- George Tseres
Source: StackOverflow