简体   繁体   English

Docker 运行 -p?/? (这两个端口号是什么,它们代表什么)

[英]Docker run -p ?/? (what are this two port numbers and what they represents )

I used command "docker run -p 8080/8080 --name my_local_image...." and it failed saying Unable to find image '8080/8080:latest' locally我使用了命令“docker run -p 8080/8080 --name my_local_image....”,但无法在本地找到图像“8080/8080:latest”

8080/8080 = what this two port represents exactly? 8080/8080 = 这两个端口究竟代表什么?

Replace this " -p 8080/8080 " by this " -p 8080:8080 "用这个“ -p 8080:8080 ”替换这个“ -p 8080/8080

  • The first port: the Docker host ( you can use this port to access to your container) to access to the container from the outside .第一个端口:Docker 主机(您可以使用此端口访问您的容器)从外部访问容器。
  • the second one: is the port used by your application.第二个:是您的应用程序使用的端口。

Example: I want to run tomcat server in a docker container, the default port of tomcat is 8080 and I want to expose my docker on port 9000 so i have to write: Example: I want to run tomcat server in a docker container, the default port of tomcat is 8080 and I want to expose my docker on port 9000 so i have to write:

docker run -p 9000:8080 --name myTomcatContainer tomcat 

So with this configuration I can access to Tomcat from outside using: http://host-ip:9000因此,通过此配置,我可以使用以下命令从外部访问 Tomcat:http://host-ip:9000

The -p option is used to expose the ports that are used for the image instance. -p选项用于公开用于映像实例的端口。 The first one is the hostport and the second parameter after the ":" fullcolon is the container port to which it should be mapped.第一个是主机端口,“:”后的第二个fullcolon是它应该映射到的容器端口。 For more details read doc .有关更多详细信息,请阅读doc

The error that you received while launching the instance using docker run tells us that the image name that you provided with the command is not locally available on the machine that you are using, it might also be an issue with the way that you provided the ports using '/' instead of ":".So, to be safe, use docker pull first to pull the latest image from repo and then run it with proper syntax.您在使用 docker run 启动实例时收到的错误告诉我们,您随命令提供的映像名称在您使用的机器上本地不可用,这也可能是您提供端口的方式的问题使用 '/' 而不是 ":"。所以,为了安全起见,首先使用docker pull从 repo 中提取最新图像,然后使用正确的语法运行它。

--expose=[]: Expose a port or a range of ports inside the container. --expose=[]:暴露容器内的一个端口或一系列端口。 These are additional to those exposed by the EXPOSE instruction这些是EXPOSE指令公开的附加内容

-P: Publish all exposed ports to the host interfaces -P:将所有暴露的端口发布到主机接口

-p=[]: Publish a container's port or a range of ports to the host format: ip:hostPort:containerPort | -p=[]:发布一个容器的端口或一系列端口到主机格式:ip:hostPort:containerPort | ip::containerPort | ip::containerPort | hostPort:containerPort |主机端口:容器端口 | containerPort Both hostPort and containerPort can be specified as a range of ports. containerPort hostPort 和 containerPort 都可以指定为一系列端口。 When specifying ranges for both, the number of container ports in the range must match the number of host ports in the range, for example: -p 1234-1236:1234-1236/tcp When specifying a range for hostPort only, the containerPort must not be a range.为两者指定范围时,范围内的容器端口数必须与范围内的主机端口数匹配,例如:-p 1234-1236:1234-1236/tcp 仅为 hostPort 指定范围时,containerPort 必须不是一个范围。 In this case the container port is published somewhere within the specified hostPort range.在这种情况下,容器端口在指定的 hostPort 范围内发布。 (eg, -p 1234-1236:1234/tcp ) (use 'docker port' to see the actual mapping) (例如, -p 1234-1236:1234/tcp )(使用 'docker port' 查看实际映射)

--link="": Add link to another container (:alias or ) --link="": 添加到另一个容器的链接 (:alias or )

Just to add a little to the already good answers here, I like to use the analogy of finding a friends apartment in a city to explain port numbers.只是为了在这里已经很好的答案中添加一点,我喜欢使用在城市中寻找朋友公寓的类比来解释端口号。

In this analogy, the在这个类比中,

  • Host OS == neighborhood where the apartment complex is主机操作系统 == 公寓大楼所在的社区

  • Your application == the apartment complex您的申请 == 公寓大楼

  • Exposed port within the application == the specific apartment in the complex应用程序的暴露端口 == 综合体中的特定公寓

If you're trying to find someone's address, you'll find the neighborhood of the user (in this case it would be the URL, most likely http://localhost if developing locally)如果您要查找某人的地址,您会找到用户的邻居(在这种情况下,它将是 URL,如果在本地开发,很可能是http://localhost

But then you need the street number of the apartment complex, that number is the first number in something like 5000 :8080, so your application is exposed on the host OS at port 5000, the friends street address is 5000, localHost street.但是您需要公寓大楼的街道号码,该号码是5000 :8080 之类的第一个数字,因此您的应用程序在端口 5000 上暴露在主机操作系统上,朋友的街道地址是 5000,localHost 街道。

But once you get to the apartment complex, you still need to know which door to knock on.但是一旦你到达公寓大楼,你仍然需要知道该敲哪扇门。 You've arrived at the apartment complex (your application), but where does your friend live specifically?您已经到达公寓大楼(您的申请),但您的朋友具体住在哪里? That's the second number, the 5000: 8080 .那是第二个数字, 5000: 8080

So your friend's network address is the combination of the URL, port within the host OS (if you're developing locally, this is your computer, if not, it's whatever server the app is running on) and port exposed within the application.因此,您朋友的网络地址是 URL、主机操作系统中的端口(如果您在本地开发,这是您的计算机,如果不是,它是应用程序运行的任何服务器)和应用程序中公开的端口的组合。

Note that an apartment complex usually has more than one apartment, but it's still at the same address, so it's very possible that your application exposes more than one port, something like 5000:8081 , or 5000:9000 .请注意,公寓大楼通常有多个公寓,但仍位于同一地址,因此您的应用程序很可能会暴露多个端口,例如5000:80815000:9000

It's also possible that two apartments in different buildings will have the same apartment number, so we can have 5000:8080 , as well as 8080:8080 , neither this nor the previous situation will cause a conflict in network traffic.也有可能不同建筑的两套公寓有相同的公寓号,所以我们可以有5000:80808080:8080 ,这两种情况都不会导致网络流量冲突。

This image is an example of the last case I mentioned这张图片是我提到的最后一个案例的一个例子

主机上的端口

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

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