Skip to content

Instantly share code, notes, and snippets.

@devnoname120
Last active April 30, 2025 11:07
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:

brew install --cask --no-quarantine xquartz

And then configure it

defaults write org.xquartz.X11 nolisten_tcp -bool false
defaults write org.xquartz.X11 no_auth -bool false
defaults write org.xquartz.X11 enable_iglx -bool true

mkdir -p ~/.xinitrc.d

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

xhost +127.0.0.1
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)

libGL error with a driver load failure

If you have the following error: libGL error: failed to load driver: swrast then you can attempt to add this argument to your docker run command:

-e LIBGL_ALWAYS_INDIRECT=1
@HerrMuellerluedenscheid

Thanks for the easy to follow instruction. I'm on OSX 15.3 (24D60) M1 and I'm still getting this error: Error: Can't open display: docker.for.mac.host.internal:0 :( Suggestions what needs to be updated?

@devnoname120
Copy link
Author

@HerrMuellerluedenscheid Can you try this instead?

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

@CarstenOSP
Copy link

This saved me when my Windows machine broke and I switched to Mac. Thank you so much!

@SGeetansh
Copy link

SGeetansh commented Apr 30, 2025

Thanks for making the simplest guide out there on this!

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