Way to Expose GUI from Docker Container

Docker containers play an important role in modern software development. Having a CLI access to the container is common, but sometimes we need to expose a GUI from the docker container. Here is a tutorial.

Tutorial

Recap: CLI Access

Normally, we use the docker flag -it (interactive & tty) to access a tty of the container, which provides us a CLI access to the container. For example, we can run a Ubuntu 22.04 image with the command

1
2
3
4
5
6
7
8
$ # create a docker container from image `ubuntu:22.04`
$ docker run \
-it \
-name test \
ubuntu:22.04

$ # later on, we can attach to the container
$ docker attach test

GUI Access

We share the X11 socket between the host and the container to enable GUI access. This could be achieved by the following command:

1
2
3
4
5
6
7
$ docker run \
-it \
-e "DISPLAY=$DISPLAY" \
-v /tmp/.X11-unix/:/tmp/.X11-unix \
-v "${XAUTHORITY:-$HOME/.Xauthority}:/root/.Xauthority:rw" \
--name test \
ubuntu:22.04

Then the GUI application can be run in the container, exposing the GUI to the outside. For example, the Kate editor:

(The hostname of the container will be shown in the title bar.)

Some Extensions

VNC Server

Another way to expose the GUI is to use a VNC server. See https://medium.com/@gustav0.lewin/how-to-make-a-docker-container-with-vnc-access-f607958141ae.

References

  1. https://stackoverflow.com/questions/44429394/x11-forwarding-of-a-gui-app-running-in-docker
  2. https://dzone.com/articles/docker-x11-client-via-ssh
  3. https://github.com/sickcodes/Docker-OSX/tree/master