简体   繁体   English

暴露Docker Port的问题

[英]Issue with exposing Docker Port

I am trying to package a war file using a tomcat Docker image. 我正在尝试使用tomcat Docker映像打包war文件。 I'm trying to expose the tomcat port in the image for it to be accessed externally. 我正在尝试在图像中公开tomcat端口,以供外部访问。

This is my docker file 这是我的码头工人文件

 FROM tomcat8:3.0.0

 COPY target/hello-world-war-1.0.0.war /usr/local/tomcat/webapps

 EXPOSE 8080

However, when I build this image, run it and try to access from my host browser (localhost:8080) it doesn't hit the endpoint, therefore it does not work as expected 但是,当我构建此映像时,运行它并尝试从主机浏览器(localhost:8080)访问该映像时,它没有命中端点,因此无法按预期工作

I still have to expose the port externally with the -p switch. 我仍然必须使用-p开关在外部公开端口。

What have I missed? 我错过了什么? or is the usage of EXPOSE something else? 还是使用EXPOSE还有其他用途?

My end need is to be able to deploy this in kubernetes. 我的最终需求是能够在kubernetes中部署它。

What have I missed? 我错过了什么?

You need to publish port in some way: 您需要以某种方式发布端口:

  • either by directly publishing it in docker run command remapping the EXPOSE Dockerfile directive to different host port using (note lowercase p): 通过直接将其发布在docker run命令重映射EXPOSE使用(注意小写p)的Dockerfile指令以不同的主机端口:

     -p=[] : Publish a container᾿s port or a range of ports to the host 
  • or, as stated in docker documentation , implicitly publish ports listed in the EXPOSE Dockerfile directive using (note capital P): 或者,如EXPOSE 文档中所述,使用(注意大写字母P)隐式发布EXPOSE Dockerfile指令中列出的端口:

     -P : Publish all exposed ports to the host interfaces 

My end need is to be able to deploy this in kubernetes. 我的最终需求是能够在kubernetes中部署它。

And to that goal you will be using ports listed in k8s manifests in similar fashion. 为此,您将以类似的方式使用k8s清单中列出的端口。 Say, you expose port 8080 in your docker image (and publish it with -p or -P), you will then be using it in container spec (exact manifest depending on chosen k8s object type) and very probably later in service manifest, and in each layer you can change port mapping (similar to docker run -p mapping host:docker port to different values if you require to do so). 假设您在docker映像中公开了端口8080(并使用-p或-P进行发布),然后将其用于容器规范(具体清单取决于所选择的k8s对象类型),并且很可能稍后在服务清单中使用,以及在每一层中,您都可以更改端口映射(类似于docker run -p将host:docker端口映射为不同的值)(如果需要)。 So as far as k8s is concerned if you EXPOSE port in Dockerfile and it works correctly locally in container and also on host through any means of publishing it ( docker run with -p or -P) it will function in kubernetes as well (as far as you list it in proper manifest files). 因此,就k8s而言,如果您在Dockerfile中使用EXPOSE端口,并且它可以在容器中本地正常运行,并且还可以通过任何发布它的方式在主机上docker run使用-p或-P docker run docker),它也将在kubernetes中起作用(到目前为止当您在正确的清单文件中列出它时)。

The EXPOSE statement in your war simply means that the war is listening to port 8080 in the image, allowing access to your application through that port. 战争中的EXPOSE语句仅表示战争正在侦听映像中的端口8080,从而允许通过该端口访问您的应用程序。

This however does not mean that it opens these ports. 但是,这并不意味着它会打开这些端口。 Thus you still need to use the -p switch to open the internal and/or external ports of docker to allow traffic between containers and/or external connections. 因此,您仍然需要使用-p开关打开docker的内部和/或外部端口,以允许容器和/或外部连接之间的通信。

The first port specified by -p is the host port and the second one the container port. -p指定的第一个端口是主机端口,第二个是容器端口。 When you only specify one port this means the container can only be accessed by other containers running in the same docker network. 当您仅指定一个端口时,这意味着该容器只能由在同一docker网络中运行的其他容器访问。

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

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