Run any GUI software on the container

Himansh Sharma
2 min readSep 13, 2023

--

Running GUI applications within a Docker container can indeed be more intricate than running command-line applications, as it requires configuring X11 forwarding and ensuring access to the host’s graphical environment. Here’s a step-by-step guide to accomplish this:

Prerequisites:

  • Ensure you have Docker installed on your Linux-based host system.
  • Install an X11 server on your host machine. Use Xming for Windows, XQuartz for macOS, or confirm that X11 is already installed on Linux.

Step 1: Enable X11 Forwarding on the Host: Before launching your Docker container, enable X11 forwarding on your host by running this command:

xhost +

Step 2: Create a Dockerfile: Create a Dockerfile to define your container:

FROM ubuntu:20.04
# Install necessary packages
RUN apt-get update && \
apt-get install -y x11-apps
# Set the display environment variable
ENV DISPLAY=:0
# Run a simple GUI application (xeyes as an example)
CMD ["xeyes"]

In this Dockerfile, we use the “xeyes” command as a simple example of a GUI application.

Step 3: Build the Docker Image: Navigate to the directory containing your Dockerfile and build the Docker image using this command:

docker build -t gui-container1 .

Step 4: Run the Docker Container: Now, run the Docker container with X11 forwarding enabled and the necessary environment variables:

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

Explanation of the command:

  • -it: Runs the container in interactive mode.
  • --rm: Removes the container when it exits.
  • --name my-gui-container1: Assigns a name to the container.
  • -e DISPLAY=$DISPLAY: Sets the DISPLAY environment variable in the container to the host's display.
  • -v /tmp/.X11-unix:/tmp/.X11-unix: Mounts the X11 Unix socket from the host to the container.

Once the container is running, it will execute the GUI application specified in the Dockerfile (in this case, “xeyes”). You can replace “xeyes” with any other GUI application you want to run.

You should now see the GUI application’s window open on your local machine, seamlessly integrated with your Docker container.

Thanks for Reading!!

--

--