简体   繁体   English

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

[英]How to run basic web app container in docker-py?

I am trying to mimic running a containerized web app exposed at some port 我试图模仿运行在某个端口暴露的容器化Web应用程序

sudo docker run -d -p 1338:1337 kermit/hellonode

in Python using docker-py . 在Python中使用docker-py So far I got this code to start the instance: 到目前为止,我得到了这个代码来启动实例:

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

But I can't access the container at the public port 1338 (which works normally with the first command) - I'm getting connection refused errors. 但我无法访问公共端口1338(通常使用第一个命令正常工作)的容器 - 我收到连接拒绝错误。 Does anyone know if I'm missing some option to make the Python call create the functional, accessible container? 有没有人知道我是否缺少一些选项让Python调用创建功能的,可访问的容器?

Inspecting the container tells me that the port is set up as it should be: 检查容器告诉我端口设置应该是:

$ sudo docker port hello 1337
0.0.0.0:1338

I also experimented with the ports=[1337] option in the create_container call, but it didn't help either. 我还在create_container调用中尝试了ports=[1337]选项,但它也没有帮助。

Update: seems this is some kind of bug . 更新:似乎这是某种bug The workaround is to specify TCP explicitly: 解决方法是明确指定TCP:

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

I can confirm that this does not work. 我可以确认这不起作用。

This methods works ok, it may be useful for you: 这种方法可行,它可能对您有用:

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']

Then, you can access service at 0.0.0.0:<host_port> 然后,您可以访问0.0.0.0:<host_port>服务0.0.0.0:<host_port>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM