Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
X11 in docker on macOS

To forward X11 from inside a docker container to a host running macOS

  1. Install XQuartz: https://www.xquartz.org/
  2. Launch XQuartz. Under the XQuartz menu, select Preferences
  3. Go to the security tab and ensure "Allow connections from network clients" is checked.
  4. Run xhost + ${hostname} to allow connections to the macOS host *
  5. Setup a HOSTNAME env var export HOSTNAME=`hostname`*
  6. Add the following to your docker-compose:
  environment:
  - DISPLAY=${HOSTNAME}:0
  volumes:
  - /tmp/.X11-unix:/tmp/.X11-unix

* It should be noted that steps 4 and 5 can be automated to run everytime XQuartz starts, but that's outside of the scope of this gist

@fapfap69

This comment has been minimized.

Copy link

fapfap69 commented Nov 11, 2017

Thanks a lot Sir !
It works fine...

@elehcimd

This comment has been minimized.

Copy link

elehcimd commented Mar 21, 2018

it worked for me as well, but after restarting Quartz after step 3

@loadaverage

This comment has been minimized.

Copy link

loadaverage commented Mar 27, 2018

Seems that ${hostname} is not correct, should it be ${HOSTNAME} or $(hostname)?
Anyway, I have troubles while trying binding X11 to UNIX socket, network connections works fine.
But it's not so comfortable for me.
UPDATE: nevermind, UNIX sockets are not supported yet
So, xhost or socat (UNIX socket->localhost port) is the only way to go.

@k2973363

This comment has been minimized.

Copy link

k2973363 commented Jul 12, 2019

Works well!

@DrSnowbird

This comment has been minimized.

Copy link

DrSnowbird commented Sep 20, 2019

For my MacOS docker run to display X11, I need to set up docker run with "-e DISPLAY=host.docker.internal:0" and then it works!

@vmayoral

This comment has been minimized.

Copy link

vmayoral commented Oct 29, 2019

+1 to -e DISPLAY=host.docker.internal:0 from @DrSnowbird, it seems that after the last Docker security updates, that form works and previous ones do not anymore.

@xeonqq

This comment has been minimized.

Copy link

xeonqq commented Dec 24, 2019

Instead of step 4, I have to do the following:

IP=$(ifconfig en1 | grep inet | awk '$1=="inet" {print $2}')
/usr/X11/bin/xhost +
/usr/X11/bin/xhost + "$IP"

and +1 also to DrSnowbird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.