Skip to content

Instantly share code, notes, and snippets.

@devnoname120
Last active December 4, 2024 07:48
Show Gist options
  • Save devnoname120/ce02ef43da968e15340427c2f1c286a7 to your computer and use it in GitHub Desktop.
Save devnoname120/ce02ef43da968e15340427c2f1c286a7 to your computer and use it in GitHub Desktop.
Xorg/X11 forwarding on macOS and docker

Xorg/X11 forwarding on macOS and docker

In order to run Xorg apps on macOS you will need to install xquartz and configure it:

brew install --cask --no-quarantine xquartz
defaults write org.xquartz.X11 nolisten_tcp -bool false
defaults write org.xquartz.X11 no_auth -bool false

mkdir -p ~/.xinitrc.d

cat << 'EOF' > ~/.xinitrc.d/xhost-config.sh
#!/bin/sh

xhost +localhost
xhost +\$(hostname)
EOF

chmod +x ~/.xinitrc.d/xhost-config.sh

open -a XQuartz

No need to restart anything or log in/out.

You can then run Xorg apps easily:

docker run -e DISPLAY=docker.for.mac.host.internal:0 petedavidson887/xclock:0.0.1

Note that you will still need to explicitly provide the DISPLAY env variable every time you run a Docker container. As far as I know there is no way to set a default env variable for all containers — at least not on Docker for macOS.

Troubleshooting

Windows have a black background instead of white when they are focused

Run the following, then restart XQuartz:

defaults write org.xquartz.X11 enable_render_extension -bool false

If the misbehaving app is a Java app you can try adding the following option to your docker run command:

-e JAVA_TOOL_OPTIONS='-Dsun.java2d.xrender=false -Dsun.java2d.pmoffscreen=false -Dremote.x11.workaround=false -Dawt.useSystemAAFontSettings=lcd -Dsun.java2d.renderer=sun.java2d.marlin.MarlinRenderingEngine -Djava2d.font.loadFontConf=true'

(source)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment