如何使用docker-py绑定端口 [英] how to bind ports with docker-py
问题描述
我这样做:
$ b $
print(create_host_config,host_config.binds,host_config.port_bindings)
the_host_config = create_host_config (binding = host_config.binds,
port_bindings = host_config.port_bindings);
the_ports = host_config.port_bindings.values();
print(create_container,image_tagged_name,command,the_ports,the_host_config)
cont_id = client.create_container(image = image_tagged_name,command = command,ports = the_ports,host_config = the_host_config)[Id]
目前的情况如下:
create_host_config ['/ dbfiles / test:/ opt / db'] {3001:3000,2425:2424,2481:2480}
create_container test:test ./ initdb.sh [3000,2424,2480] {'Binds':['/ dbfiles / test:/ opt / db'],'PortBindings':{'3001 / tcp':[{'HostPort':'3000' 'HostIp':''}],'2425 / tcp':[{'HostPort':'2424','HostIp':''}],'2481 / tcp':[{'HostPort':'2480' 'HostIp':''}]}}
docker ps告诉我:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
169ad3ae0f63 test:test./initdb.sh5分钟前Up 5分钟2424 / tcp,2480 / tcp,3000 / tcp silly_pasteur
但是,如果我给它映射3000 - > 3000,2424 - > 2424和2480 - > 2480,它会给出
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cba483673bdd test:test./initdb.sh53分钟前Up 5分钟0.0.0.0:2424 - > 2424 / tcp,0.0.0.0:2480->2480/tcp,0.0.0.0:3000->3000/tcp stupefied_ptolemy
重点是从命令行我可以启动具有适当端口映射的容器。那就是
docker run -d -p 3001:3000 -p 2425:2424 -p 2481:2480 -v / dbfiles / test:/ opt / db localhost:5000 / test:test /initdb.sh
给出所需的结果。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c1580e0ace9 localhost:5000 / test:test/initdb.sh8秒前上传6秒0.0.0.0:2425->2424/tcp,0.0.0.0: 2481-> 2480 / tcp,0.0.0.0:3001->3000/tcp backstabbing_brahmagupta
然而与docker-py我只是无法弄清楚如何将端口映射到不同的端口号。问题是docker-py将容器端口首先放在主机配置中,而docker客户端把它们放在第二位。更有趣的是,我终于找到了。诀窍是安装 socat ,然后安装
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ D $ / tmp / debug
这样可以方便地查看docker客户端和docker的流量-py客户端。
我在里面搜索了PortBindings字符串。对于原来的客户端
这给了我:
PortBindings:{
2424 / tcp :[{HostIp:,HostPort:2425}],
2480 / tcp:[{HostIp:,HostPort:2481}],
3000 / tcp:[{HostIp:,HostPort:3001}]
}
对于我的代码,我给了我
PortBindings:{
2425 / tcp:[{HostPort:2424,HostIp:}],
2481 / tcp:[{HostPort:2480,HostIp }],
3001 / tcp:[{HostPort:3000,HostIp:}]
},
这使一切都很明显。这个问题不是没有公开端口,而是端口的顺序错误。
I try to start a docker container with docker-py (Version 1.3.1). I want to map the container internal ports to different ports but fail to expose them properly.
I do this like so:
def start_container(client, host_config, image_tagged_name, command):
print ("create_host_config", host_config.binds, host_config.port_bindings)
the_host_config = create_host_config(binds = host_config.binds,
port_bindings = host_config.port_bindings);
the_ports = host_config.port_bindings.values();
print ("create_container", image_tagged_name, command, the_ports, the_host_config)
cont_id = client.create_container(image=image_tagged_name, command=command, ports=the_ports, host_config=the_host_config)["Id"]
In the case at hand the output is as follows:
create_host_config ['/dbfiles/test:/opt/db'] {3001: 3000, 2425: 2424, 2481: 2480}
create_container test:test ./initdb.sh [3000, 2424, 2480] {'Binds': ['/dbfiles/test:/opt/db'], 'PortBindings': {'3001/tcp': [{'HostPort': '3000', 'HostIp': ''}], '2425/tcp': [{'HostPort': '2424', 'HostIp': ''}], '2481/tcp': [{'HostPort': '2480', 'HostIp': ''}]}}
docker ps tells me:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
169ad3ae0f63 test:test "./initdb.sh" 5 minutes ago Up 5 minutes 2424/tcp, 2480/tcp, 3000/tcp silly_pasteur
However if I give it mappings 3000 -> 3000, 2424 -> 2424 and 2480 -> 2480 it gives
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cba483673bdd test:test "./initdb.sh" 53 minutes ago Up 5 minutes 0.0.0.0:2424->2424/tcp, 0.0.0.0:2480->2480/tcp, 0.0.0.0:3000->3000/tcp stupefied_ptolemy
The point is that from the commandline I can start the container with proper port mappings. That is
docker run -d -p 3001:3000 -p 2425:2424 -p 2481:2480 -v /dbfiles/test:/opt/db localhost:5000/test:test /initdb.sh
gives the desired result.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c1580e0ace9 localhost:5000/test:test "/initdb.sh" 8 seconds ago Up 6 seconds 0.0.0.0:2425->2424/tcp, 0.0.0.0:2481->2480/tcp, 0.0.0.0:3001->3000/tcp backstabbing_brahmagupta
However with docker-py I just can not figure out how to map the ports to different port numbers. What am I missing?
The issue was that docker-py puts the container ports first in its host configuration while the docker client puts them second. More interesting though is how I finally found it out. The trick was to install socat and then
$ socat -v UNIX-LISTEN:/tmp/debug, fork UNIX-CONNECT:/var/run/docker.sock
$ export DOCKER_HOST=unix:///tmp/debug
This allows to conveniently look into the traffic of the docker client as well as the docker-py client.
I searched inside for the PortBindings strings. For the original client this gave me:
"PortBindings": {
"2424/tcp": [{"HostIp":"","HostPort":"2425"}],
"2480/tcp": [{"HostIp":"","HostPort":"2481"}],
"3000/tcp": [{"HostIp":"","HostPort":"3001"}]
}
While for my code it gave me
"PortBindings": {
"2425/tcp": [{"HostPort": "2424", "HostIp": ""}],
"2481/tcp": [{"HostPort": "2480", "HostIp": ""}],
"3001/tcp": [{"HostPort": "3000", "HostIp": ""}]
},
This made everything obvious. The issue was not failure to expose the ports but wrong ordering of the ports.
这篇关于如何使用docker-py绑定端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!