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.
--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")
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.