Overview
Running graphical Docker containers on macOS requires additional setup because macOS doesn’t natively support X11. You’ll need to install and configure XQuartz, an X Window System implementation for macOS, to forward the graphical display from the Docker container to your macOS environment.Prerequisites
This guide assumes you have Docker Desktop for Mac already installed. If not, download it from the official Docker website.
Setup Steps
Install XQuartz
XQuartz is an X Window System implementation for macOS. It is required to forward the graphical display from the Docker container to your macOS environment.Download and Install:
- Download XQuartz from the official website: https://www.xquartz.org/
- Install XQuartz by following the instructions provided in the downloaded package
- Double-click the
.dmgfile and drag XQuartz to your Applications folder
Configure XQuartz Security Settings
After installing and logging back in, configure XQuartz to allow connections from network clients:After changing this setting, quit and restart XQuartz for the changes to take effect.
- Open XQuartz from your Applications folder
- Go to XQuartz Preferences (XQuartz → Preferences in the menu bar)
- Navigate to the Security tab
- Check the box for “Allow connections from network clients”
This setting is crucial for Docker containers to connect to XQuartz. Without it, the display forwarding will fail.
Start XQuartz and Enable Display Access
Open a terminal and run the following command to allow connections from the local machine:This command allows connections from localhost (127.0.0.1), which is necessary for Docker to forward the graphical display.To verify XQuartz is running:You should see output like
/private/tmp/com.apple.launchd.xxx/org.xquartz:0Run the Docker Container
Now you can run the Docker container with the necessary parameters to forward the display:
Notice the key difference from Linux: on macOS, we use
DISPLAY=127.0.0.1:0 instead of DISPLAY=$DISPLAY.Understanding macOS Display Forwarding
DISPLAY Environment Variable
The key difference between Linux and macOS is how theDISPLAY variable is set:
Why 127.0.0.1:0?
127.0.0.1: Localhost IP address:0: Display number 0 (the first display)
127.0.0.1:0 for X11 connections, so we point the container to this address.
Running the Container
Here’s the complete command with explanations:-it: Starts the container in interactive mode with a terminal attached--rm: Automatically removes the container when it exits-e DISPLAY=127.0.0.1:0: Sets theDISPLAYenvironment variable to the XQuartz address-v /tmp/.X11-unix:/tmp/.X11-unix: Mounts the X11 socket directory for communication-v ./user_code:/app/user_code: Mounts your localuser_codedirectory to/app/user_codeinside the containerraylib_container: The name of the Docker image
Place your Raylib project files in the
user_code directory on your Mac, and they’ll be accessible at /app/user_code inside the container.Hardware Acceleration on macOS
Unlike Linux, you cannot pass GPU devices to containers on macOS. The--device /dev/dri:/dev/dri flag used on Linux won’t work. All rendering will be done in software mode.
For better performance:
- Keep your graphics workload minimal during development
- Use simpler assets and lower resolutions for testing
- Consider testing on a Linux machine for performance-critical work
Common Workflows
Starting a Development Session
Creating a Launch Script
Create a script to automate the process:Troubleshooting
XQuartz window doesn't appear
XQuartz window doesn't appear
Symptoms: Container runs but no graphics window appearsSolutions:
- Verify XQuartz is running (you should see it in the menu bar)
- Check that you ran
xhost + 127.0.0.1 - Ensure “Allow connections from network clients” is enabled in XQuartz Preferences → Security
- Try restarting XQuartz
- Verify the DISPLAY variable:
echo $DISPLAYinside the container should show127.0.0.1:0
Connection refused error
Connection refused error
Full error:
Error: Can't open display: 127.0.0.1:0Solutions:- Ensure XQuartz is running before starting the container
- Check that you’ve logged out and back in after installing XQuartz
- Verify the security setting is enabled: XQuartz Preferences → Security → “Allow connections from network clients”
- Run
xhost + 127.0.0.1again
xeyes or graphics apps are slow
xeyes or graphics apps are slow
Cause: Software rendering is slower than hardware accelerationSolutions:
- This is expected behavior on macOS
- Reduce window size and complexity during development
- Use Docker Desktop’s resource settings to allocate more CPU/memory
- For performance testing, use a Linux machine or native macOS build
Permission denied when mounting volumes
Permission denied when mounting volumes
Full error:
Error response from daemon: error while creating mount source pathSolutions:- Ensure the
user_codedirectory exists:mkdir -p user_code - Check Docker Desktop → Preferences → Resources → File Sharing
- Add the directory containing your project to the allowed paths
- Use absolute paths:
-v /Users/yourname/project/user_code:/app/user_code
Additional Notes
If you encounter issues not covered here, double-check that you’ve:
- Logged out and back in after installing XQuartz
- Enabled “Allow connections from network clients” in XQuartz security settings
- Started XQuartz before running the container
- Run the
xhost + 127.0.0.1command
Next Steps
Build the Image
Learn how to build the Raylib Container image
Quick Start
Create your first Raylib project