How to run GUI applications in docker

Today lets see how we can run gui applications in docker like firefox, gedit, jupyter notebook which we normally use in ML/DL codes, etc.

First lets see what happens if we try to run. So, run a centos container using the command docker run -it centos and install firefox in it using the command yum install firefox. I have already made an image where firefox is preinstalled so just using it to launch my container, it doesn’t make any difference.

Now i am inside the container. lets try to launch firefox.

it shows errors like no dbus and no display environment. if i normally run it in my redhat os it get run without any error. why ?

so, this is because, to run any gui application, we need a display where it can run. so, for the display, we have X11 server in redhat which is actually not present in docker. let me show you

So, as you can see, the X11 folder has some file where the server code is present which is actually empty in the docker container.

So, either you can copy this in your container or you can mount this folder with the docker container. Lets launch a container with mounting it

so, as you can see we have mounted it using the -v option and it came also in docker too. Now lets try to run firefox again as we have this server in docker.

Again error, but this time something changed. see, it saying somthing like no display variable is specified. let’s check it

So, as you can see this variable is empty. this variable actually tells our programs that we have this display go and use it. But right now it is empty. If i check this in my base os i.e. redhat

so we have this in our redhat. so lets share this variable also while launching our container. we can share the environment variables in docker using the option -e.

or instead of sharing, in the previous docker container you can also specify it manually as shown below👇

Now try to run firefox

so, again same error that display env ariable not specified althought we have set it. Then why ?

This is because when we set an env variable in our terminal it can be used by the terminal only(terminal is also a program), if we want that other programs can also use it then we have to export it as shown below👇

Now again try to run.

How can you confirm that this firefox is from your docker container and not from your base os ?

then see in the top boundary of firefox, there is an id written which is the same sa of our container.

So, now you can run anything on this container!

If it still shows some error in your case, then just give a command in your base os as shown below👇

Sometimes x server doesn’t allows other applications to use it, so using this we just allowed docker to use x11.