Task 02 👨🏻‍💻
Task Description đź“„
đź“Ś GUI container on the Docker
đź”… Launch a container on docker in GUI mode
đź”… Run any GUI software on the container

GUI container on the Docker

dharshan r
4 min readAug 22, 2021

Hi, guys in this blog we are going to learn how to run GUI like running GEDIT(editor), Firefox, Jupyter on top of a Docker container. Here we are creating our own image and running GEDIT(editor), Firefox, Jupyter.

GUI CONTAINER ON THE DOCKER

Docker is the best tool you can use if you want to develop one single application that can run and work uniformly at any environment, laptop or public cloud instance.

Benefits of Docker

  • Test, Roll Back, and Deploy.
  • Flexibility.
  • Collaboration, Modularity, and Scaling. and many more

After researching about Docker GUI, I concluded that very less used case where docker container is used for GUI purposes.

But if we need to run GUI on docker then we need to understand some of the basics things.

If we want to run UI application on top of the container. Then we need to connect our container to our display of baseOS, as container is minimum and can remove and manage more effectively.

We have to understand there are two types of Applications:-

  1. Applications that are designed to be run in the background.(For ex: WebServer)
  2. The application is designed to be run in Foreground. (For ex: Chrome)

The Foreground application always runs in the foreground always requires XServer.

Let’s dev into practical before that let’s know few things

What is XServer?

An X server most likely refers to the X11 windowing system, which is the GUI that most Unix flavors (including Linux) use. It’s a client/server setup

X is the GUI protocol for Unix/Linux. The X server accepts connections and displays their windows. The clients are actually the programs themselves. These clients can be local or remote, it doesn’t matter to X. X just displays them as requested, on the local screen or over a TCP connection. This is lower-level stuff than terminal servers and allows graphical programs to run on one machine and display on another.

Same XServer we are going to use for our Docker container.

For that we need to run the command while launching container with options for sharing our Host’s XServer with a container, we need to create a volume then share, give an environmental(env) variable DISPLAY need to mention from where we are connecting here we are using HOST.

To share our Host’s XServer with a container, we need to create a volume then share

--volume=”$HOME/.Xauthority:/root/.Xauthority:rw”

Environmental Variable

--env=”DISPLAY”

sharing from base OS, So I need a network i.e, host

--net="host"

Here we are going to run gedit, firefox, and jupyter on top of container.

Always good practice to create an image. But before that let me show you step by step configuration manually then later we can create a DOCKERFILE

Launch Container with centos image

I already created one Docker image same image I’m taking to launch the container you can refer to this to create your own docker image in this image I already configured few things and installed GEDIT, FIREFOX, AND JUPYTER.

Launch Docker container from below command with any Linux centos image version

docker run --rm -it --net=host --env=DISPLAY --volume=$HOME/.Xauthority:/root/.Xauthority centos:latest

Install packages Gedit, Firefox and Jupyter

yum install gedit firefox -y

To Install Jupyter needs to install dependencies Python36 which install both python and pip3 then install jupyter using PIP3

yum install python36 -y
pip3 install jupyter

Now let’s run the GUI applications

By running the below commands we can see GUI windows

>> gedit
>> firefox
>> jupyter notebook --allow-root

I started my practical by using launching centos conainer and explained step by step, as we discussed in the begining it’s very good practice to create an image using Dockerfile.

Here we are going to write docker file and built image(BUILD), then pushes to docker hub(SHIP) then going to use that image to launch container(RUN)

Create a workspace(directory)

>> mkdir workspace

Create a Dockerfile inside workspace

>> vim Dockerfile

Inside Dockerfile write this code

>>vim DockerfileFROM centos:lates
RUN yum install firefox gedit python36 -y
RUN
pip3 -q install pip --upgrade
pip3 install jupyter
CMD ["/usr/bin/gedit"]
CMD [“/usr/bin/firefox”]

Now let’s Build an Image using the build command make sure you in the current directory

>>docker build -t dharshan/container-gui:v1 .

Show Image

Launch new container with the name of GUI by image

Push Image to Docker Hub

>> docker build -t userName/imageName:version containerName

Dockerhub

get my image from dockerHub

Run Gedit, firefox and jupyter commands

Runing firefox

>>firefox

firefox on container

Running Gedit

>> gedit

gedit on container

Running Jupter notebook

>> jupyter notebook --allow-root

Jupyter on container

Thanks for Reading !!

Keep Learning !! Keep Sharing !!

--

--