how to bind ports with docker-py

跟風遠走 提交于 2019-12-02 00:52:09

You have to publish and expose the ports when using docker-py. (When you publish with docker run, the ports are implicitly exposed)

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. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!