使用REST API将端口绑定到主机接口 [英] Binding a port to a host interface using the REST API
问题描述
命令行界面的文档说明如下:
将容器的端口绑定到主机的特定接口
系统,使用docker运行命令的-p参数:
常规语法
docker run -p [([< host_interface>:[host_port]])|(< host_port>):]< container_port> [/ udp]< image>
/ p>
当没有提供主机接口时,端口绑定到
主机的所有可用接口(也称为INADDR_ANY,或
0.0.0.0)当没有提供主机端口时,动态分配一个主机端口。 TCP端口选项的可能组合如下:
所以我想知道我如何做同样的REST API? / p>
使用 POST / container / create
我试过:
-
PortSpecs:[5432:5432]
这似乎暴露了端口但不绑定到主机接口。 >
-
PortSpecs:[5432]
给出与上一个相同的结果。 -
PortSpecs:[0.0.0.0:5432:5432]
这会返回错误无效hostPort:0.0.0.0
这是有道理的。
当我做 sudo docker ps
容器显示 5432 / tcp
应该是 0.0.0.0:5432/tcp
检查容器给我以下内容:
NetworkSettings :{
IPAddress:172.17.0.25,
IPPrefixLen:16,
网关:172.17.42.1,
桥 docker0,
PortMapping:null,
端口:{
5432 / tcp:null
}
}
完全检查可以找到 here 。
这是一个无证的功能。我在邮件列表:
创建容器时,您必须设置 ExposedPorts
:
ExposedPorts:{22 / tcp:{}}
启动容器时,需要设置 PortBindings
:
code>PortBindings:{22 / tcp:[{HostPort:11022}]}
github 有关于此的问题。
The documentation for the commandline interface says the following:
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
So I was wondering how I do the same but with the REST API?
With POST /container/create
I tried:
"PortSpecs": ["5432:5432"]
this seems to expose the port but not bind it to the host interface."PortSpecs": ["5432"]
gives me the same result as the previous one."PortSpecs": ["0.0.0.0:5432:5432"]
this returns the errorInvalid hostPort: 0.0.0.0
which makes sense.
When I do sudo docker ps
the container shows 5432/tcp
which should be 0.0.0.0:5432/tcp
.
Inspecting the container gives me the following:
"NetworkSettings": {
"IPAddress": "172.17.0.25",
"IPPrefixLen": 16,
"Gateway": "172.17.42.1",
"Bridge": "docker0",
"PortMapping": null,
"Ports": {
"5432/tcp": null
}
}
Full inspect can be found here.
This is an undocumented feature. I found my answer on the mailing list:
When creating the container you have to set ExposedPorts
:
"ExposedPorts": { "22/tcp": {} }
When starting your container you need to set PortBindings
:
"PortBindings": { "22/tcp": [{ "HostPort": "11022" }] }
There already is an issue on github about this.
这篇关于使用REST API将端口绑定到主机接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!