Run GUI commands in Docker Container

VinayGupta
3 min readOct 28, 2021

--

In this chapter, I will show you how we can run GUI commands(like gedit, firefox...) in Docker Container. (Keep in mind, “By default, Docker Engine launches the container with CLI mode”)

For performing this practical, we will use Redhat as host. So let’s get started…!

First of all, make sure you have installed docker-ce on your system & started the services. Then for verify, simply run # docker ps command.

Now, Lets talk about some fundamental about DISPLAY. In Graphical Redhat system, we are able to run GUI programs because there exists the device called DISPLAY. Mostly, they have 0 as default display. You can check it from below command.

Second big reason is, In our BaseOS(host) there is one server is running in background by which we can start the GUI program. The name of this server is X-server. Also, this server provides the value for display in form of socket.

Here, X0 & X1024 are two server socket helps to Start GUI.

Now, the same thing if you try to see in Container launch from docker, you will not found any Display socket because container don’t have X-server & that’s why they don’t have sockets. Verify by running ls command.

This container is launched by centos linux.

As shown, they don’t have directory & no display value is showing. Now, here we know that our host contains the X-server & socket file also. So, we can tell our host to provide X-server’s services to guest system also(Here, guest system is our container).

# xhost +local:docker 

By this command, we added docker services to local connections in which now X-server can provide their service.

Still we haven’t specify that when docker container launches which display number it have to work on. And for doing this that container must require X-server socket files.

For this both requirements, we can share our host’s DISPLAY env variable & Socket file also to container while launching it. For any files that you want to share with docker container, then -v <source_host>:<dest_guest> & for sharing Environmental variable -e <var_name>:$<var_name> , Here $ represents the Env variable.

Sharing env variable & socket files X0 & X1024 to container

Now, you will see this Socket files on container(guest) also. But, you need to tell which display to use.

X0 & X1024 is now available.
Tell container to use :0 display for GUI program

Now, install any graphical program on container(As they doesn’t comes as by default).

Gedit is GUI text editor in linux.

After installing run that program(Here, I have ‘gedit’)

# gedit

Finally, we successfully launched GUI program in container. As container doesn’t comes with GUI mode, while launching gedit they might throw some WARNING!! Same way, just install firefox & launch it…!

Thank you…!

--

--

VinayGupta

Hey..! I'm Vinay Gupta, on the way of learning new technologies.