GUI Application inside Docker
Gaurav Rathi
Cloudops Engineer | RHCSA | RHCE | RHCoE(CEPH) ,(OpenShift) | Docker | Ansible | Kubernetes | OpenStack | AWS | MLOps | Jenkins |
Docker containers run as a process under the main operating system, you can run multiple containers on a single machine. This means that, instead of choosing the exact right size for each machine, you can simply provide a large machine and run several Docker containers on that machine. If you need more horsepower for one of your containers, you can simply move it to a new machine. Mostly container applications run in background mode but we can run foreground (GUI application) over docker container if the base os has GUI facilities.
we have many by which we can launch GUI inside Docker but here I use 2 of those in both some of the part is same or we can say almost all process is same we have a site difference at last
Follow the steps to run firefox(GUI App) on the top of docker:-
1) Create a Dockerfile that consist firefox as an application
FROM centos:latest RUN yum install firefox -y CMD /usr/sbin/firefox
2) create a Docker image with Dockerfile by using build cmd.
# go inside the folder where we have Dockerfile docker build -t <image_name>:v1 <location-of-docker-file> docker build -t firefox-centos:v1 .
3) After creating image now we have to write a cmd by which we can launch docker
in that cmd we use some more specification and that are
1.share the Host’s Display environment with container
--env="DISPLAY"
2. set the networking to the host driver
--net=host
— net=host option is used to make the programs inside the Docker container look like they are running on the host itself
3. share your Host’s Xserver with the container by mounting it with container volume
--volume="$HOME/.Xauthority:/root/.Xauthority:rw"
now here we have many ways to take this Xauthority we can take this and we have one more that is
- 3.1 We need to transfer the X11 socket from Linux base OS to the container in order to run directly.
-v /tmp/.X11-unix/
4) so now launch with first one
docker run -it --net-host --env="DISPLAY" --name gui --volume="$HOME/.Xauthority:/root/.Xauthority:rw" firefox-centos:v1
4.1) lets see with second one also
docker run -it -e DISPLAY --net=host -v /tmp/.X11-unix/ firefox-centos:v1
5. lets see o/p
Here you can see that gedit is running in GUI mode with some warnings in the backend.
Thank You