如何在docker-py中运行基本的Web应用程序容器?
我试图模仿运行一个集装箱化的Web应用程序暴露在一些端口
sudo docker run -d -p 1338:1337 kermit/hellonode
在Python中使用docker-py 。 到目前为止,我得到了这个代码来启动实例:
container = c.create_container('kermit/hellonode', name='hello') c.start(container, port_bindings={1337: ('0.0.0.0', 1338)})
但我不能访问在公共端口1338容器(这与第一个命令正常工作) – 我得到连接拒绝错误。 有谁知道如果我错过了一些选项,使Python调用创buildfunction,可访问的容器?
检查集装箱告诉我,港口设置应该是这样的:
$ sudo docker port hello 1337 0.0.0.0:1338
我也尝试了create_container
调用中的ports=[1337]
选项,但是也没有帮助。
更新:似乎这是某种错误 。 解决方法是明确指定TCP:
container = c.create_container('kermit/hellonode', name='hello', ports=[(1337, 'tcp')])
我可以确认这是行不通的。
这个方法工作正常,它可能对你有用:
container = c.create_container('kermit/hellonode', name='hello', ports=[1337]) c.start(container, publish_all_ports=True) info = c.inspect_container(container) host_port = info['NetworkSettings']['Ports']['1337'][0]['HostPort']
然后,您可以访问服务在0.0.0.0:<host_port>