How to run GUI stuff inside a Docker container using Docker for Mac

I used to have a flow to set up a display for my Docker containers when the Mac version used boot2docker. However when I made the update for Docker for Mac unexpectably my displays were gone and I was really annoyed with that. Then I went out there trying to find a solution and it was not easy.

But, guess what: It actually came out to be really simple 😆 I found the solution in Docker forum and I decided to write more about it here. Theoritically the solution stills the same as with boot2docker, however with subtle different configurations. Fire up a XQuartz and then forward Docker display to it.

I will use an Intellij container that you can pull from snellm/intellij with

docker pull snellm/intellij

But of course you can use whatever image you want. Anything that as a GUI I mean 😅

Get XQuartz

First thing is to get XQuartz for Mac. XQuartz is a version of the X.Org X Window System that runs on OS X. Go and get the latest version here. Install and then move on to the next step.

Launch that XQuartz

First you need some little config job. Open up XQuartz

open -a Xquartz

Go to preferences and then to the security tab and check “Allow connection from network clients”. Then restart XQuartz and check whether it is listening to the port 6000

lsof -i :6000

Get your machine IP

Create an environment variable with your IP

ip=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}') && echo "My IP is: $ip"

Tell your machine: It is ok to talk with XQuartz

xhost + ${ip}

Fire up your container

Get your container up and running. If you are not using docker-compose fire it up like this

docker run -d --name intellij -e DISPLAY=${ip}:0 -v /tmp/.X11-unix:/tmp/.X11-unix snellm/intellij

Yes. That’s it. You can now use any GUI app inside Docker. Hope it helps ⚔

Originally published at