简体   繁体   English

Docker 端口映射是主机到容器还是容器到主机?

[英]Are Docker port mappings host-to-container or container-to-host?

I use Docker port-mappings often to map a host port to a container port, ie traffic sent to the host port is forwarded to the container port.我经常使用 Docker 端口映射将主机端口映射到容器端口,即将发送到主机端口的流量转发到容器端口。

This can be seen as a host-to-contaner mapping of ports.这可以看作是端口的主机到容器映射。

What confuses me is that many articles talk about mapping container ports to host ports, ie the reverse mapping.令我困惑的是,很多文章都在谈论将容器端口映射到主机端口,即反向映射。

Furthermore, docker ps lists host-to-container port mappings, while docker port lists container-tohost port mappings:此外, docker ps列出主机与集装箱的端口映射,而docker port列表容器tohost端口映射:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
5526355f9c75        ubuntu:latest       "/bin/bash"              55 seconds ago      Up 54 seconds       127.0.0.1:4444->8080/tcp   angry_tharp

$ docker port angry_tharp
8080/tcp -> 127.0.0.1:4444

Why all this confusion with the direction of the port mapping?为什么所有这些都与端口映射的方向混淆? Are host-to-container port mappings any different form container-to-host port mappings, or are they the same?主机到容器端口映射与容器到主机端口映射有什么不同,还是相同?

host-to-container and container-to-host port mappings are exactly the same.主机到容器和容器到主机的端口映射完全相同。 You can see this is true just by looking at your two examples.仅通过查看您的两个示例,您就可以看出这是真的。 The ports are reversed, so the mappings are the exact same.端口是相反的,因此映射完全相同。

Docker port shows the port mappings in relation to the container, whilst docker ls / ps shows it in relation to the host. Docker port 显示与容器相关的端口映射,而 docker ls / ps 显示与主机相关的端口映射。 Why?为什么? Not sure, but that doesn't change the mappings.不确定,但这不会改变映射。

From their official docs, which I personally think has precedence over any unofficial article:来自他们的官方文档,我个人认为它优先于任何非官方文章:

Flag value  Description
-p 8080:80  Map TCP port 80 in the container to port 8080 on the Docker host.
-p 192.168.1.100:8080:80    Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100.
-p 8080:80/udp  Map UDP port 80 in the container to port 8080 on the Docker host.
-p 8080:80/tcp -p 8080:80/udp   Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

They always mention the mappings as host-to-container.他们总是将映射称为主机到容器。

A docker port mapping exposes a port from within the container to the host. docker 端口映射将容器内的端口暴露给主机。 So what you call a "host-to-container" could as well be worded "container-to-host": it depends of the point of view.因此,您所说的“主机到容器”也可以称为“容器到主机”:这取决于观点。

Let's pretend we are a network packet.让我们假设我们是一个网络数据包。 When arriving on the host, we are forwarded to the container.当到达主机时,我们被转发到容器。 That is "host-to-container".那就是“主机到容器”。

But now let's pretend we are the running service inside the container.但是现在让我们假设我们是容器内正在运行的服务。 When we start, be bind to the container port, which is mapped to the host's.当我们开始时,绑定到映射到主机的容器端口。 So in a way that is "container-to-host".所以在某种程度上是“容器到主机”。

You get the idea: that's the same thing.你明白了:那是同样的事情。

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

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