How to launch any GUI app inside docker container ?

Ankit Shukla
4 min readJul 31, 2023

--

Greetings and best wishes for an insightful read😇 ,

Hope this blog finds you great and healthy !!😊

Lets begin with brief introduction about me…..

Hi, This is Ankit Shukla and I am working as Automation Engineer in Regulatory Reporting Domain. In this article we will discuss about How we can launch any GUI application inside container?

Before going deep dive into this …few concepts are useful when we perform any GUI operation on container.

X11 Server :

The X11 server, also known as the X Window System or X.Org Server, is a fundamental component of graphical user interfaces (GUIs) in Unix, Linux, and other Unix-like operating systems. It is responsible for managing and displaying graphical elements on the screen and handling user input such as mouse clicks and keyboard events. The X11 server works with window managers, applications, and other components to create a cohesive desktop environment.

Key Concepts of X11 Server:

  1. Client-Server Architecture: The X11 system follows a client-server architecture. The X server is the central component responsible for managing the display, and it interacts with various X clients (applications) that request services such as drawing windows and handling input events.
  2. Network Transparency: X11 provides network transparency, allowing clients to run on different machines from the X server. Clients can display their graphical interface on a remote machine’s screen as long as the X server is configured to allow network connections.
  3. Window Management: The X11 server handles window management tasks, such as window placement, resizing, and handling window events (e.g., close, minimize). The window manager is responsible for controlling window appearance and behavior.
  4. Rendering and Graphics: The X server handles graphics rendering and compositing. It works with graphics hardware or graphics drivers to draw images and user interface elements on the screen.
  5. Input Handling: The X server manages user input, including keyboard events and mouse clicks. It distributes input events to the appropriate X clients based on the position of the cursor.
  6. Display Protocol: X11 uses a protocol to communicate between the X server and X clients. The protocol defines how clients and the server exchange information about graphics, events, and input.
  7. Display and Screen Terminology: In the X11 system, a “display” refers to an instance of the X server. Each display can manage one or more “screens,” which represent the physical screens connected to the computer.
  8. X Resources: X11 clients can access and modify X resources, which are configuration settings for the X server and applications. X resources allow users to customize the appearance and behavior of applications.
  9. Extensions: X11 supports extensions that provide additional functionality beyond the core protocol. Some extensions enable advanced graphics capabilities, like 3D rendering, while others facilitate modern features like window compositing and desktop effects.

Launching a GUI application inside a container involves a few steps. Here, I’ll provide a general outline of the process. Keep in mind that the exact commands and steps might vary depending on the container runtime you are using (e.g., Docker, Podman) and the GUI application you want to run. For this example, I’ll use Docker as the container runtime and a simple GUI application like “xeyes.”

Step 1: Install Dependencies

Make sure you have Docker (or any other container runtime) installed on your system. Additionally, ensure that an X11 server is installed and running on your host machine. The X11 server is necessary to display GUI applications from the container on your host.

Step 2: Create the Dockerfile

# Use an Ubuntu base image
FROM ubuntu:latest

# Install necessary dependencies for the GUI application
RUN apt-get update && apt-get install -y x11-apps

# Set the display environment variable to point to the X11 server on the host
ENV DISPLAY=host.docker.internal:0

# Run the GUI application (xeyes in this case) when the container starts
CMD ["xeyes"]

Step 3: Build the Docker Image

Navigate to the directory containing the Dockerfile and build the Docker image using the docker build command:

docker build -t my_gui_app .

This command will create a Docker image named my_gui_app based on the instructions in the Dockerfile.

Step 4: Run the Container

Now, you can run the container based on the image you just built, using the docker run command:

docker run --rm -it --env="DISPLAY" --volume="/tmp/.X11-unix:/tmp/.X11-unix" my_gui_app

Let’s break down the options used in the docker run command:

  • --rm: Automatically remove the container when it exits.
  • -it: Allocate a pseudo-TTY and keep STDIN open, allowing you to interact with the GUI application if needed.
  • --env="DISPLAY": Pass the DISPLAY environment variable from the host to the container. This setting is crucial for the GUI application to access the X11 server on the host.
  • --volume="/tmp/.X11-unix:/tmp/.X11-unix": Mount the X11 socket from the host to the container. This allows the GUI application to communicate with the X11 server.

The xeyes GUI application should now open on your host machine.

Thanks for reading !! 😊

--

--

Ankit Shukla
Ankit Shukla

Written by Ankit Shukla

| Software Developer | SDET |

No responses yet