14

I'm trying to make GET/POST requests between docker containers.

In one of them (the others work fine), when doing the following:

ADDR = 'http://172.20.0.2:5002/see'
r = requests.post(ADDR, data=data, headers=headers)

the command hangs forever (or until timeout, if one is defined) on trying to connect. It never even establishes the connection:

^CTraceback (most recent call last):
  File "/workspace/master_server.py", line 35, in <module>
    r = requests.post(ADDR, data=data, headers=headers)
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1151, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 166, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.5/dist-packages/urllib3/connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.5/dist-packages/urllib3/util/connection.py", line 73, in create_connection
    sock.connect(sa)

If I run the exact same request using curl from bash, it works effortlesly:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '"[1215 2589 1392 3019]"' 'http://172.20.0.2:5002/see'

Any other containers that I try to use in the exact same way work with either the requests module or curl. Any help on how to fix it or at least debug the problem effectively?

3
  • I have the exact same problem right now. It worked on windows but ubuntu hangs now (using python3, robobrowser) Commented Nov 6, 2018 at 12:37
  • same problem, works locally on windows and hangs on ubuntu ec2 server Commented Jul 26, 2019 at 20:49
  • are you trying curl inside the container? Commented Oct 8, 2020 at 10:04

4 Answers 4

8

I recently ran into the same issue locally, and like some comments on the original question, my code worked in windows, but not linux. After a few hours found the culprit for my case: IPv6.

The python module I was using attempts to make a socket connection with a server and was by default using IPv6. After turning IPv6 off in my linux environment (Ubuntu 18.04) with the following steps:

  1. Open terminal
  2. Enter: sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
  3. Enter: sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1

The module worked as expected**.

**note that these steps will likely vary per environment

Sign up to request clarification or add additional context in comments.

Comments

2

I faced the same problem and lost a lot of time ... In my case, the problem was as follows (guniconf.py):

workers = 1

change to:

workers = 10  # any number is greater than 1

More: There is a Flask application that serves some REST API. The application has a REST API method (let's call it A-method), which makes inside itself a call to another REST API method (let's call it B-method) using requests.post(). When the B-method is called from A-method, app hangs. As I understand it, when calling the A-method, the gunicorn worker processes it, but with the B-method, there are no more workers.

Comments

1

I had the same issue on macOS (Catalina) recently and it turned out to be due to IPv6.

I had to go to System Preferences -> Network -> Wi-Fi -> Advanced -> TCP/IP and change Configure IPv6 to Link-local only (there wasn't an "Off" option in the list, though this would also be applicable). Then click OK and Apply.

You can also disable altogether using:

sudo networksetup -setv6off Wi-Fi

1 Comment

Yet another Stack Overflow save. Thank you for your input here. You just solved my problem on MacOS. +1
0

I encountered this error when running my Debian container with docker compose, on a MacOS host. Based on @Ben Elliot's answer, I disabled IPv6 by adding this to the containers config in my docker-compose.yaml:

sysctls:
- net.ipv6.conf.all.disable_ipv6=1

Example:

services:
  srv_a:
    build: ...
    sysctls:
    - net.ipv6.conf.all.disable_ipv6=1
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.