How to forward Docker for Mac to X11?
Solution
After installing XQuartz you will need to restart your computer as horcle_buzz suggested; starting a new terminal session is not enough.
After a reboot you can grant access for whatever you need using the xhost
command. Just be careful and understand the consequences of too permissive a rule. This other discussion has more info: Running Chromium inside Docker - Gtk: cannot open display: :0
Additional findings
Some extra things I discovered along the way:
- 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 withdocker 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 callingdocker run
.
I tested this with XQuartz 2.7.11 and Docker Desktop 2.1.0.2 on macOS Mojave (10.14.6).
I have xQuartz 2.7.11 installed on Mac OS.
I was getting the following error
<program>: cannot connect to X server <ip>:0
when trying to run docker container. I tried downgrading XQuartz but it still didn't fix my issue.
A crucial piece to to the setup is to add your ip access control list for xhost.
xhost + $(ipconfig getifaddr en0)
After adding the ip to xhost, everything works. There is no need to downgrade Xquartz version.
Turns out there's a bug in XQuartz 2.7.9.
When using XQuartz 2.7.10_beta2, it works.