Reputation: 3
I'm trying to run a container using docker-py and bind ports with the host. I'm afraid I'm not experimented with Docker, but I try many things and I can't see where my mistake is.
My docker API version is 1.22
Here is my code for create the container :
from docker import Client
cli = Client(base_url='tcp://172.16.3.87:2375', version='1.22')
container_id = cli.create_container( 'busybox', 'ls', name='test', ports=[1111], host_config=cli.create_host_config(port_bindings={ 1111:8000 }, publish_all_ports=True) )
print(container_id)
The container is created, but there is no bind :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3ad8566958d busybox "ls" 42 seconds ago Created test
However, the host received the command. I captured the incoming traffic on 2375 port. Here is the result :
{"Tty": false, "NetworkDisabled": false, "Image": "busybox", "Cmd": ["ls"], "StdinOnce": false, "HostConfig": {"PortBindings": {"1111/tcp": [{"HostPort": "8000", "HostIp": ""}]}, "PublishAllPorts": true}, "AttachStdin": false, "MemorySwap": 0, "Memory": 0, "ExposedPorts": {"1111/tcp": {}}, "AttachStderr": true, "AttachStdout": true, "OpenStdin": false}
When I try to bind on the host directly, it runs without any problem.
Has someone experimented the same issue ?
Thank you by advance!
Upvotes: 0
Views: 1490
Reputation: 5223
The code you posted creates a container. In order to bind the ports, you need to start the container.
from docker import Client
cli = Client(base_url='tcp://172.16.3.87:2375', version='1.22')
container_id = cli.create_container(
'busybox',
'ls',
name='test',
ports=[1111],
host_config=cli.create_host_config(port_bindings={ 1111:8000 }, publish_all_ports=True)
)
response = cli.start(container=container_id.get('Id'))
print(container_id)
print(response)
Once you start the container it should bind the ports.
Upvotes: 1