Running a GUI Application on Docker

Akurathi Sri Krishna Sagar
3 min readJun 17, 2021

--

Whenever you run any docker image, in most of the times, you get a black screen terminal. But, there is a way to run GUI applications in docker. In this article, I’ll show you how to run Firefox browser in docker container.

Here, I’m using the centos:latest Docker image. After the image is pulled from docker hub, we have to install Firefox and run it. To avoid these manual steps, I will show you the Dockerfile, which when you run, you will get a Centos image configured with Firefox and run the Firefox as soon as any container is launched from it.

The following is the Dockerfile :

FROM centos:latest
RUN yum install firefox -y
CMD firefox

In the above Dockerfile, “RUN” command runs a command during build time i.e., the time during our image is being built.

“CMD” command is used to run a command during run time i.e., when any container is being run from the image.

After writing the Dockerfile, we have to build the image. For this, go to the directory of Dockerfile and run the following command :

docker build -t docker-gui:latest .

In the above command “-t” option is just give a name or tag to the image which is going to be built. Here, my final output image will be there with the name “docker-gui:latest”. And, in the above command, “.” is to indicate that the Docker file is in the current directory.

If the 3 steps in the Dockerfile are successful, you will get the following :

Now run the command “docker image ls” and you can view the newly created image :

You can push the newly created docker image to the docker Hub. In my case I’ve already pushed the image and you can use that image directly by pulling it with the command :

docker pull asks1012/docker-gui

For pushing the image, first run the command : “docker login” and provide your docker hub credentials :

You have to follow a definite naming convention in order to push your docker image to hub. Now, tag the docker image with your docker hub username like below :

docker tag docker-gui:latest asks1012/docker-gui:latest

Now, run the following command to push the docker image :

docker push asks1012/docker-gui:latest

You can see my image in docker hub now :

Now it’s time to run a container from the image with the command :

docker run -it --name myfirefox -e DISPLAY=$DISPLAY --net=host asks1012/docker-gui

In the above command, the host DISPLAY environmental variable of the host or base OS will be shared by the docker. “ — net” option gives the docker container greater access to the network of the host.

Firefox browser will be launched :

Thanks for reading 😊

--

--

No responses yet