How To Launch a Container on Docker in GUI mode?

Rahul Bhardwaj
3 min readMay 16, 2021

--

By-default any container that you run on docker is in CLI mode. It is so because the main use of containers are to deploy apps while running programs in the background. This is how it is used for the best to give an isolated environment with an amazing performance.

What is the need to launch a container in GUI mode?

There are times when you need to evaluate an application and you do not want it to mess with your host operating system. So, launching that application in an isolated environment will be a good idea using a docker container.

Overview

Unlike the regular operating systems docker containers do not have any GUI framework by-default. The framework most commonly used is X Window System (X11 or simply X). An X-server provides the GUI environment for the drawing and moving windows on the screen and helping in interacting with mouse and keyboard.

So what we can do to provide our container this server is we can share this framework with our currently running container.

Making the Image with Dockerfile

This will be a simple task to do. The method described here will on Operating Systems like Ubuntu, Fedora, CentOS and Redhat. We’ll be using Rhel 8 as our base operating system and CentOS as container base image.

The application we’ll be going to launch will be firefox. You can you gedit if it takes longer on you VM to download.

As simple as that.

Building the image

Command ran successfully. The name of image was gpp:v2.

Launching the Container

Now this is the tricky part where all the main task lies.

We will run our container with some options that will provide the requirements for running our image in an GUI environment.

We will pass the DISPLAY environment variable of our host OS to the container using

-e DISPLAY

Next, we’ll attach the volume of the X server which is in /root/.Xauthority. The file in container on which we will mount it should have the same name such that it will feel like its working on the same place using the same user (root in our case) hence will cause no problem allowing our container to use host OS’ X server.

-v /root/.Xauthority:/root/.Xauthority

Now, we will specify the network we’re going to use in our container. For that, we’ll be using host network as it will provide all the network facilities of the host OS. It be having access to all the networking abilities as well as the socket programs allowing it to run it in a way as it is being run on the host OS!

--net=host

So, our command will look something like this:

docker run -e DISPLAY -v /home/root/.Xauthority:/root/.Xauthority   --net=host <image_name>

Note: If you’re using a user other than root create directory according to that and create the user in the docker image.

And its done!

Note: This method of launching might give a bad performance for the applications requiring graphics card as the method we used would not use any GPU power of the base OS but that surely can be done with some other ways.

Thank you for reading.

--

--