skip to Main Content

situation: Running a CentOS terminal in a docker container on a windows host. cv2 installed, working perfectly.
script:

import sys
import cv2
def main(argv):
        inputfile = ''
        for arg in argv:
                img = cv2.imread(arg)
                cv2.imshow('Image', img)
                cv2.waitkey(0)
if __name__ == "__main__":
        main(sys.argv[1:])

Error: : cannot connect to X server
Tried solution:
[root@b26030f33e65 PythonScripts]# xhost local:root
xhost: unable to open display ""

2

Answers


  1. On unix-like systems, you can pass-through the host’s X server (if any) to the docker container by setting up a set of required mounts (see this article for an idea).

    Since you are on Windows, there’s hardly any chance that you can pass-through X from your host to your container since Windows does not run an X server; it uses other systems for on-screen rendering.

    I think the only possible solution for you is to mount a directory from your host to your container and write your image to a file in that directory. It’s then easily accessible from your host system.

    Just run your container with arguments similar to:

    docker run
      --rm -ti
      --volume /path/to/host/directory:/path/to/container/directory:rw
      --entrypoint=/bin/bash
      yourcentosimagename
    
    Login or Signup to reply.
  2. For windows you could try one of the various Xserver implementations, the most simple one being VcXsrv, other implementations are available from the Cygwin world and there are also some comercial solutions.

    The main idea would be to start the Xserver making it to listen on TCP on all interfaces and without any auth then connect to it from the container over TCP

    inside_container > export DISPLAY="<some ip>:0"
    inside_container > # start your whatever
    

    Note: listening on all interfaces and without auth is not the safe, you should use it only in the initial fase until you get the things running, afterwards you should take the necessary step to reduce the “exposed surface”.

    Now … I’m not really sure how exactly to make you IP visible to the container, but I hope that this could be enough to get you started.

    Later Edit:
    I’ve found this (over-complicated) step-by-step guide, which basically says pretty much the same thing:

    • install VcXsrv (or Xming, or whatever)
    • start it with :
      • “Multiple windows” for a more streamlined experience
      • “no client”
      • “Disable access control” (again only to avoid the initial hassle)
    • whenever you need to run some GUI app, add a DISPLAY env var pointing to your “display”
    # example
    > docker run -ti --rm -e DISPLAY=172.17.0.1:0.0 firefox
    > docker run -ti --rm -e DISPLAY=172.17.0.1:0.0 /bin/sh
    # where "172.17.0.1" would be 1 of your IPs
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search