-1

I am running a server within a docker container on my machine. I want to send HTTP requests from my machine (host) to the server which is inside the container. When I send a GET request with HTTP inside the container, it detects the server. But if I send the same request to the IP address of my container (from the host, outside the container, this time) it tells me this :

Traceback (most recent call last):
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/urllib3/connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/urllib3/util/retry.py", line 410, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/urllib3/packages/six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/urllib3/connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/urllib3/connectionpool.py", line 426, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/urllib3/connectionpool.py", line 421, in _make_request
    httplib_response = conn.getresponse()
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/http/client.py", line 1344, in getresponse
    response.begin()
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/http/client.py", line 306, in begin
    version, status, reason = self._read_status()
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/http/client.py", line 267, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

or this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/home/user/miniconda3/envs/dl_project/lib/python3.7/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='172.17.0.2', port=8000): Max retries exceeded with url: /hello (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f5312c54690>: Failed to establish a new connection: [Errno 111] Connection refused'))

Here is the command I use when I start the container:

docker run -it --publish 8000:8000 my_server:v1

And the Python script I run for sending the request:

import requests
print(requests.get("http://172.17.0.2:8000/hello").text

Here this the command I used for finding the IP address of the container:

$ docker inspect d6c7d98b9fdd | grep IP
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,

I am aware of this question, but I am not using docker files to create the image (I pulled it from docker hub) so the answer does not help me. I also found this one but it did not help me.

Thank you in advance for your help.

anemyte
  • 17,618
  • 1
  • 24
  • 45
ava_punksmash
  • 357
  • 1
  • 4
  • 13

1 Answers1

0

--publish 8000:8000 makes a port-forwarding rule to direct packets received on host's port 8000 (left value) to container's port 8000 (right value). That is you can reach your container with <hostIpOrName>:8000. On the host you can use localhost:8000.

You had better not use container IP in your code because it can change frequently.

It should be also noted that in order to have this working, your backend should listen for something other than loopback IP-address (127.0.0.1 or similar). Use 0.0.0.0 to allow the application to accept connections from any IP. For ray this can be achieved with serve.start(http_host="0.0.0.0").

anemyte
  • 17,618
  • 1
  • 24
  • 45
  • I tried ```print(requests.get("http://127.0.0.1:8000/hello").text)``` but it did not work :/ – ava_punksmash Feb 10 '21 at 09:45
  • @ava_punksmash The app inside the container, does it listen for any IP (`0.0.0.0`) or just localhost/128.0.0.1? If it's the latter then any external communication is not possible, unless you deactivate network isolation by running the container in host's network mode. – anemyte Feb 10 '21 at 10:04
  • 1
    thx for your answer, I am running a "ray serve" instance, which is itself connected to a "ray cluster". From what I understand it is a server connected to another server. But it is said that it is listening on 127.0.0.1:8000 : ```2021-02-10 01:43:34,664 INFO worker.py:657 -- Connecting to existing Ray cluster at address: 172.17.0.2:6379 (pid=128) 2021-02-10 01:43:35,222 INFO controller.py:346 -- Starting router with name 'SERVE_CONTROLLER_ACTOR:SERVE_PROXY_ACTOR-node:172.17.0.2-0' on node 'node:172.17.0.2-0' listening on '127.0.0.1:8000' (pid=153) INFO: Started server process [153] ``` – ava_punksmash Feb 10 '21 at 10:16
  • In case you don't know ray serve and would like to know more: https://docs.ray.io/en/master/serve/index.html – ava_punksmash Feb 10 '21 at 10:18
  • @ava_punksmash That's exactly the case. You need to make it to listen for any IP-address so that you can access it from the host or other machine on the network: `serve.start(http_host="0.0.0.0")` – anemyte Feb 10 '21 at 10:26
  • OK I understand, before running it inside the container I started by running it on the host, it tells me ```(pid=7720) 2021-02-10 11:31:16,721 INFO controller.py:346 -- Starting router with name 'SERVE_CONTROLLER_ACTOR:SERVE_PROXY_ACTOR-node:192.168.1.43-0' on node 'node:192.168.1.43-0' listening on '0.0.0.0:8000' (pid=7744) Task exception was never retrieved (pid=7744) future: exception=OSError(98, 'Address already in use')>``` – ava_punksmash Feb 10 '21 at 10:33
  • I guess I must find what is already running – ava_punksmash Feb 10 '21 at 10:34
  • @ava_punksmash probably the container. – anemyte Feb 10 '21 at 10:40
  • OK so I am working on it, now it is working locally, I will try it in the container – ava_punksmash Feb 10 '21 at 10:48