GUI Based Application inside Docker Container

Yash Hirulkar
FAUN — Developer Community 🐾
5 min readApr 9, 2021

--

Today, the entire IT world is utilizing the power of a container to do some amazing stuff. No doubt, containerization has turned out to be a boon for the tech market. (Here, I am particularly talking about the Docker)

  • Most of the times you have heard that people generally use containers for Enterprise Server Applications. But standing alone from the crowd, is it possible to use containers for a Desktop environment?

And the answer is a YES…!

  • Did you know, that the Docker container has a huge scope when it comes to impacting the Development Environment?
    The best part about Docker is that — using docker, you can containerize almost every single technology you work on. From launching the environment to deploying some real servers on it — just a second away!
  • Now, the most important question — How is it possible to run a GUI based app inside Docker? Here is the answer:

If we talk about the running nature of any application, you can find only two of them:

  • Applications that run in the background such as webservers.
  • Applications that run in the foreground (generally, GUI based) such as a web browser.

In this blog, I am going to show you how to run this second type of application inside a docker container using X Server.

What is X Server:

X server is a windowing system for bitmap displays, common on Linux operating systems.

The X11 server (usually Xorg these days) communicates with clients like xterm, firefox, etc via some kind of reliable stream of bytes. A Unix domain socket is probably a bit more secure than a TCP socket open to the world, and probably a bit faster, as the kernel does it all, and does not have to rely on an ethernet or wireless card.

Let’s first try to run a GUI application (say, Firefox) inside a docker container.

Follow the steps to run firefox(GUI App) on the top of docker:-

1. Install docker in your system (here I am using RHEL8)

To check whether Docker has been installed successfully use command as shown below:-

you can see docker has been successfully installed…!

2. Enable the Docker services using command as shown below :-

3. Create a Dockerfile that consist firefox as an application

Instructions used:-

  • FROM centos:8
  • RUN yum install firefox -y
  • CMD [“/usr/bin/firefox”]

you can check if the image has been build sucessfully using command shown below:-

3) Now, run the container using the above-created image.

Command:

< docker run -it — net=host -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix yash701/docker-firefox:v1>

Note:

  • docker run -it is used to launch the container
  • — net=host is used to set up the host adapter with the container
  • -e DISPLAY is used to display the firefox on the docker
  • We need to transfer the X11 socket from Linux base OS to the container in order to run directly.
  • “-v /tmp/.X11-unix:/tmp/.X11-unix”

-v bind mounts the X11 socket residing in /tmp/.X11-unix on your local machine into /tmp/.X11-unix in the container.

  • Launching container by providing it with a DISPLAY variable. Also, we will launch it in a ‘host’ network. Host network by default exposes a container so that it can be directly used from the base OS.

4. Here’s the firefox will open

  • This practical was just a small demonstration of the magical stuff that can be done using Docker.
  • Docker has an amazing scope in the IT world. Top MNCs are leveraging the benefits of Docker to a huge extent.

Now, if you want to push this image to your Docker Hub Registry:-

  1. Create a account on Docker Hub
  2. Use the command “docker login” to login into your Docker registry via cli.
  3. Add your username and password

4. Then push the image using command as shown below:-

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--