1

I am using the docker-py client to create containers on a need basis. So for this, I am using a generator to come up with a port number and trying to use httpd image on a particular port of the host from the generator. But, the client gives out ("invalid port specification: "port number here"") for any number, for any port number that I am trying to use.

Below is sample code that I am trying:

import docker
client = docker.from_env()
container= client.containers.run(image="httpd", ports={'80/tcp': 43545}, detach=True)

To note: The number 43545 does not have any significance here.

Docker details:

  1. Client - 19.03.6
  2. API Version - 1.40
  3. Engine: 19.03.6

Error:

File "/project/api/.venv/lib/python3.7/site-packages/docker/models/containers.py", line 803, in run
detach=detach, **kwargs)
File "/project/api/.venv/lib/python3.7/site-packages/docker/models/containers.py", line 861, in create
resp = self.client.api.create_container(**create_kwargs)
File "/project/api/.venv/lib/python3.7/site-packages/docker/api/container.py", line 429, in create_container
return self.create_container_from_config(config, name)
File "/projectapi/.venv/lib/python3.7/site-packages/docker/api/container.py", line 440, in create_container_from_config
return self._result(res, True)
File "/projectpi/.venv/lib/python3.7/site-packages/docker/api/client.py", line 267, in _result
self._raise_for_status(response)
File "/projectapi/.venv/lib/python3.7/site-packages/docker/api/client.py", line 263, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/projectyter/api/.venv/lib/python3.7/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 400 Client Error: Bad Request ("invalid port specification: "43545"")

j-code
  • 11
  • 2
  • Your example works for me. Could you try running this command without the `ports` argument? Just to see what happens. – Hami May 22 '20 at 11:43
  • without ports, it creates a container. The behaviour of the docker-py wrt ports is erratic and I am unable to understand what is actually causing that error. – j-code May 22 '20 at 12:37
  • So this part works. Let's see if you can spawn a container with ports from a shell. Please run `docker run -it --rm -p 43545:80 httpd` and then using a browser, navigate to `localhost:43545`. You should get to a page which simply says: "It works!" – Hami May 22 '20 at 12:41
  • 1
    Hi, i seem to have understood what the error means. the invalid client port specification error is thrown in scenarios where the port number you are trying to assign is out of range. https://en.m.wikipedia.org/wiki/Ephemeral_port this link gave me a better understanding. Thanks. – j-code May 24 '20 at 17:01

0 Answers0