Most of us know that Docker is a containerization tool that helps us in provisioning an OS within seconds.
Well if you did not know about that, you know it now !! ๐
It basically creates a process in the base system which is treated as a new operating system.
In most of the use cases in the industry utilizing the container technology we use the CLI only and Docker gives us the CLI of a container only and not the GUI.
But it might be sometimes needed that we want to launch a graphical application inside a docker container and that is by default not possible. We need to pass certain variables during the runtime of the container so that the container can launch certain GUI applications.
๐ฉ In this blog, I am going to show you how you can enable and launch a GUI application inside a docker container. โจ๐คฉ
I will be using ๐ฅ๐ฒ๐ฑ๐๐ฎ๐ ๐๐ป๐๐ฒ๐ฟ๐ฝ๐ฟ๐ถ๐๐ฒ ๐๐ถ๐ป๐๐ ( ๐ฅ๐๐๐ ) ๐ด operating system as the Docker host that is the base system.
๐ You need to have docker configured in the system. You can refer to the below-mentioned blog where I have explained how to configure docker.
After you have configured docker in your system, proceed with the following steps. โ
I have created a basic Dockerfile that will have the GUI applications software already installed.
You can find the Dockerfile in the following Github repo.
Create a directory and save this Dockerfile there.
mkdir docker_gui/
cd docker_gui/
โ The name of the file should be โDockerfileโ only. โ
After you have created the Dockerfile using your favourite text editor, now it's time to create the docker image.
docker build -t docker_gui:v1.0 .
Make sure to run this command in the location where the Dockerfile is present.
I have already created the image and uploaded it to Docker Hub. You can pull that image also.
Docker Hub Link โ https://hub.docker.com/r/rahulsil/docker_gui
After we have all the above things set up, let us move onto the main part of launching a GUI application inside a Docker container.โจ
docker run -it --name guios--net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" rahulsil/docker_gui:v1.0
Run the above command to launch a new container.
Now the command is quite messy, right? Let us understand it properly:
- run:- is used to start our container
- -it:- this keyword provides us with an interactive terminal
- - -name:- used to set the name of the container
- guios:- it is the name of the container, anything could be given.
- - -net=host:- use to launch the container with the host network.
- - -env=โDisplayโ:- This is used to share the display of the host to the container
- - -volume=โ$HOME/.Xauthority:/root/.Xauthority:rwโ:- This is used to share the host X server with the container by creating volume.
As you can see as we launched the container and executed the firefox command the firefox browser opened and shows the centos home page as the container is created from Centos image.
This way we have successfully completed the task of launching a GUI application inside a docker container. ๐โจ
I will soon come up with a blog where I will be explaining the basics of docker and the commnds of docker, dockerfile that I have used here. Stay tuned for that !!
I hope you liked this article.๐
Would definitely like to hear your views on this and feedbacks so that I can improve on those points in future articles. ๐ Comment your views below.
You can also check my LinkedIn profile and connect with me.
Follow me on medium as I will come up with articles on various technologies like Cloud Computing, DevOps, Automation, and their integration.
Thatโs all for now. Thank You !! ๐โ