简体   繁体   中英

How do I connect via SSH to docker image without “Invalid DISPLAY variable”-error by matplotlib when local docker session works fine?

I have a fully working docker-image running/hosted on my Ubuntu 18.04 linux machine. However, connecting to the physical machine via SSH from my Win10 Laptop via PowerShell:

ssh username@machine

I do get the following error from matplotlib when I try to execute my code remotely via ssh:

Traceback (most recent call last):
  File "foo", line 284, in <module>
    cnnTrainTestApply.applyStructureDetectionNet(absPathToCsvFiles, absPathToCnnOutputFiles)
  File "/home/dev/foo.py", line 702, in bar
    plt.figure(figsize=(15, 15))
  File "/opt/conda/lib/python3.5/site-packages/matplotlib/pyplot.py", line 539, in figure
    **kwargs)
  File "/opt/conda/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 171, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/opt/conda/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 177, in new_figure_manager_given_figure
    canvas = cls.FigureCanvas(figure)
  File "/opt/conda/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 35, in __init__
    super(FigureCanvasQTAggBase, self).__init__(figure=figure)
  File "/opt/conda/lib/python3.5/site-packages/matplotlib/backends/backend_qt5.py", line 235, in __init__
    _create_qApp()
  File "/opt/conda/lib/python3.5/site-packages/matplotlib/backends/backend_qt5.py", line 122, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

Neither this, nor ssh -X username@machine do yield in success. Working directly on my machine without ssh does not make any issues. I suppose it is a missing XServer running, or something similar.

What do I get wrong about the ssh connection? How can I resolve the issue?

As matplotlib demands functioning qt5 backend, I can not simply avoid the forwarding of the X-server of the host system to docker.

This solution is a workaround, that needs a logged in user with active X-Server. Which is not optimal.

As supposed DISPLAY is missing during the SSH-Connection. To use it for with matplotlib we have to do the following:

After the ssh login, on the host system "machine" do type following command before connecting to the docker image.

export DISPLAY=:1

This may depend on your machine setup, as long as you have the X-Server running with your current user, you may have to put the output of echo $DISPLAY instead of 1 as export. As long as the same usernames are provided this should work.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM