如何将Docker for Mac转发到X11?

11

使用 Docker for Mac 1.12 (稳定版) 和 OS X 10.11.5 (15F34),我试图让我的一个容器转发到 X11。

首先,我从我的 bash shell 安装了 XQuartz:

$ brew cask install xquartz

==> Downloading https://dl.bintray.com/xquartz/downloads/XQuartz-2.7.9.dmg
######################################################################## 100.0%
==> Verifying checksum for Cask xquartz
==> Running installer for xquartz; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
==> installer: Package name is XQuartz 2.7.9
==> installer: Installing at base path /
==> installer: The install was successful.
  xquartz staged at '/usr/local/Caskroom/xquartz/2.7.9' (73M)

然后我继续安装XQuartz...

open -a XQuartz

在XQuartz首选项->安全选项卡中,我启用了“允许来自网络客户端的连接”。
然后我应该在我的bash shell中运行xhost + $MY_IP_ADDRESS,但是我得到的只是这个错误:
xhost:  unable to open display ""

最终,我阅读的所有指南都说我应该这样运行我的容器:
docker run -e DISPLAY=$MY_IP_ADDRESS:0 -v /tmp/.X11-unix:/tmp/.X11-unix DOCKER_IMAGE

...但是它并不起作用。在我的特定容器中,我会得到这个错误:

... cannot connect to X server 10.0.0.131:0

我做错了什么?

3个回答

4

原来XQuartz 2.7.9存在一个漏洞。

使用XQuartz 2.7.10_beta2时,它可以正常工作。


1
我也遇到了2.7.11版本的同样问题。我卸载了它并降级到了2.7.10_beta2,现在一切都正常了。我只需要重新启动(而不是注销)就可以让事情开始运作。 - revolutionary
3
这个问题让我很头痛……被降级了,等等。结果只需要重新启动一下就好了。现在在 MacOS 10.13.3 上使用 XQuartz 2.7.11 和 Docker CE 版本 17.12.0 运行非常顺畅。 - horcle_buzz
优秀的工作。当我将XQuaterz降级到2.7.10_beta2时,它可以正常运行。 - Ryan Chou

2
我在Mac OS上安装了xQuartz 2.7.11。
尝试运行docker容器时,我遇到了以下错误: <program>: cannot connect to X server <ip>:0 我尝试了降级XQuartz,但问题仍未解决。
设置的关键部分是将您的IP访问控制列表添加到xhost中。 使用以下命令添加IP到xhost中: xhost + $(ipconfig getifaddr en0) 添加IP到xhost后,一切都正常工作。无需降级Xquartz版本。

2.7.11在我的电脑上(macOS Sierra 10.12.4 + Docker CE 17.03.1)无法运行。我不得不降级到2.7.10_beta2才能使其正常工作。 - Coyotwill
xhost在哪里? - kyb
xhost 命令应该在 XQuartz 的 xterm 窗口中输入。 - radarhere

2

解决方案

安装XQuartz后,您需要重新启动计算机,如horcle_buzz建议的那样;仅启动新的终端会话是不够的。

重新启动后,您可以使用xhost命令授权您需要的任何访问权限。只需小心并了解过于宽松的规则所带来的后果。这个讨论提供了更多信息:在Docker中运行Chromium - Gtk:无法打开显示::0

其他发现

我在途中还发现了一些额外的事情:

  • XQuartz must be running first:
    $ open -a XQuartz
    
  • You can expose 127.0.0.1 instead of looking up your local IP (caveat emptor: I'm not an expert on the implications of this setting):
    $ xhost + 127.0.0.1
    127.0.0.1 being added to access control list
    
  • You can use the special host.docker.internal identifier with docker run:
    $ docker run -e DISPLAY=host.docker.internal:0 jess/firefox
    # ‍♀️ magic ‍♂️
    
  • At least for me, ctrl + c does not shutdown the container; I need to close the X11 window manually.
  • If you quit XQuartz, you must re-auth with xhost after starting it again (the permission doesn't "stick").
  • Doing it this way I didn't need to mount /tmp/.X11-unix when calling docker run.
我使用的是macOS Mojave(10.14.6)上的XQuartz 2.7.11和Docker Desktop 2.1.0.2进行测试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接