Skip to content

Instantly share code, notes, and snippets.

@H2CO3
Forked from paul-krohn/docker_x11_macOS.md
Created August 26, 2021 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save H2CO3/736967dbdaa7dfc91f87304673eac642 to your computer and use it in GitHub Desktop.
Save H2CO3/736967dbdaa7dfc91f87304673eac642 to your computer and use it in GitHub Desktop.
Docker X11 macOS

Preamble

There is a longstanding issue/missing feature/bug with sockets on Docker on macOS; it may never work; you'll need to use a network connection between Docker containers and X11 on macOS for the foreseeable future.

I started from this gist and made some adjustments:

  • the volume mappings aren't relevant/used, due to the socket issue above.
  • this method only allows X11 connections from your Mac, not the entire local network, which would include everyone on the café/airport WiFi.
  • updated to include using the host.docker.internal name for the the container host, instead.
  • you have to restart XQuartz after the config change.

Assumptions

  • you have a docker image with x11-apps (or the equivalent for not-ubuntu) installed
  • you have XQuartz installed (ie from Homebrew, brew cask install xquartz) and your PATH updated to include /opt/X11/bin and/or /usr/X11/bin.

Set up XQuartz

  1. Launch XQuartz. Under the XQuartz menu, select Preferences
  2. Go to the security tab and ensure "Allow connections from network clients" is checked.
  3. Restart XQuartz.

Every time you want to run an X11 program

  1. Make sure X11 is accepting connections from your host with xhost +$(hostname).local (if your Mac's name is So-and-So's Computer, you will need to do some fancy quoting and escaping).
  2. Set your DISPLAY environment variable to :0, eg export DISPLAY=:0.
  3. Test it from your Mac with xeyes.
  4. It works? Cool, one step fancier, from a container: docker run --rm -e DISPLAY=host.docker.internal:0 -it some-container-name xeyes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment