title: Xt error: Can't open display, if using default DISPLAY
body: <h2>Overview</h2>
  
  <p>I'm attempting to get XQuartz to work on OSX so I can do X11 forwarding via Docker. I'm following <a href="https://learning-continuous-deployment.github.io/docker/images/dockerfile/2015/04/22/docker-gui-osx/" rel="noreferrer">the instructions here</a>. 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.</p>
  
  <h2>Installation</h2>
  
  <p>I've installed it via homebrew, via <code>brew cask install xquartz</code>. Then I <code>open -a XQuartz</code> to start it.</p>
  
  <h2>Local xterms</h2>
  
  <p>Testing it out, if I try to open an <code>xterm</code>, it does not work:</p>
  
  <pre><code>MacBook-Pro:opencv-gui csaftoiu$ xterm
  xterm: Xt error: Can't open display: /private/tmp/com.apple.launchd.3wncZULdXC/org.macosforge.xquartz:0
  </code></pre>
  
  <p>The pseudo-file exists, though:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I <em>can</em> open an xterm via XQuartz. Then:</p>
  
  <pre><code>bash-3.2$ echo $DISPLAY
  :0
  </code></pre>
  
  <p>This value works from a regular OSX too:</p>
  
  <pre><code>$ DISPLAY=:0 xterm
  # opens xterm, waits for it to finish
  $
  </code></pre>
  
  <p>The following do not work though, not sure why based on <a href="https://unix.stackexchange.com/questions/16815/what-does-display-0-0-actually-mean">the answer here</a>:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Note that <code>xinit</code> does work for some reason:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <h3>Question 1: What is XQuartz actually listening on?</h3>
  
  <hr>
  
  <h2>Docker Forwarding with socat</h2>
  
  <p>In any case, moving on, this <code>socat</code> command does not work:</p>
  
  <pre><code>MacBook-Pro:opencv-gui csaftoiu$ socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
  </code></pre>
  
  <p>Running that, from another window I do:</p>
  
  <pre><code>MacBook-Pro:opencv-gui csaftoiu$ docker run --rm -it -e DISPLAY=`ipconfig getifaddr en0`:0 ubuntu:14.04 bash
  root@912eec31b8cb:/# apt-get update &amp;&amp; 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
  </code></pre>
  
  <p>From the <code>socat</code> window I get:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I can't use the DISPLAY variable that works, either:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Now, this is not a UNIX-CLIENT IP. But, I don't know <em>what</em> <code>DISPLAY=:0</code> 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 <code>6005</code>, to forward to <code>6000</code>, and make the Docker container DISPLAY be <code>$(ipconfig getifaddr en0):5</code> instead, then the connection is of course refused:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <h3>Question 2: How to proceed from here?</h3>