Run graphical software inside a Docker container.
Running graphical software inside a Docker container requires setting up a way for the container to interact with the host's display system. This can be achieved by sharing the host's X11 or Wayland display with the container or using protocols like VNC or Xpra. Below are some methods to run graphical applications in Docker containers.
1. Using X11 Forwarding for Linux Hosts
Steps:
Install X11 on the Host Machine: Ensure that your host machine has an X server running (most Linux systems come with X11 pre-installed).
Create a Dockerfile for the GUI Application: Here’s a sample Dockerfile to run graphical applications in a container. It installs a simple GUI app like Firefox.
FROM ubuntu:20.04 # Install necessary packages RUN apt-get update && apt-get install -y \ firefox \ x11-apps \ --no-install-recommends && \ rm -rf /var/lib/apt/lists/* # Set the entry point for running the GUI app CMD ["firefox"]
Run the Container with X11 Forwarding:
You need to share your X11 socket with the Docker container and allow connections from the container to the host’s X server.
xhost +local:root # Allow access to X server docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --rm -it gui-container
-e DISPLAY=$DISPLAY
: Passes the host's display environment variable to the container.-v /tmp/.X11-unix:/tmp/.X11-unix
: Mounts the X11 Unix socket so that the container can access the host's display.xhost +local:root
: Allows Docker to access the host’s X server.
Revoke Access (Optional):
After you're done, you can revoke the container’s access to the X server.
xhost -local:root
2. Running with VNC for Remote or Local Access
Steps:
Install a VNC Server in the Dockerfile:
Here’s a sample Dockerfile that installs a VNC server and a graphical application (e.g., Xfce desktop environment and Firefox).
FROM ubuntu:20.04 # Install necessary packages RUN apt-get update && apt-get install -y \ xfce4 \ firefox \ tigervnc-standalone-server \ --no-install-recommends && \ rm -rf /var/lib/apt/lists/* # Set VNC password RUN mkdir ~/.vnc && echo "password" | vncpasswd -f > ~/.vnc/passwd && chmod 600 ~/.vnc/passwd # Start VNC server CMD ["vncserver", "-fg", ":1"]
Run the Container with Exposed VNC Port:
docker run -p 5901:5901 --rm -it vnc-container
-p 5901:5901
: Exposes port 5901 (the default VNC port) for remote access.
Connect via VNC Client:
Use a VNC client (e.g.,
TigerVNC
orRealVNC
) on your host machine or remotely to connect tolocalhost:5901
.
3. Using Xpra (More Efficient Than VNC)
Xpra allows forwarding of graphical applications from Docker containers with better performance than VNC, particularly for remote connections.
Steps:
Install Xpra and a GUI Application in Dockerfile:
Here’s a sample Dockerfile that installs Xpra, Firefox, and an Xfce environment.
FROM ubuntu:20.04 # Install necessary packages RUN apt-get update && apt-get install -y \ xfce4 \ firefox \ xpra \ --no-install-recommends && \ rm -rf /var/lib/apt/lists/* # Start Xpra server CMD ["xpra", "start", "--bind-tcp=0.0.0.0:14500", "--start-child=firefox", "--exit-with-children"]
Run the Container with Xpra:
docker run -p 14500:14500 --rm -it xpra-container
Connect Using Xpra Client:
Install
Xpra
on your local machine and connect:xpra attach tcp:localhost:14500
This allows you to forward the graphical application from the Docker container to your local machine efficiently.
4. Running with Wayland (For Modern Display Servers)
If your host uses the Wayland display server (more common in modern Linux distributions), you can run graphical apps using Wayland instead of X11.
Steps:
Enable Wayland Support:
Install the necessary dependencies in your Dockerfile, and set up the container to use Wayland.
Pass the Wayland Socket:
Run the Docker container with the following flags to mount the Wayland socket from the host.
docker run -e WAYLAND_DISPLAY=$WAYLAND_DISPLAY -v /run/user/1000/wayland-0:/run/user/1000/wayland-0 --rm -it gui-container
-e WAYLAND_DISPLAY=$WAYLAND_DISPLAY
: Exports the display environment variable for Wayland.-v /run/user/1000/wayland-0:/run/user/1000/wayland-0
: Mounts the Wayland socket to the container.
Summary:
X11 forwarding is the easiest option for running GUI apps in Docker containers on Linux hosts.
VNC allows remote or local GUI access and is ideal for lightweight desktop environments inside a container.
Xpra provides an efficient alternative to VNC with better performance for graphical applications.
Wayland support is useful for modern Linux systems that have shifted away from X11.
These techniques help to run graphical software in containers while maintaining isolation and flexibility.
Subscribe to my newsletter
Read articles from Vaibhav Parekh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vaibhav Parekh
Vaibhav Parekh
I am a Cloud enthusiast . Delivering continuous learnings through blogs . You have any doubts you can contact me directly don't hesitate . We all have a start and initial days of learning !!