How to run GUI Application in Docker Container
It is said that Docker is commonly used to install background applications and CLI programs, but You can also use it to run graphical programs. You can use an existing X Server, where the Host machine already running a graphical environment.
Why we run GUI application in Docker?
Launching the GUI program in Docker can be a useful way when exploring a new piece of software. You can install the software in a clean container, instead of messing up your manager with new packages.
In this task I am using RHEL8 as a base operating system and on this base OS I will launch docker container.
Step 1: Configure Docker repository in RHEL8.
For configure docker repos on RHEL 8 go to /etc/yum.repos.d/ directory and make new directory where you can provide path to install docker in your local system.
Step 2: Install Docker on RHEL 8 operating system.
To install Docker on RHEL8 use command yum install docker — nobest.
Step 3: Start Docker services.
Without starting docker services on Base OS you are not able to use docker services. So to start docker services use command systemctl start docker. After that to check docker services are running or not use command systemctl status docker.
Step 4: Pull centos image in docker as a base OS.
To run any application we required base OS without any OS we are not able to run our application so, in docker container also we required base OS to run application in docker, so for this I am pulling centOS image as a base OS in docker container. So, to launch centOS image in docker use command docker pull centos.
Step 5: Running docker container with GUI support.
To run docker container with GUI support we have to forward x socket to a docker container.
Providing a Docker container with access to your host’s X socket is a straightforward procedure. The X socket can be found in /tmp/.X11-unix
on your host. The contents of this directory should be mounted into a Docker volume assigned to the container. You’ll need to use the host
networking mode for this to work.
You must also provide the container with a DISPLAY
environment variable. This instructs X clients – your graphical programs – which X server to connect to. Set DISPLAY
in the container to the value of $DISPLAY
on your host.
You can encapsulate all this by running the following command:
docker run -it — name <image_name> -e DISPLAY="${DISPLAY}"
-v /tmp/.X11-unix/:/tmp/.X11-unix/ centos
Step 6: Install any GUI application in docker.
To launch GUI application in docker we have to first install any GUI application in docker container. for that I am installing firefox browser.
command: yum install firefox -y
run command: firefox
Thank You!!!