docker remote api无法将端口绑定到主机

我试图使用docker的远程API绑定主机,但它总是失败。

我使用python和urllib2来传递这个dict

 { 'Tty':True, 'Volumes':[], 'Image':'ubuntu', 'Cmd':'', 'WorkingDir':None, 'Entrypoint':None, 'Env':None, 'AttachStdin':False, 'AttachStdout':True, 'AttachStderr':True, 'OpenStdin':True, 'ExposedPorts':{'80/tcp':{}}, 'HostConfig':{'PortBindings':{'80/tcp':['15080']}} } 

然后我使用远程API来检查容器,它给了我以下内容:

 "NetworkSettings":{ "Bridge":"docker0", "Gateway":"172.17.42.1", "IPAddress":"172.17.0.48", "IPPrefixLen":16, "PortMapping":null, "Ports":{"22/tcp":null,"80/tcp":null} } 

我也尝试使用这个链接中的方法使用REST API绑定端口到主机接口 ,但是当我发布数据来启动容器时,它将引发HTTP错误500.所以我猜Docker Remote api是否已经被改变了。

看起来你的PortBindings参数稍微偏离。 你错过了"HostPort":

将其更改为:

 'HostConfig':{'PortBindings':{'80/tcp':[{ "HostPort": '15080' }] }} 

从文档 :

  • PortBindings – 它们应该映射到的公开容器端口和主机端口的映射。 它应该以{<端口> / <协议>forms指定:[{“HostPort”:“<port>”}]}请注意,端口被指定为一个string,而不是一个整数值。