Xt error: Can't open display, if using default DISPLAY

流过昼夜 提交于 2019-12-03 11:14:10

I hadn't restarted after re-installing XQuartz. I restarted, and now it works. :).

Dockerized UI Apps in Docker for Desktop MacOS 2018+

Went through all the pain to get the simplest version possible that does not depend on checking port, ip, etc... Here it is.

  • Running version XQuartz 2.7.11 (xorg-server 1.18.4)
  • Docker version docker version 18.06.1-ce

Make sure to install XQuartz

$ brew install socat
$ brew cask reinstall xquartz
  • Don't forget to close logout and log back in.

1. Close any 6000

On a new terminal, verify if there's anything running on port 6000

$ lsof -i TCP:6000
$

If there is anything, just kill the process

2. Close any 6000

Open a socket on that port and keep the terminal open

$ socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"

3. Verify 6000 is open

In a new terminal, verify if it is opened

$ lsof -i TCP:6000
COMMAND   PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
socat   29298 mdesales    5u  IPv4 0xe21e43ca9d99bf1d      0t0  TCP *:6000 (LISTEN)

4. Build and Run simple UI App

$ cat Dockerfile.eyes
FROM centos
RUN yum install -y xeyes
CMD ["/usr/bin/xeyes"]
$ docker build -t eyes -f Dockerfile.eyes .

The magic happens using the variables from Docker. Just using the -e DISPLAY=docker.for.mac.host.internal:0 did the trick, as it it will point to the internal IP address and provide that to the docker image. The port forward will do its magic.

$ docker run -e DISPLAY=docker.for.mac.host.internal:0 eyes

I noticed that at this point XQuartz is opened on it own to the same port

$ lsof -i TCP:6000
COMMAND   PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
socat   29298 mdesales    5u  IPv4 0xe21e43ca9d99bf1d      0t0  TCP *:6000 (LISTEN)
X11.bin 29462 mdesales    8u  IPv6 0xe21e43ca7cdb1135      0t0  TCP *:6000 (LISTEN)

5. Profit and run more apps

$ docker run -e DISPLAY=docker.for.mac.host.internal:0 jess/tor-browser

$ docker run -e DISPLAY=docker.for.mac.host.internal:0 batmat/docker-eclipse

I needed to quit Terminal and then relaunch it in order to get it to work.

For OS X 10.6.3 and later, per XQuartz 2.7.11 instructions:

If this is your first time installing XQuartz, you may wish to logout and log back in. This will update your DISPLAY environment variable to point to XQuartz.app rather than X11.app. If you would prefer to keep using X11.app as your default server (you can still launch XQuartz.app manually), you’ll want to disable /Library/LaunchAgents/org.macosforge.xquartz.startx.plist using launchctl(1).

After installing XQuartz 2.7.11 on my macOS High Sierra, logging out of my Mac and logging in again was enough for this to work via my MacOS Terminal. However, you may avoid having to logout and log in by opening the XQuartz Terminal application (XQuartz > Applications > Terminal), and running your X application from there. For example:

and then

bash-3.2$ xclock &
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!