Launching GUI on Docker
— Using graphic based application on containers
Container are used for specific use cases only. For example, one container is used for hosting web services, and another to host database services, not using single container for both use cases.
That means, containers are designed to be light, so that they can process fast. As we know graphics requires lots of computing and processing, thus tends to slow the system comparatively. Thus by default graphics are not supported in containers.
But sometimes we may want to use some graphical based tools, software etc. For example, launching a python environment in container. as discussed in previous article:
Machine Learning over Container
— Launching python environment with docker, to run M.L models
As a programmer, we are more comfortable in writing code in I.D.Es or some editors, with generally requires graphic based support, like we’ve jupyter-notebooks, sypder etc. for python, or VsCode as integrated editor. We all have our favorites, but using them in containers requires graphics. That’s what we’re gonna do in this article, by providing graphical support to containers, via X11 socket of docker hosts. How ?
Very simple, requires only one step:
docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix
-e DISPLAY=$DISPLAY will inherit the display environment variable of my docker host. i.e. :~
-v /tmp/X11-unix:/tmp/X11-unix will mount Container’s unix X11 socket to docker host’s unix socket. Thus it can use the graphic support of base docker host.
Let’s see some hands-on as I’ll show to install jupyter-notebook and launch it with firefox browser from centos container.:
- Launching container with GUI support, with same command shown above,
2. Installing python36 & Firefox packages,
yum install python36 firefox -y
3. Confirming firefox running on centos container,
Now let’s install Jupyter, and launch the notebook, from container.
Finally, launching GUI based I.D.E :~
Similarly, you can use any graphical based editor, or even more, any tool software which requires graphic support with this method.