port_bindings don't really expose the container
See original GitHub issueI originally asked this on SO, but since more people confirmed that this seems like unexpected behaviour, I’ll report it here as well.
I am trying to mimic running a containerized web app exposed at some port
sudo docker run -d -p 1338:1337 kermit/hellonode
in Python using 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. Does anyone know if I’m missing some option to make the Python call create the functional, accessible container?
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.
As the answer noted, this works if publish_all_ports
is used, but then you can’t control the port number.
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']
Issue Analytics
- State:
- Created 10 years ago
- Comments:11 (8 by maintainers)
Top GitHub Comments
Ok, I found what the problem was. When creating the container, if you don’t define the protocol type (TCP or UDP), the
HostConfig
will contain an invalid port mapping for the same port that was requested on thestart()
call:This seems to confuse Docker and it doesn’t create the
iptables
NAT rule. If you do the following, it works:Checks out:
So, what’s the final word? I agree that the Docker API shouldn’t accept it, but to prevent people from falling into the trap/bug while using
docker-py
we could default totcp
increate_container()
if the protocol is not specified, what do you think?