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…
On 2013, Solomon Hykes introduced the biggest innovation of decades called Docker & within 2 years docker & container technology became the most demanding technology in the Industry.
- Previously when it was taking 10 to 15 minutes to configure & setup a server on VM or over Cloud Computing, there Docker was able to launch the entire OS with pre-configured server in just 1 or 2 seconds. Industry started booming & every company started implementing DevOps solutions like Container Technology.
- 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.
Note : I will talk about the basics of Docker at first, so that anyone who's reading this blog can learn this amazing tool.
What is a Container ?
Let's think in this way — Why we need OS ?
- Because we want to run some program in it. OS has the capability to contact to hardware to run our Programs. Now as per the constraints we can only run one OS on top of one RAM & CPU at a particular point of time.
- 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).
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.
I'm using a Windows system & in it I have installed Oracle VirtualBox Software, there I have one RHEL8 VM. This will be my Docker Host. You can use any Linux OS to install Docker but based on the Flavour of the Linux Distribution, Docker Installation process will be different. But once we install the Docker, then everything is exactly same no matter whatever OS you are using.
Note: I'm not showing how to install Docker because there are lots of article on Google on “How to install docker-ce on CentOS8/RHEL8”. Just follow any of the one & for reference I'm sharing one link — https://www.linuxtechi.com/install-docker-ce-centos-8-rhel-8/
Once you download & install the Docker software, start the docker service.
What is a Docker Image ?
To install any OS, we need respective image. Image contain all the data, that we need to run that particular OS.
- Similarly to launch a Docker Container or OS we need to use Docker Image. There are thousands of image on Docker repository called Docker Hub — https://hub.docker.com/.
- 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.
Now we gonna use Docker to download that image on our local system, in my case it’s RHEL8 system which is my Docker Host. Run the below mentioned command to download the image…
docker pull ubuntu:18.04
Next you will see this output mentioned in the below screenshot…
Once image download is successful, run the below mentioned command to see how many images you have locally downloaded…
docker image ls
Let's download one more image called centos 8. Similar like previous command run…
docker pull centos:8
Now we have two Docker images. Let's go to the next step.
Launching the Container :
Now here you will see the actual power of Docker. As I had already told that we need Docker Image to launch the container. So, now as we have the images, let's launch one container. Run the below mentioned command to launch a container…
docker run -it --name os1 ubuntu:18.04
- We have given our docker container a name called “os1” & it's launched using ubuntu:18.04 image. Here this “-it” option means after container launch provide me one “Interactive Terminal”.
- 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…
Now as you are on your local Docker Host GUI, there open one new terminal without closing the previous one. Here run…
You will see one container is running & how much time before it was launched. Now let’s do one more very much interesting thing. Run the below mentioned command & launch one centos:8 container…
docker run -it --name os2 centos:8
As soon as you run the command, now you are inside “centos:8” docker container. To check that run “yum repolist” & you will see it's working fine. But if you run “apt list” it will say “apt command not found”. For reference check the below mentioned screenshot…
Isolation of the Containers :
I have already told previously that Container is like an isolated space with it's own OS respective command, files & folders, own network adaptor, own processes, storage etc. Let's check if those things are there or not. On os2 run the below mentioned command…
yum install net-tools -y
Note : If you see yum is not installing the software, then on your base OS, stop the firewalld service & then restart the docker & then launch again one new container with a different name & it will start working.
- Once this software is installed you will get some commands on your container. Now run “ifconfig” command & you will see your container has one entirely new IP address with respective network adaptor. Next if you run “ls” command you will see it has all the OS specific files & folders.
- 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…
To start a stopped container & then to go inside the running container, you can use the below mentioned commands respectively…
docker start os2
docker attach os2
There are lots of concepts of Docker, but as a basic starting this much knowledge is enough to go to the next practical.
Creating Docker Image :
Now it's time to create our own Docker Image. There are two methods of doing this — Docker Commit Command & Dockerfile concept. I want to talk about Dockerfile.
- Remember one thing that our final goal is not just to launch the OS. Our final goal is to run our programs inside container. So, for that either we can launch one container & can run our program, but in Industry we run thousands of containers in parallel.
- 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.
This is the reason we want to create Docker Image & for that purpose we use “Dockerfile” concept. On your Docker Host create one folder to use as your workspace & then inside that workspace create one file using vim or gedit editor. One small thing to note here is that the file name should be “Dockerfile”. Each & every character of the file name should be exactly same like I had written.
Now in your “Dockerfile” put the below mentioned code…
RUN yum install net-tools -y
- Save the file & now let's understand how this file has been written. Dockerfile has it's own specific keywords to tell certain things. Like we used “FROM” keyword to tell use “centos:8” image & on top of this image we will run our program.
- 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.
Now it's time to build the image. For that run the below mentioned command on the same folder where you have the “Dockerfile”…
docker build -t myimage:v1 .
- Here we are using “docker build” command to build the image & to give the image a name/tag we are using “-t” option. Then we give “myimage” tag to our image & the “v1” is the version of our image. You can give any tags & version name you want, but try to be logical in this part.
- 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.
Now you will see your image is ready. So again to create the container from this image you can run…
docker run --name myos1 myimage:v1
- Here we are not using “-it” because we don't want to use the terminal of our container. So, as you can now realize we are launching one container & gave the name “myos1”, & it's launching from our own created Docker Image. For reference check the below screenshot…
- You will see within 1 or 2 seconds, you get the o/p of the “ifconfig” command from your container & the best part is you won't even realize when you login & logout from the OS of the container.
- 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 :
Next if you run “docker ps -a” you will see your “myos1” container launched just a few minutes ago.
- One thing to note that in Docker if you create one container with let's say “myos5” name, then in future you can't again use “docker run” to run the same container with the same name, because it's already exists.
- 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 ?
For that run the below mentioned command…
docker rm myos1
- Now here instead of providing your container name you can also provide the container id. Now if you run “docker ps -a” you will see “myos1” container removed.
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`
- Here we are using “-f” option means forcefully to even remove the containers that are currently running. Next we use back quote to tell provide the output of the command that's inside back quote to the “docker rm” command. And lastly “docker ps -aq” helps to find all the container ids.
Similarly, there are hundreds of commands & options we have in Docker for achieving different kind of used cases. I believe now as we have the knowledge of “creating your own docker image” & how to launch the container. We can go to the final practical !!!
Running GUI Application inside Docker Container :
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”…
RUN yum install firefox -y
- As we can notice it's a short basic “Dockerfile” which is using “centos:8” image & installing “firefox” software in it. Then finally it's starting the Firefox software using “CMD” keyword.
Now let's build the image…
docker build -t myfirefox:v1 .
Once build is done, let's launch the container. So as like previous time run the below mentioned code…
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
- Now it's obvious that you might be thinking why we need “net” option, in this command. Here, this is a networking concept, but in basic if we want to understand, then you can think this option goanna connect your docker container with your RHEL8 docker host’s main network card. This option is actually needed because after launching container it will provide us the Firefox software in GUI window, now whatever we are searching in this window needs to go to the Docker Container via Docker Host. For that connectivity we add our Docker Host network to the container.
That's all… Now let's see it in action in the below GIF…
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 — https://youtube.com/playlist?list=PLAi9X1uG6jZ30QGz7FZ55A27jPeY8EwkE
- 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…
Raktim Midya - Microsoft Learn Student Ambassadors (Beta) - Microsoft | LinkedIn
★ I'm a tech enthusiast working to better understand the core concepts behind different popular Technological Fields…
Thanks Everyone for reading. That’s all… Signing Off… 😊