Science and Nature

Study the option to Speed GUI Applications in a Docker Container

James Walker

| 4 min read

Docker’s generally feeble to containerise background applications and CLI applications. It’s likely you’ll per chance presumably moreover use it to hump graphical applications despite the indisputable fact that! It’s likely you’ll per chance presumably both use an existing X Server, where the host machine is already working a graphical environment, otherwise you would possibly hump a VNC server contained within the container.

First it’s necessary to realise what Docker truly does. A Docker “container” is a invent of encapsulation which appears to be like superficially equal to a digital machine. Unlike a digital machine, containers portion the identical Linux kernel as their host machine.

The next element is the X Window Procedure. X Servers corresponding to Xorg present the basic graphical capabilities of Unix methods. GUI applications can’t render without an X Server readily obtainable. (Alternative windowing methods, corresponding to Wayland, are readily obtainable – we’re focusing on X listed here.)

Attempting to hump an X Server in Docker is theoretically that you just would possibly imagine but now not regularly feeble. You’d must hump Docker in privileged mode (--privileged) so it might per chance perchance per chance access your host’s hardware. Initiating the server would strive to impart your video gadgets, generally resulting in loss of video output as your host’s normal X server gets its gadgets yanked away.

A larger skill is to mount your host’s X Server socket into the Docker container. This permits your container to use the X Server you’ll need already bought. GUI applications working within the container would then appear on your existing desktop.

Why Speed GUI Apps in Docker?

Working a GUI program in Docker generally is a vital design whenever you’re evaluating a brand new fragment of instrument. It’s likely you’ll per chance presumably install the instrument in a desirable container, as a replace of having to pollute your host with new applications.

This skill moreover helps you steer particular of any incompatibilities with other applications on your environment. If it is necessary to temporarily hump two variations of a program, you would possibly use Docker to steer particular of having to do away with and reinstall the instrument on your host.

Forwarding An X Socket to A Docker Container

Offering a Docker container with access to your host’s X socket is a straightforward plan. The X socket is also chanced on in /tmp/.X11-unix on your host. The contents of this listing must be mounted into a Docker quantity assigned to the container. You’ll must use the host networking mode for this to work.

It be necessary to moreover present the container with a DISPLAY environment variable. This instructs X purchasers – your graphical applications – which X server to hook up with. Web impart online DISPLAY within the container to the price of $DISPLAY on your host.

It’s likely you’ll per chance presumably encapsulate all this configuration in a single docker-luxuriate in.yml file:

version: "3"

products and companies:
    train: my-app:most original
    glean: .
      - /tmp/.X11-unix:/tmp/.X11-unix
    network_mode: host

Next, it is necessary to invent a Dockerfile for your software. Right here’s an instance that runs the Firefox web browser:

FROM ubuntu:recentRUN applicable-glean update && applicable-glean install -y firefox
CMD ["/usr/bin/firefox"]

Now glean and hump the train:

docker-luxuriate in earndocker-luxuriate in up

A brand new Firefox window must appear on your desktop! The Firefox instance will hump contained within the container, independently of every other commence Firefox windows. The container will portion your host’s X socket, so the containerised Firefox quiet reveals up on your desktop.

This skill must absolute best be feeble whenever you have faith your Docker container. Exposing the host’s level to server is a security likelihood must you’re now not exclusively obvious what lies contained within the container.

Handling X Authentication

It’s likely you’ll per chance presumably must authenticate the container to access the X Server. First glean an X authentication token out of your host machine. Speed xauth list and tell down regarded as one of many listed cookies. You’ll must reproduction your entire line.

At some stage within the Docker container, install the xauth kit. Then hump xauth add, passing the token you copied within the outdated step.

applicable install -y xauth
xauth add 

Your container must now successfully authenticate to the X Server.

One other Blueprint – Working a VNC Server

In case you’re unable to use X socket forwarding, you would possibly setup a VNC server interior your container. This skill lets you gaze graphical apps within the container by connecting from a VNC client working on the host.

Add the VNC server instrument to your container:

FROM ubuntu:recentRUN applicable-glean update && applicable-glean install -y firefox x11vnc xvfb
RUN echo "exec firefox" > ~/.xinitrc && chmod +x ~/.xinitrc
CMD ["v11vnc", "-create", "-forever"]

When you hump this container, a VNC server will be created automatically. It be necessary to bind a bunch port to the container’s port 5900 – here’s the port the VNC server will be uncovered on.

Firefox gets launched on startup as it’s added to .xinitrc. This file will be accomplished when the VNC server launches and initialises a brand new level to.

To hook up with the server, you’ll desire a VNC client on your host. Glean the IP take care of of your container by working docker ps, noting down the container ID and passing it to docker gaze . You’ll ranking the IP take care of come the bottom of the output, contained within the Network node.

Use the container’s IP take care of in conjunction with your VNC client. Join on port 5900 without authentication. You would possibly per chance now be in a position to have interaction with the graphical applications working within your Docker container.


It’s likely you’ll per chance have gotten the assorted of two approaches when working graphical applications within a containerised environment. For general use, sharing the host’s X socket generally affords the absolute best resolution. It’s likely you’ll per chance presumably moreover make a selection to hump a VNC server contained within the container. This skill is also safer whenever you didn’t invent the container train.

Containerised graphical apps are vital whenever you’re evaluating instrument or must hump two variations of a kit. It’s likely you’ll per chance presumably use applications on your existing desktop without desiring to touch your host’s configuration.

Related Articles

Back to top button