Run a GUI Application inside a Docker Container
Docker is one of the most common container technology. Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Docker makes application deployment very fast and efficient.
There can be two types of applications that we can containerize.
- Applications that run as a Background Service (like a Database, WebServer, etc)
- GUI Applications that run in the foreground
Docker’s normally used to containerize background applications and CLI programs this is the normal trend in the market. But we can also use it to run graphical programs, YES we can!!!
First of all we have to aware how a GUI program Run on machine with the help of OS. So there is a X server program which helps a GUI program to run on a system window.
An X server is a program in the X Window System that runs on local machines i.e. the computers used directly by users and handles all access to the graphics cards, display screens and input devices (typically a keyboard and mouse) on those computers.
But We know that Docker container run in a isolated environment and don’t have any X server program. So what we can do,
- share the Host’s X Server with the Container by mounting a volume. It can be found
/tmp/.X11-unixon linux host.
- share the Host’s DISPLAY environment variable to the Container. This instructs X client our graphical programs. which X server to connect to.
- run container with host network driver.
So Lets start….
For Doing this practical my plan is like:-
- Firstly create a docker image, in this a GUI program install ( I choose Firefox) with the help of Dockerfile.
- Then create a Docker-compose file to deploy our container.(By simple command we also deploy our GUI container)
RUN yum install firefox -y
we can firstly build the image or direct run the docker-compose file via command.
By this command firstly docker image build named as mukuljeveriya/firefox_container:v1 and then all the services write in docker-compose file will start.
Look in the above image service start successfully. Our GUI application run inside a docker container.
On my host machine RHEL8 installed but here you can see as I took centos as a base image in my dockerfile so the Firefox run on Centos.
You can see mukuljeveriya/firefox_container:v1 image, this is the image I created in which I installed Firefox.
We can also launch our GUI container directly with docker command without docker-compose.
docker run --net=host --env="DISPLAY" -v /tmp/.X11-unix:/tmp/.X11-unix --name <any name> <imagename/:version>
Thanks for reading , I hope you like the Blog!!!
Suggestions, Feedbacks are always welcome, keep in touch on Linkedin.