如何在docker-py中运行基本的web应用程序容器?

时间:2014-01-14 11:40:34

标签: python docker dockerpy

我正在尝试模仿运行在某个端口公开的容器化Web应用程序

sudo docker run -d -p 1338:1337 kermit/hellonode
使用docker-py在Python中

。到目前为止,我得到了这个代码来启动实例:

container = c.create_container('kermit/hellonode', name='hello')
c.start(container, port_bindings={1337: ('0.0.0.0', 1338)})

但我无法访问公共端口1338(通常使用第一个命令正常工作)的容器 - 我收到连接拒绝错误。有没有人知道我是否缺少一些选项让Python调用创建功能的,可访问的容器?

检查容器告诉我端口设置应该是:

$ sudo docker port hello 1337
0.0.0.0:1338

我还在ports=[1337]调用中尝试了create_container选项,但它也没有帮助。

更新:似乎是some kind of bug。解决方法是明确指定TCP:

container = c.create_container('kermit/hellonode', name='hello', ports=[(1337, 'tcp')])

1 个答案:

答案 0 :(得分:3)

我可以确认这不起作用。

此方法正常,可能对您有用:

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>

访问服务