X forwarding on Docker

Mark Buckler and I use Docker a lot. Sometimes, we need X11 forwarding to work over SSH. Here’s a summary of the steps involved in case you’re curious.

Edit the Docker file

Add these lines:

RUN apt-get update
RUN apt-get install -qqy x11-apps

This installs the necessary X packages on the Docker image.

Run this once per SSH session

XAUTH=$HOME/.Xauthority
touch $XAUTH

This code sets the path to the .Xauthority file. When using SSH with X11 forwarding, the .Xauthority file is automatically created in the user’s $HOME folder. If not using X11 forwarding (for instance, when operating locally on the machine), touch will create the .Xauthority file.

Start the Docker image

docker run --tty --interactive --network=host --env DISPLAY=$DISPLAY --volume $XAUTH:/root/.Xauthority <dockerimagename>

This starts a Docker image with a TTY, in interactive mode, using the host’s networking stack (i.e. the container and the host are identical in their network usage, so port XX in the container is port XX on the host. It also exports the $DISPLAY environment variable to Docker, and binds the .Xauthority file (on the host) to the root user within the container.

That’s all folks! I suspect that these instructions will also be cross-posted on Mark’s website. You should absolutely follow his blog.

comments powered by Disqus