GUI container on the Docker. Launch a container on docker in GUI mode. Run any GUI software on the container.

Sonam Thakur
4 min readSep 14, 2023

--

Running a Docker container in GUI (Graphical User Interface) mode can be useful for certain applications that require a graphical interface, such as running GUI software or testing GUI components. Here are the steps to launch a Docker container with GUI support:

Prerequisites:

  1. A Linux system with Docker installed.
  2. An X server running on your host system. Most Linux systems have this by default.

Understanding GUI Containers:

Running GUI applications in a Docker container requires some additional configuration because GUI applications typically require access to graphical resources and a display server. By default, Docker containers do not have access to these resources. However, with a few adjustments, we can enable GUI applications to run smoothly within Docker containers.

Setting up the Docker Environment for GUI Containers: To set up the Docker environment for running GUI containers, follow these steps:

  1. Install Docker: Ensure that Docker is installed on your system. Docker provides installation guides for various operating systems on their website.
  2. X11 Socket Sharing: GUI applications use the X Window System to display graphical interfaces. To enable GUI applications within Docker to use the host’s display server, we need to share the X11 socket between the host and the container. This can be achieved by using the following command when running the container:
docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix <image_name>

This command shares the X11 socket and forwards the display to the container.

3. Granting Access to Graphics: Depending on the system, you might need to grant access to the graphics device. On Linux systems, you can grant access using the following command:

xhost +local:docker

Running GUI Software in the Container:

Once the Docker environment is set up for GUI containers, you can run any GUI software as you would on a regular desktop environment. For example, to run a simple GUI text editor like gedit, you can use the following command:

docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix ubuntu gedit

This command launches an Ubuntu container, runs the gedit text editor, and allows it to display the GUI on the host’s desktop environment.

Benefits of GUI Containers: Running GUI containers offers several benefits:

  1. Isolation: GUI containers provide the same level of isolation as traditional Docker containers, encapsulating the GUI application and its dependencies. This isolation ensures that the application behaves consistently across different environments.
  2. Reproducibility: GUI containers simplify the process of reproducing a specific GUI application’s environment. Developers can share the container image with colleagues, ensuring everyone uses the same software and dependencies.
  3. Portability: GUI containers can be run on any system that supports Docker, regardless of the underlying operating system or hardware. This portability makes it easy to move GUI applications between development, testing, and production environments.
  4. Security: GUI containers add an extra layer of security, as the application is sandboxed within the container and separated from the host system. This reduces the risk of potential security breaches.

Step 1: Create a Dockerfile First, create a Dockerfile that specifies the base image and installs the necessary packages for the GUI application you want to run. Here’s a simple example using Ubuntu as the base image:

DockerfileCopy code
# Use an official Ubuntu as the base image
FROM ubuntu:latest
# Install required packages for the GUI application
RUN apt-get update && apt-get install -y \
x11-apps \
x11-utils \
x11-xserver-utils \
xdg-utils \
xterm \
&& rm -rf /var/lib/apt/lists/*

This Dockerfile installs some basic X11 utilities, which are essential for running GUI applications.

Step 2: Build the Docker Image Navigate to the directory containing your Dockerfile and run the following command to build the Docker image:

bashCopy code
docker build -t gui-container .

Replace “gui-container” with the desired name for your Docker image.

Step 3: Run the Docker Container with GUI Support To run a Docker container with GUI support, you need to share the X server socket with the container and set the DISPLAY environment variable inside the container to point to your host’s X server. Run the following command:

bashCopy code
docker run -it --rm \
--net=host \ # Allows the container to use the host's network stack
--env="DISPLAY" \
--volume="$HOME/.Xauthority:/root/.Xauthority:rw" \ # Share X server credentials
gui-container

This command does the following:

  • -it: Runs the container in interactive mode.
  • --rm: Removes the container when it exits.
  • --net=host: Allows the container to use the host's network stack, which is required for X11 communication.
  • --env="DISPLAY": Sets the DISPLAY environment variable inside the container.
  • --volume="$HOME/.Xauthority:/root/.Xauthority:rw": Shares X server credentials with the container. Replace $HOME with your home directory if it's located elsewhere.

Step 4: Test GUI Applications Once the container is running, you can install and run GUI applications as needed. For example, you can install the graphical calculator xcalc:

bashCopy code
apt-get install -y xcalc
xcalc

The xcalc GUI application should now appear on your host's screen.

Please note that running GUI applications inside a Docker container can be complex and may not work for all applications due to compatibility and security issues. Additionally, this setup assumes that you trust the containerized application as it will run with the same privileges as your user on the host system.

THANKYOU!

--

--