Getting started with Docker & Running GUI Application inside it

Mostly we run our servers or tasks inside Docker Container, but ever imagined — we can even launch GUI applications inside Docker Container & can access them from the Docker Host system. Stick with me till the end of this blog to learn the basics of Docker & how to achieve this fantastic practical…

Raktim Midya
Apr 20 · 14 min read

Docker :

Docker logo, Source : Google
  • In the mean time Docker also started improving & the biggest profit Docker had at that time was — It was an open-source product. Means thousands of Techy guys started implementing new new ideas on Docker & it also started improving over the years.
  • Today container technology is so powerful that it can do each & everything we want. We can setup multi node clusters using Container like Kubernetes Cluster, Storage Cluster, Database Cluster, Compute Cluster, Network Cluster etc. In fact nowadays Container Technology is so powerful that we can even setup entire Private Cloud Computing in it.
  • There’s limitless possibilities Container Technology has but in this blog I gonna talk about one of the very much interesting possibility of Docker Container that is we gonna run GUI applications like firefox, gedit etc. inside Docker Container.

What is a Container ?

Self made using Canva
  • If you notice this constraint, then you will always feel your resources are being wasted because most of the time we don't utilize the full hardware that we buy. To overcome the challenge of this constraint, Engineers came up with the solution called Virtualization means we will create the hardware spaces virtually so that we can run multiple OS on one hardware.
  • This solves the challenge of resource wastage for some limit but ultimately it was taking so much time to provision, then setting up the VM & then run the programs etc. Finally if you think once again the 1st question why we need OS, the answer is always the same, we want to run some programs & without OS it's impossible to run any program.
  • So why to waste time in Provisioning the OS. To solve this issue Industry started using Container Technology. The best part of Container Technology is it can launch & login to one entirely fresh new OS in just 1 or 2 seconds. Also if you has the pre-configured Docker Image then you can launch your entire OS with the running server in it in just few seconds.
  • Container is like an isolated environment to run your application. It same like having multiple VM in your system. Similar like VM, Container also has it's own OS specific commands, own network card, own OS specific files & folders, own processes, own storage etc.

Let's go to the Practical :

  • Remember one thing if you want to do virtualization then you need at least one virtualization software to be installed on your system like Oracle VirtualBox, Hyper-V, VM-Ware, KVM etc. Similarly if you want containerization then you need the respective software.
  • There are lots of program that has the capabilities to create container like Docker, CRI-O, Podman, Rocket etc. But in this blog our focus is on Docker Technology. Let's start by installing the software of Docker. Now there are two versions of Docker — Community Edition which is free to use & Enterprise Edition which requires license to use. We are going to use Docker Community Edition (docker-ce).

Pre-requisite :

Although we can run Docker inside Windows System but I'm gonna use Linux OS as my Docker Host. Means on my RedHat Linux 8 system I will install Docker Software. If you don't have RHEL8 system with you, then you can also use CentOS/Fedora system too.

What is a Docker Image ?

Self made using Canva
  • Most of the images on Docker Hub is free to use. Let's say we want a Ubuntu 18.04 OS, then to launch it using Docker we at first need the Docker Image of Ubuntu 18.04. Go to Docker Hub & search “Ubuntu” & you will find the official image.
docker pull ubuntu:18.04
docker image ls
docker pull centos:8

Launching the Container :

Self made using Canva
docker run -it --name os1 ubuntu:18.04
  • You will see within 1 second, your terminal changed & now you are inside a new terminal. The most interesting part here is this terminal is not your Docker Host (RHEL8) terminal. It's the terminal of Ubuntu Container.
  • That means now you will see none of the RHEL8 specific commands are working. For example run “yum repolist” command & you will see “yum command not found”. Because in Ubuntu we use “apt-get” to install the software. If you run “apt list” it will start working. For reference see the below mentioned screenshot…
docker ps
docker run -it --name os2 centos:8

Isolation of the Containers :

Self made using Canva
yum install net-tools -y
  • Now to exit from the container just run “exit” & you will see you again landed on the terminal of your Docker Host. Next if you run “docker ps” you will see no container is running, but if you want to see the stopped containers then you can run “docker ps -a”. For reference check the below mentioned screenshot…
docker start os2
docker attach os2

Creating Docker Image :

Self made using Canva
  • So, it's impossible to run program manually in every container. So what we can do is — we can put all the information inside Docker Image & once we launch the container our program will also start & till the time our program is running container will run & once program close, we don't need the container so it will automatically stop.
FROM centos:8
RUN yum install net-tools -y
CMD ifconfig
  • Next to run any program we need to install the required software, for that we used “RUN” keyword & tell to use “yum install net-tools -y” command to install the software. Just note one thing that as we are using “centos:8” to run our program that's why to install the software we used “yum” command.
  • Next we used “CMD” keyword to tell as soon as the container launch run “ifconfig” command.
docker build -t myimage:v1 .
  • Lastly we use “.” to tell docker that Dockerfile is there where we are running “docker build” command. If you notice here we are not mentioning any file name because I already told you this name of the file “Dockerfile” is fixed. So, on your currently workspace it will look for that file & start building the image from it.
docker run --name myos1 myimage:v1
  • Why you logout so quickly ? — Because I have already told you, we use container to run our program. Now as “ifconfig” cmd takes less that 1 second to run, so our container will run for that moment only.

Deleting old containers :

Self made using Canva
  • So, what we can do is, we can change the name each time to launch the container. But one thing to note that the data of the container is actually stored in your hard disk, means soon you will feel lack of space. So how to remove one container ?
docker rm myos1

One quick trick we normally follow on Linux to remove all the containers, that is run the below mentioned command…

docker rm -f `docker ps -aq`

Running GUI Application inside Docker Container :

Self made using Canva

Customized Docker Image Building :

Let's say we want to run “firefox” program inside Docker Container. So, what we can do is — We can create one Docker Image & can say whenever any container launch from this Image, run the Firefox program. So, let's create the “Dockerfile”…

FROM centos:8
RUN yum install firefox -y
CMD firefox
docker build -t myfirefox:v1 .
docker run -it --name firefoxos1 myfirefox:v1

You will see errors & finally Firefox will not launch successfully. Why ?

  • Logic is Firefox need one environmental variable to run itself. This shell variable actually help Firefox to project it's gui tab on screen. Now we definitely know that Docker Container don't has a gui screen to run the Firefox, that's why we need to tell the container to use the host system’s gui screen & for that we gonna use that environmental variable using the option “env” in “docker run” command. So, run the below mentioned command…
docker run -it --name firefoxos2 --env="DISPLAY" --net=host myfirefox:v1
Process demonstration

Final Words :

  • There are thousands of companies in today’s Industry who are using Container Technologies because it’s super fast, has great scalability & obviously it’s secure. Docker has lots of possibilities & learning Docker is so much helpful in today's Industry. To learn lots more about Docker you can refer to this YouTube Playlist —
  • I tried my level best to talk about Docker & it’s possibilities. Also we have seen one of the very much interesting practical. Just wanted to say, lastly if you like this blog, give some claps & leave your thoughts in the comments.
  • I keep on writing Blogs on Machine Learning, DevOps Automation, Cloud Computing, Big Data etc. So, if you want to read future blogs of mine, follow me on Medium. You can also ping me on LinkedIn, checkout my LinkedIn profile below…

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn.

Raktim Midya

Written by

Technical Content Writer || Machine Learning | DevOps Automation | Cloud Computing | Big Data Analysis Enthusiast

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store