Running GUI Applications inside Docker Containers

Ashish Dwivedi
4 min readMay 29, 2021

In this blog we will see how to use jupyter notebook in docker container.

While the IT world is embracing Containers Technology primarily for Enterprise Server Applications, There is also a huge scope of Docker Containers impacting the Desktop and Development Environment. We leverage this heavily at Admatic, where we containerise every application and service we use — further including the GUI applications and tools that we use for Day to day Development

If we want to switch to Deep Learning Dev environment — its 1 Click, If we want Android Dev Env — its just another 1 sec away, Need to integrate Computer vision Algorithms

Thanks to Docker Containers that is revolutionising the current DevOps landscape and we plan to revolutionise the current development environment landscape with the same.

Here will share you how to Containerise a GUI app in Docker

There can be two types of applications (usually services) that you can containerise,

  • Applications that run as a Background Service (like a Database, WebServer, etc)
  • GUI Applications that (obviously!) run in the foreground

The second option is what we will see now,

For a GUI Application to run, we need to have a XServer which is available as part of every Linux Desktop Environment, But within a Container we don’t have any XServer — so we will

  • share the Host’s XServer with the Container by creating a volume
--volume="$HOME/.Xauthority:/root/.Xauthority:rw"
  • share the Host’s DISPLAY environment variable to the Container
--env="DISPLAY"
  • run container with host network driver with
--net=host  

Before doing all stuff we have to do something on our docker host or we can say in local computer. So whenever we use any gui application like firefox or gedit as root user most of the time they wont work. Sometimes this can create a problem while running a gui application in docker container.

So to solve this issue we have to run a command in our Base Os where docker engine so we can use firefox or gedit like applications. Use this command as localhost user as root user.

xhost +

This command will give us power to connect from any host and now we have to export Xauth file.For further process.

export XAUTHORITY=/home/user/.Xauthority

Now create a file with name Dockerfile .

vim Dockerfile

Dockerfile it is predefine name inside this docker file we will write commands to install and run and will make image of it so we dont have to mannualy download the prerequisite for launching docker container to use jupyter notebook.

Now with this Dockerfile we will create an image.

docker build -t gui .

gui is name of image and “ . “ will fetch file present in directory with name Dockerfile and will create image accordingly.

To launch our docker container we will use this image But here we have to share the Host’s XServer with the Container by creating a volume , share the Host’s DISPLAY environment variable to the Contain, run container with host network driver.

docker run -it --name jupyter --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" gui

By running this command we will land in our docker container and we installed everything while creating image so now we have to directly run jupyter note book command and our notebook will open.

jypyter notebook --allow-root

Happy learning……..

--

--