简体   繁体   English

将 tomcat 端口从 Dockerfile 永久更改

[英]Permanently change the tomcat port from Dockerfile

I want to run two containers inside a k8s pod.我想在 k8s pod 中运行两个容器。

  1. tomcat exporter ( which runs on port 8080 ) tomcat 导出器(在端口 8080 上运行)
  2. tomcat application ( which also runs on the port 8080 ) tomcat 应用程序(它也在端口 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 ))由于 pod 内的多个运行容器不能共享同一个端口,我期待使用不同的端口构建自定义 tomcat 映像(例如 9090(默认 tomcat 端口为:8080))

This is what the Dockerfile I have used.这就是我用过的Dockerfile。

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.构建该映像并运行容器后,我看到 9090 端口已分配,但我也看到 8080 仍然存在。

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我想知道这个 8080/tcp 端口是从哪里来的,即使我已经在 server.xml 文件中将所有 8080 的引用更改为 9090

Any thoughts are appreciated.任何想法表示赞赏。

Checking the tomcat:9.0.34 Dockerfile in Dockerhub , we can see that it is exposing port 8080 .检查 Dockerhub 中的tomcat:9.0.34 Dockerfile ,我们可以看到它正在暴露端口8080 What happens when you use this image as your parent image, is that you inherit this EXPOSE instruction from that image.当您将此图像用作父图像时会发生什么,即您从该图像继承了此EXPOSE指令。

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.通过文档搜索,Dockerfile 中似乎没有“取消曝光”指令来撤消父图像的EXPOSE 8080指令。

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.这应该不会导致任何问题,但是如果您想消除它,您可以分叉 tomcat Dockerfile,删除EXPOSE指令并构建您自己的 tomcat 映像。

With lots of effort, I found the solution to change the internal port of tomcat container经过一番努力,我找到了更改 tomcat 容器内部端口的解决方案

my Dockerfile is我的 Dockerfile 是

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.这里ADD./tomcat-cas/war/ ${CATALINA_HOME}/webapps/部分不是必需的,除非您想最初部署一些war文件。 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.而且我也没有添加EXPOSE 4287 ,因为如果我这样做了,tomcat 服务器没有绑定到端口 4287 然后它总是绑定到 8080 默认端口。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM