简体   繁体   English

如何使用docker-py绑定端口

[英]how to bind ports with docker-py

I try to start a docker container with docker-py (Version 1.3.1). 我尝试使用docker-py(版本1.3.1)启动Docker容器。 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告诉我:

$ 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 但是如果我给它映射3000-> 3000,2424-> 2424和2480-> 2480

$ 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 docker运行-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. 但是,使用docker-py,我无法弄清楚如何将端口映射到不同的端口号。 What am I missing? 我想念什么?

You have to publish and expose the ports when using docker-py. 使用docker-py时必须发布公开端口。 (When you publish with docker run , the ports are implicitly exposed) (当您使用docker run发布时,端口被隐式公开)

example: 例:

container = config['connection'].create_container(
    image=imageName,
    name=containerName,
    ports=[2424],
    host_config=create_host_config(port_bindings={2424:2425})
)

The issue was that docker-py puts the container ports first in its host configuration while the docker client puts them second. 问题是docker-py在其主机配置中将容器端口放在第一位,而在Docker客户端中将容器端口放在第二位。 More interesting though is how I finally found it out. 不过,更有趣的是我终于发现了它。 The trick was to install socat and then 诀窍是先安装socat ,然后再安装

$ 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. 这样可以方便地查看docker客户端以及docker-py客户端的流量。

I searched inside for the PortBindings strings. 我在里面搜索PortBindings字符串。 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. 问题不在于未公开端口而是端口的错误顺序。

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

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