PyCharm refuses connection from Docker remote interpreter with plots over VcXsrv.
I have successfully configured a remote interpreter in a Docker image. Everything works fine getting messages to/from the interpreter (printing, stdout, stderr, everything) until the moment I want to display plots using VcXsrv. PyCharm then raises ConnectionRefusedError: [Errno 111] Connection refused. The connection with the Docker server is carried out via exposure tcp://localhost:2375, but the "Docker for Windows" option leads to the same error.
Error: failed to send plot to http://127.0.0.1:63342
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/urllib/request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/opt/conda/lib/python3.6/http/client.py", line 1262, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/opt/conda/lib/python3.6/http/client.py", line 1308, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/opt/conda/lib/python3.6/http/client.py", line 1257, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/opt/conda/lib/python3.6/http/client.py", line 1036, in _send_output
self.send(msg)
File "/opt/conda/lib/python3.6/http/client.py", line 974, in send
self.connect()
File "/opt/conda/lib/python3.6/http/client.py", line 946, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/opt/conda/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/opt/conda/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/.pycharm_helpers/pycharm_display/datalore/display/display_.py", line 60, in _send_display_message
urlopen(url, buffer)
File "/opt/conda/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/opt/conda/lib/python3.6/urllib/request.py", line 526, in open
response = self._open(req, data)
File "/opt/conda/lib/python3.6/urllib/request.py", line 544, in _open
'_open', req)
File "/opt/conda/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/opt/conda/lib/python3.6/urllib/request.py", line 1346, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/opt/conda/lib/python3.6/urllib/request.py", line 1320, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 111] Connection refused>
Process finished with exit code 0
Note that the process exits with exit code 0 from the perspective of the python interpreter in the Docker container. The plotting script I am running only contains the following:
#plotting.py
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(100)
y = np.sin(x)
plt.plot(x,y)
plt.show()
All python dependencies are correctly installed, and colleagues of mine are developing with their PyCharms and the same remote Docker environment/interpreter as me, and they do not get any issue. In order to be able to display plots in the Windows host generated by the interpreter in the Docker container, I have a VcXsrv server set up. Correspondingly, I need to set up the DISPLAY env variable. My Run/Debug Configurations Docker container settings are:
-e DISPLAY=192.168.0.150:0 --entrypoint -v C:/Users/project/path:/opt/project --rm
and if I run the task "manually" from a Powershell everything works fine. A client gets created, connects to the VcXsrv server and an interactive matplotlib plot gets returned:
docker run -it -e DISPLAY=192.168.0.150:0 --entrypoint python -v C:/Users/project/path:/opt/project --rm dockerimage /path/to/file/plotting.py
Similarly, if I set up the equivalent Docker run under Run | Edit Configurations... it also works (I just need to manually pass it each time the file it has to run):

To me, it looks as if PyCharm is refusing external connections on the default port 63342. Amongst all the things I have tried to debug this issue I have found an option to change the built-in server port under Settings | Build, Execution, Deployment | Debugger and also to explicitly make it accept external connections (for ports different than 63342), with no effect (requests keep coming under 63342). I have not found any other way to set ports
With regard to the above exception, I do not understand why when handling the original exception it tries to go over Datalore. For what is worth, just mentioning that under Settings | Tools | Datalore I do not have any active Token.
About:
PyCharm 2020.2.1 (Professional Edition)
Build #PY-202.6948.78, built on August 25, 2020
Runtime version: 11.0.8+10-b944.31 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
GC: ParNew, ConcurrentMarkSweep
Registry: debugger.watches.in.variables=false
Non-Bundled Plugins: com.carbonblack.intellij.rpmspec
Windows 10 10.0
Version: 2.3.0.4 (46911)
Engine: 19.03.12
Any help in solving this is greatly appreciated. Thanks in advance.
D.
Please sign in to leave a comment.
Hi,
PyCharm doesn't support showing plots with Docker-based interpreters. Here is a relevant comment https://youtrack.jetbrains.com/issue/PY-18102#focus=Comments-27-1412306.0-0
Unfortunately, I don't have any ideas on why your workaround doesn't work even though you have the same configuration as your colleagues. Sorry about that.
Hey Sergey,
Could you please elaborate on why would be PyCharm refusing connection requests on port 63342 and if there are ways to bypass that (selecting other ports etc)? Or why the ports settings I found under Settings | Build, Execution, Deployment | Debugger did not change anything?
Could you also provide some information about the Docker image .pycharm_helpers that is being used under the hood?
Thanks,
D.
Those settings are for JS-related built-in web server https://www.jetbrains.com/help/phpstorm/php-built-in-web-server.html
They do not apply to Python debugger.
Any way for me to avoid port 63342?
I got the same problem. The problem is that the domain is wrong i.e. 127.0.0.1 does not refer to the host from inside the docker container (see https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container). It would be nice if this would be somehow configurable preferably via an environment variable.
I think running in host mode should work, but if you use a docker-compose interpreter then it is not possible (pycharm 2020.2). Dunno about the docker interpreter.
@Seiler Looking at the error traceback carefully it stroke me not only the host IP address, but moreover, the fact that PyCharm was trying to push the plots for rendering somewhere via HTTP (instead of using the X protocol to send it to the X server on my Windows machine for rendering, as it was the idea). A bit further up in the logs there was a message regarding some matplotlib backend.
It turns out that PyCharm sets its own matplotlib backend when using the "scientific mode". Thus, the fix for me was going to Files | Settings | Tools | Python Scientific and ensuring the checkbox "Show plots in tool window" is UNchecked.
Good luck!
oh yes. i actually did that and it woks for me but only if i set the environment variable
and mount the following volume in docker
Then i'll get plots but it is still a bit problematic. The console sometimes crashes when i close a plot and execute some code ....
Of course the python interpreter whithin the container needs to know where is the X server located that should render the plot, therefore you have the DISPLAY environment variable. The correct value for setting the DISPLAY variable is not
but:
As an example, see my original post question with the DISPLAY env var correctly defined and passed to the docker run:
That worked for me just fine and I did not need to mount any X server directory in the run (but I am on a Windows machine and using VcXsrv. Since your host seems to be a Linux machine, it could be that you have to make that step manually.