Running GUI applications on docker in linux

Running GUI applications inside Docker containers requires connecting the container's display output to the host system's display server. By default, Docker containers cannot access the host's display, making it impossible to run graphical applications. This article demonstrates how to configure Docker containers to run GUI applications like Firefox by forwarding X11 sockets and managing display permissions.

X11 Display Forwarding

The X11 display server handles graphical output on Linux systems. To run GUI applications in Docker, we need to:

  • Forward the X11 socket to the container

  • Set the DISPLAY environment variable

  • Configure X server authentication permissions

Creating the Docker Image

First, create a Dockerfile that installs the necessary GUI application and X11 authentication tools:

# Set ubuntu as base image
FROM ubuntu

# Install dependencies
RUN apt-get -y update
RUN apt-get -y install xauth
RUN apt-get -y install firefox

# Expose a port number
EXPOSE 8887

# Run firefox
CMD /usr/bin/firefox

The xauth package provides authentication mechanisms for X server access. This allows Docker containers to connect to the host's display server using security tokens called magic cookies.

Step-by-Step Execution

Step 1: Get X11 Authentication Cookie

On your host machine, retrieve the current X server authentication cookie:

xauth list

This outputs something like:

username/unix:0 MIT-MAGIC-COOKIE-1 8fe8efc75454dbf178bbe00442689406

Copy this entire line for use in the container.

Step 2: Run Container with Display Forwarding

Build and run the container with the necessary X11 forwarding options:

sudo docker run -ti --net=host -e DISPLAY -v /tmp/.X11-unix <IMAGE_NAME> bash

The parameters work as follows:

  • --net=host - Uses host network stack

  • -e DISPLAY - Forwards the DISPLAY environment variable

  • -v /tmp/.X11-unix - Mounts the X11 socket directory

Step 3: Configure Authentication Inside Container

Inside the container bash session, add the authentication cookie:

xauth add username/unix:0 MIT-MAGIC-COOKIE-1 8fe8efc75454dbf178bbe00442689406

Verify the connection is established:

xauth list

Step 4: Launch GUI Application

Run the Firefox application:

/usr/bin/firefox

The Firefox window should now appear on your host desktop, running from inside the Docker container.

Troubleshooting

If you encounter the error couldn't connect to display ":1", modify the display number in the authentication token. Replace :0 with :1 in the xauth command:

xauth add username/unix:1 MIT-MAGIC-COOKIE-1 8fe8efc75454dbf178bbe00442689406

Security Considerations

This method grants the container access to your X11 display server, which has security implications. For production environments, consider using alternative approaches like:

  • X11 forwarding through SSH

  • VNC-based solutions

  • Wayland protocol forwarding

Conclusion

Running GUI applications in Docker containers requires forwarding the X11 display server and configuring proper authentication. By mounting the X11 socket, setting environment variables, and managing magic cookies, you can successfully run graphical applications inside containers while displaying them on the host system.

Updated on: 2026-03-17T09:01:38+05:30

657 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements