Container focused on game development using Raylib. The goal is to provide a consistent and isolated development environment, minimizing configuration issues on the host system.
You can see the Container Github Repo here:
Raylib-Container
Are you using macOS? Follow the guide for specific instructions. Go to MacOS Compatibility Guide section below.
Before using the container, you need to configure your host system to allow graphical applications from the container to run and to manage Docker without sudo (recommended).
Allow local Docker containers to connect to your graphics server (X11 or Wayland via XWayland).
# Allow connections from Docker
xhost +local:dockerNote: This command usually needs to be run in each new graphical session or after rebooting the system. You might want to add it to your startup scripts (like
.profile,.xinitrc, etc.).
Adding your user to the docker group with the command below generally removes the need to use sudo to execute docker commands:
sudo usermod -aG docker $USERHowever, it's important to mention that, depending on the specific configuration of your Linux distribution, there might be situations (for example, when trying to interact with the graphical interface or "open the display" from within a container) where using sudo might still be necessary for some tasks, even if the user is a member of the docker group.
Important: After running this command, you must log out and log back in or reboot the system for the group change to take effect.
If you don't have the Docker image locally yet, or if you want to update it (e.g., after modifying the Dockerfile or to get a new Raylib version), use the build command:
# Navigate to the directory containing the 'Dockerfile'
# cd /path/to/project
docker build -t raylib_container .Up-to-date Raylib: When building the image, Raylib is cloned and compiled directly from the official GitHub repository, ensuring you always have the latest version available.
To start an interactive container with access to your display and your project code mounted:
Option 1: With Graphics Hardware Acceleration (Recommended)
This option attempts to use your host system's GPU for better performance.
docker run -it --rm \
-e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v ./user_code:/app/user_code \
--device /dev/dri:/dev/dri \
raylib_containerOption 2: With Software Rendering (Fallback)
Use this option if hardware acceleration doesn't work (you might see errors related to dri, glx, mesa, or graphics drivers). Performance will be lower.
docker run -it --rm \
-e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v ./user_code:/app/user_code \
-e LIBGL_ALWAYS_SOFTWARE=1 \
raylib_containerParameter Explanation:
-it: Starts the container in interactive mode with a terminal attached.--rm: Automatically removes the container when it exits.-e DISPLAY=$DISPLAY: Passes the host'sDISPLAYenvironment variable to the container, telling it which screen to use.-v /tmp/.X11-unix:/tmp/.X11-unix: Mounts the host's X11 socket into the container, allowing graphical communication.-v ./user_code:/app/user_code: Mounts a directory nameduser_code(which will be created in the current host directory if it doesn't exist) into the container at/app/user_code. This is where you should place your game's source code. Files are synchronized between the host and the container.--device /dev/dri:/dev/dri(Option 1): Maps the host's Direct Rendering Infrastructure devices into the container, allowing GPU access.-e LIBGL_ALWAYS_SOFTWARE=1(Option 2): Forces the Mesa graphics library to use software (CPU) rendering.raylib_container: The name of the Docker image to use.
Once inside the container's terminal (after running docker run), test if the graphics connection is working:
xeyesA window with eyes that follow the mouse should appear. You can close it (usually by right-clicking or closing the window normally).
Your source code should be placed in the user_code folder on your host system, which is mapped to /app/user_code inside the container. The container already has GCC and Raylib libraries installed.
Compilation Example:
Navigate to your code directory inside the container (cd /app/user_code if needed) and compile your C file:
# Example for a file named 'my_game.c'
gcc my_game.c -o my_game -lraylib -lGL -lm -lpthread -ldl -lrt -lX11Note: The linked libraries (
-l...) might vary slightly depending on the Raylib features you use.
Running the Compiled Program:
./my_gameIf you want to undo the changes made in the prerequisites:
-
Revoke Display Access:
xhost -local:docker
-
Remove User from Docker Group:
sudo gpasswd -d $USER dockerRemember to log out/log in or reboot afterwards.
-
Docker Socket Permissions:
- Do not change the Docker socket (
/var/run/docker.sock) permissions to666! This is a severe security risk. The correct and secure way to avoidsudois to add your user to thedockergroup (Prerequisite 2). - If you accidentally changed the permissions, the default is usually
660with ownerrootand groupdocker. You might try to restore it with:# Only if you incorrectly changed permissions before! sudo chmod 660 /var/run/docker.sock sudo chown root:docker /var/run/docker.sock - But the best approach is never to use
chmod 666on the socket.
- Do not change the Docker socket (
-
Error
docker: Cannot connect to the Docker daemon... Permission denied.: You likely haven't added your user to thedockergroup or didn't log out/log in after adding them. Try usingsudo docker ...or follow Prerequisite 2. -
Error
docker: invalid reference format: Check if the image name (raylib_container) is spelled correctly in thedocker runcommand and that the image actually exists (check withdocker images). -
Window doesn't appear / Error
cannot open display: :0: Check that you ranxhost +local:dockerin the current host graphical session. Also verify theDISPLAYvariable is being passed correctly (-e DISPLAY=$DISPLAY). -
Error
MESA: error: Failed to query drm device.,glx: failed to create dri3 screen,failed to load driver: iris/radeon/etc.: Hardware acceleration isn't working. Ensure the--device /dev/dri:/dev/driflag was used. If it still fails, try Option 2 ofdocker run(software rendering with-e LIBGL_ALWAYS_SOFTWARE=1). -
Need to Rebuild Image: If the image seems outdated or corrupted, rebuild it:
docker build -t raylib_container .
or if you prefer:
docker build --pull --rm -f 'Dockerfile' -t 'raylib_container:test' '.'To run graphical applications within a Docker container on macOS, some additional steps are required to enable the container to access the macOS display. Here's how to do it:
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 XQuartz from the official website: https://www.xquartz.org/
- Install XQuartz by following the instructions provided in the downloaded package.
- Important: After installation, you must log out and log back in to your macOS account for the changes to take effect.
After installing and logging back in, configure XQuartz to allow connections from network clients:
- Open XQuartz.
- Go to XQuartz Preferences (XQuartz → Preferences).
- In the "Security" tab, make sure "Allow connections from network clients" is checked.
Open a terminal and run the following command to allow connections from Docker:
xhost + 127.0.0.1This command allows connections from the local machine, which is necessary for Docker to forward the graphical display.
Now you can run the Docker container with the necessary parameters to forward the display. Here's an example:
docker run -it --rm \
-e DISPLAY=127.0.0.1:0 \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v ./user_code:/app/user_code \
raylib_containerExplanation of Parameters:
-e DISPLAY=127.0.0.1:0: Sets theDISPLAYenvironment variable to the correct address for XQuartz on macOS.-v /tmp/.X11-unix:/tmp/.X11-unix: Mounts the X11 socket directory to allow communication with the X server.-v ./user_code:/app/user_code: Mounts your localuser_codedirectory to the/app/user_codedirectory inside the container. Place your Raylib project files in theuser_codedirectory.raylib_container: The name of the Docker image.
Inside the container, run xeyes to verify that the graphical display is working correctly:
xeyesIf xeyes opens a window and the eyes follow your mouse cursor, the configuration is correct, and you can proceed with developing your Raylib project.
- Ensure that XQuartz is running before you start the Docker container.
- If you encounter any issues, double-check that you have followed all the steps correctly, especially logging out and back in after installing XQuartz and configuring its security settings.
If the problem persists, feel free to open an issue on the project's repository for assistance.
This file is under the license of Eclipse Public License 2.0