Overview
I'm attempting to get XQuartz to work on OSX so I can do X11 forwarding via Docker. I'm following the instructions here. I believe my question may be answered by just the first part, but just in case (to avoid the XY problem), I've provided the second part as well.
Installation
I've installed it via homebrew, via brew cask install xquartz
. Then I open -a XQuartz
to start it.
Local xterms
Testing it out, if I try to open an xterm
, it does not work:
MacBook-Pro:opencv-gui csaftoiu$ xterm
xterm: Xt error: Can't open display: /private/tmp/com.apple.launchd.3wncZULdXC/org.macosforge.xquartz:0
The pseudo-file exists, though:
MacBook-Pro:opencv-gui csaftoiu$ echo $DISPLAY
/private/tmp/com.apple.launchd.3wncZULdXC/org.macosforge.xquartz:0
MacBook-Pro:opencv-gui csaftoiu$ ls -alh $DISPLAY
srw-rw-rw- 1 csaftoiu wheel 0B May 6 21:12 /private/tmp/com.apple.launchd.3wncZULdXC/org.macosforge.xquartz:0
I can open an xterm via XQuartz. Then:
bash-3.2$ echo $DISPLAY
:0
This value works from a regular OSX too:
$ DISPLAY=:0 xterm
# opens xterm, waits for it to finish
$
The following do not work though, not sure why based on the answer here:
xterm: Xt error: Can't open display: localhost:0
MacBook-Pro:opencv-gui csaftoiu$ DISPLAY=127.0.0.1:0 xterm
xterm: Xt error: Can't open display: 127.0.0.1:0
MacBook-Pro:opencv-gui csaftoiu$ DISPLAY=`ipconfig getifaddr en0`:0 xterm
xterm: Xt error: Can't open display: 192.168.1.15:0
Note that xinit
does work for some reason:
$ xinit
xinit: XFree86_VT property unexpectedly has 0 items instead of 1
# opens xterm, waits for it to finish
xinit: connection to X server lost
waiting for X server to shut down
Question 1: What is XQuartz actually listening on?
Docker Forwarding with socat
In any case, moving on, this socat
command does not work:
MacBook-Pro:opencv-gui csaftoiu$ socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
Running that, from another window I do:
MacBook-Pro:opencv-gui csaftoiu$ docker run --rm -it -e DISPLAY=`ipconfig getifaddr en0`:0 ubuntu:14.04 bash
root@912eec31b8cb:/# apt-get update && apt-get install xterm
... such install, wow ...
root@912eec31b8cb:/# xterm
Warning: This program is an suid-root program or is being run by the root user.
The full text of the error or warning message cannot be safely formatted
in this environment. You may get a more descriptive message by running the
program as a non-root user or by removing the suid bit on the executable.
xterm: Xt error: Can't open display: %s
root@912eec31b8cb:/# echo $DISPLAY
192.168.1.15:0
From the socat
window I get:
2016/06/14 21:08:15 socat[24289] E connect(5, LEN=68 AF=1 "/private/tmp/com.apple.launchd.3wncZULdXC/org.macosforge.xquartz:0", 68): Connection refused
I can't use the DISPLAY variable that works, either:
MacBook-Pro:opencv-gui csaftoiu$ socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\":0\"
2016/06/14 21:09:43 socat[24309] E connect(5, LEN=4 AF=1 ":0", 4): No such file or directory
Now, this is not a UNIX-CLIENT IP. But, I don't know what DISPLAY=:0
is connecting to. It's certainly not port 6000 since that's the port it used to be listening on. If I change it to 6005
, to forward to 6000
, and make the Docker container DISPLAY be $(ipconfig getifaddr en0):5
instead, then the connection is of course refused:
$ socat TCP-LISTEN:6005,reuseaddr,fork TCP:localhost:6000
2016/06/14 21:20:32 socat[25379] E connect(8, LEN=16 AF=2 127.0.0.1:6000, 16): Connection refused
Question 2: How to proceed from here?
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 &
来源:https://stackoverflow.com/questions/37826094/xt-error-cant-open-display-if-using-default-display