Running Desktop Applications with Docker (With UI Support)

Docker is great for isolating environments and applications, but when it comes to running desktop GUI apps, things can get tricky. Thankfully, with the right configuration, it’s entirely possible to run Linux GUI applications inside a Docker container and display them on your host desktop.

Prerequisites

Before we start, make sure you have the following:

  • A Linux desktop environment
  • Docker installed and running
  • X11 server (usually installed by default on Linux desktops)

This guide is Linux-focused. Running GUI apps in Docker on Windows/macOS requires additional setup (like VcXsrv or XQuartz).

Step 1: Allow Docker to Access the X Server

By default, Docker containers can’t access the X server. Run this command to allow access from Docker:

xhost +local:docker

This makes your X server accessible to local Docker containers.

Step 2: Create a Dockerfile

Here’s an example Dockerfile to run a simple GUI app like xeyes:

FROM ubuntu:22.04

RUN apt-get update && apt-get install -y \
x11-apps \
&& rm -rf /var/lib/apt/lists/*

CMD ["xeyes"]

Save this as Dockerfile.

Step 3: Build the Docker Image

docker build -t gui-xeyes .

Step 4: Run the Docker Container with Display Access

Use the following command to run the GUI app:

docker run -it \
--rm \
-e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
gui-xeyes

Explanation:

  • -e DISPLAY=$DISPLAY: Passes your display environment variable to the container.
  • -v /tmp/.X11-unix:/tmp/.X11-unix: Shares the X11 Unix socket.
  • --rm: Automatically cleans up the container after it exits.

You should now see the xeyes window pop up on your desktop.

Example: Running a Full-Fledged App (e.g., Firefox)

You can modify the Dockerfile to install more complex apps like Firefox:

FROM ubuntu:22.04

RUN apt-get update && apt-get install -y \
firefox \
&& rm -rf /var/lib/apt/lists/*

CMD ["firefox"]

Then build and run:

docker build -t gui-firefox .
docker run -it \
--rm \
-e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
gui-firefox

Security Consideration

Running GUI apps via Docker with X11 socket sharing isn’t the most secure approach, as it gives the container access to your display server. Alternatives like Wayland, Xpra, or VNC may offer more isolation.

Docker isn’t just for servers and backend tools you can also use it to sandbox GUI apps. Whether you’re testing, packaging, or isolating apps, running desktop applications in Docker can be a powerful part of your toolkit.