How to run GUI Application within Docker Container?

Nowadays, Container is almost used everywhere either in the production world or to set up local environment. Containers are a solution to the problem of how to get software to run reliably when moved from one computing environment to another.

A container consists of an entire runtime environment: an application, plus all its dependencies, libraries, and other binaries, and configuration files needed to run it, bundled into one package. By containerizing the application platform and its dependencies, differences in OS distributions and underlying infrastructure are abstracted away.

Docker is one of the powerful tools which help in launch and manage the container. It also called container runtime.

We mostly use container to launch software having no GUI environment. But some applications need GUI to launch.

So let’s find out some way to launch GUI application inside container.

For a GUI application to run, we need to have Xserver which is available as part of every Linux environment by default, but within a container, we don’t have any Xserver.

An X server is a program in the X Window System that runs on local machines and handles all access to the graphics cards, display screens, and input devices (typically a keyboard and mouse) on those computers.

So to use Xserver within container, we will :-

  • share the Host’s DISPLAY environment variable to the Container

--env="DISPLAY"

  • run container with host network driver

--net=host

Now let’s build a docker image using Dockerfile for testing…

Now run the command…

You can directly use that as I upload it to my Docker hub account

Now let’s run the container…

We can see that it works perfectly fine…🙌

We can build other images too in the same way which run GUI Application like android studio, VS Code, etc.

Hope you enjoyed the article…

Feel free to connect on linkedin…😊

Having any issues related to article, please DM me…

Tech Enthusiasts, Passion to learn and share