4

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
  • 57
  • 2
  • 9
  • 1
    Probably from your base image, tomcat:9.0.34. The exposed ports don't mean the container is actually listening to those ports. You can be listening unexposed ports, or you can be not listening exposed ones. EXPOSE is mostly informational. – Burak Serdar Apr 11 '20 at 18:42
  • Do they _have_ to be the same pod? If you were trying to run these two processes on the same host outside of Docker, how would you do it? – David Maze Apr 11 '20 at 18:55
  • If you're willing to build a custom tomcat image, why not downloading and running the exporter as webapp inside tomcat? It will remove the need of a second container and port collision and hopefully yield the wanted result – Totem Apr 11 '20 at 22:06
  • @BurakSerdar - Yes EXPOSE is just information , but my question as mentioned above is that even after changing the server.xml file of tomcat , container still shows 8080 as active (in the actual case it should only show 9090). The problem is , when creating a pod with above 2 containers , pod is unable to start because of port bind exceptions. – santhu Apr 12 '20 at 11:22
  • @DavidMaze problem here is that the tomcat exporter that I have found on the internet is a war file - hence by default running on the port 8080 - and as I see it , we cant change port for the exporter. Running both of them in different servers may not equip the exporter to gather the metrics . All we can do is to look at how can we make the app use a different port than the exporter. – santhu Apr 12 '20 at 11:25

2 Answers2

6

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

Rasika Weragoda
  • 936
  • 14
  • 15
1

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
  • 498
  • 6
  • 19
  • But for some reason, even after changing the port in original Docker file tomcat is still getting bind to the same old port 8080. Here is what docker logs show ``````` 12-Apr-2020 14:42:22.850 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 12-Apr-2020 14:42:22.893 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [284] milliseconds ``````` – santhu Apr 12 '20 at 14:51
  • My understanding is that the initial question focused why the 8080 port has exposed by Docker, not why Tomcat does not respect the port you configured. Is your question about Tomcat not using port 9090 even after you `sed` `8080` with `9090` in `server.xml`? – George Tseres Apr 12 '20 at 15:39
  • yes exactly that is what my problem is. Why tomcat doesnt respect even after I changed all occurrences of 8080 to 9090 – santhu Apr 14 '20 at 12:51
  • Would it be possible to attach the tomcat `server.xml` after your `sed` command? – George Tseres Apr 14 '20 at 17:35