docker 网络 - 主机模式与桥接模式 - 端口映射

docker networking - host mode vs bridge mode - port mapping

在生产中,建议使用 host 模式网络而不是 bridge 模式以避免数据包转发开销。

$ docker container run --help | grep "network"
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the container

Cloudformation 资源类型 AWS::ECS::TaskDefinition 选项(NetworkMode)内部使用决定启动 docker 容器的网络模式,它说:

If the network mode is host, you cannot run multiple instantiations of the same task on a single container instance when port mappings are used.


使用 AWS::ECS::TaskDefinition

正在启动 master jenkins(docker 容器)的单个实例,端口映射为 8080:8080。 EC2 端口 8080 与容器端口 8080 的映射。


说的是什么意思? 当使用端口映射时,不能运行在单个容器实例上对同一任务进行多次实例化...

当您使用 network mode 作为主机时,容器共享主机的网络命名空间,如 here 所述,因此端口映射不会生效。这意味着您不能将容器的 8080 端口映射到主机的 80 端口,因为当容器公开 8080 端口时,它将直接在主机的 8080 端口可用。

cannot run multiple instantiations of the same task on a single container instance when port mappings are used....

容器实例基本上是一种特殊类型的 EC2 实例,如 here 所述。这意味着如果您在同一台主机(容器实例)中 运行 超过 1 个相同任务的实例化,它们将公开相同的端口(例如 8080),该端口又直接映射到主机上的相同端口机器,因此导致冲突。