Friday, October 2, 2020

Running GUI Applications Inside Docker Container

Recently I was debugging an issue on a docker container which required running a legacy application inside the docker container. I use a Mac and I tried setting different DISPLAY variables but it did not work for me. Finally went back to the Docker documentation website and found about the special host.docker.internal endpoint. It is well documented here https://docs.docker.com/docker-for-mac/networking.

For my case, to get a GUI application working (say xclock), I had to do the following:

1. Install XQuartz

2. Update the Preferences of the XQuartz Terminal

 

3. From the X11 Terminal, run `xhost +`

 

You can restrict xhost with an IP address as well but in my case, its all local so I didn't care much about which IPs are whitelisted, ideally I should have run `xhost +127.0.0.1` instead. 

4. Connect to the Docker Container and run the following commands

 `export DISPLAY=host.docker.internal:0`


Once you have done it, you can run any GUI application.