How to run GUI applications with Docker on MacBook

For one of my recent projects where I am required to use Docker on Macbook, I had a hard time figuring out how to run GUI applications with Docker installed on Macbook.

1. Pull your docker image.

2. Install socat on macbook

Steps to install socat is "brew install socat"
Get Homebrew if you dont use it.

3. Run the following command in a terminal

socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"

4. Install XQuartz

brew cask install xquartz
open -a XQuartz

4. Find out the IP address you are connected to

ipconfig getifaddr en0

5. Run your docker container using the following command

docker run -it --rm -e DISPLAY=<ip address from previous command>:0 -v /tmp/.X11-unix:/tmp/.X11-unix <Your favoriet docker image>

Another command for non Macbook systems like linux servers

docker run -it --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw"  <image name>