gpt4 book ai didi

docker - 使用 REST API 将端口绑定(bind)到主机接口(interface)

转载 作者:IT老高 更新时间:2023-10-28 12:42:14 27 4
gpt4 key购买 nike

命令行界面的文档说明如下:

To bind a port of the container to a specific interface of the host system, use the -p parameter of the docker run command:

General syntax

docker run -p [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp] <image>

When no host interface is provided, the port is bound to all available interfaces of the host machine (aka INADDR_ANY, or 0.0.0.0).When no host port is provided, one is dynamically allocated. The possible combinations of options for TCP port are the following

所以我想知道如何使用 REST API 来做同样的事情?

POST /container/create我试过了:

  • "PortSpecs": ["5432:5432"]这似乎暴露了端口,但没有将其绑定(bind)到主机接口(interface)。
  • "PortSpecs": ["5432"]给我与前一个相同的结果。
  • "PortSpecs": ["0.0.0.0:5432:5432"]这将返回错误 Invalid hostPort: 0.0.0.0这是有道理的。

当我这样做时 sudo docker ps容器显示 5432/tcp应该是 0.0.0.0:5432/tcp .

检查容器给了我以下信息:

"NetworkSettings": {
"IPAddress": "172.17.0.25",
"IPPrefixLen": 16,
"Gateway": "172.17.42.1",
"Bridge": "docker0",
"PortMapping": null,
"Ports": {
"5432/tcp": null
}
}

完整的检查可以找到here .

最佳答案

这是一个未记录的功能。我在 mailing list 上找到了答案:

创建容器时你必须设置 ExposedPorts:

"ExposedPorts": { "22/tcp": {} }

启动容器时,您需要设置 PortBindings:

"PortBindings": { "22/tcp": [{ "HostPort": "11022" }] }

github 上已经存在问题关于这个。

关于docker - 使用 REST API 将端口绑定(bind)到主机接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20428302/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com