情况:在 Windows 主机上的 docker 容器中运行 CentOS 终端。已安装 cv2,运行良好。
脚本:
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:])
错误:
: cannot connect to X server
尝试解决方案:[root@b26030f33e65 PythonScripts]# xhost local:root
xhost: unable to open display ""
最佳答案
对于 Windows,您可以尝试各种 Xserver 实现中的一种,最简单的一种是 VcXsrv ,其他实现可从 Cygwin 世界获得,也有一些商业解决方案。
主要想法是启动 Xserver 使其在所有接口(interface)上监听 TCP 并且没有任何身份验证,然后通过 TCP 从容器连接到它
inside_container > export DISPLAY="<some ip>:0"
inside_container > # start your whatever
注意:监听所有接口(interface)并且没有 auth 是不安全的,你应该只在初始阶段使用它,直到你让事情运行起来,之后你应该采取必要的步骤来减少“暴露表面”。
现在......我不太确定如何让你的 IP 对容器可见,但我希望这足以让你开始。
稍后编辑:
我找到了 this (过于复杂)分步指南,基本上说的几乎相同:
DISPLAY
env var 指向你的“显示”# 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
关于Python cv2.imshow() 不工作 : cannot connect to X server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58922358/