Examples of X11 forwarding to Docker for Linux and macOS. In both situations:
xhost
is used to set the host and/or user names allowed to make connections to the X server- The
DISPLAY
Docker environment variable must be set - The directory
/tmp/.X11-unix/
directory must be volume mounted into the container at/tmp/.X11-unix/
On Linux things are pretty straightforward
$ xhost +
$ docker run --rm -ti \
> -e DISPLAY="${DISPLAY}" \
> -v /tmp/.X11-unix:/tmp/.X11-unix \
> <image-name:tag>
Opening a TBrowser
in ROOT
$ xhost +
$ docker run --rm -ti \
> -e DISPLAY="${DISPLAY}" \
> -v /tmp/.X11-unix:/tmp/.X11-unix \
> atlasamglab/stats-base:root6.24.06
root@<id>:~/data# root -l
root [0] TBrowser b
(TBrowser &) Name: Browser Title: ROOT Object Browser
root [1] .q
root@<id>:~/data#
On macOS things are similar but the DISPLAY
environment variable will need to be set based on your ifconfig
output.
From this, you can set your ip
for Docker
$ export ip=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
and then use that for the DISPLAY
value on the container side
$ xhost +
$ export ip=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
$ docker run --rm -ti \
> -e DISPLAY="${ip}:0" \
> -v /tmp/.X11-unix:/tmp/.X11-unix \
> <image-name:tag>
Opening a TBrowser
in ROOT
$ xhost +
$ export ip=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
$ docker run --rm -ti \
> -e DISPLAY="${ip}:0" \
> -v /tmp/.X11-unix:/tmp/.X11-unix \
> atlasamglab/stats-base:root6.24.06
To help make things easier, you can also make commands for your shell, like the following in my ~/.bashrc
:
# remember to xhost +
function root-docker () {
local input_path
input_path="${1}"
if [ -z "${input_path}" ]; then
input_path=$(pwd)
fi
if [[ "${input_path}" == "shell" ]]; then
cmd='/bin/bash'
else
if [[ "${input_path::7}" == "root://" ]]; then
# Accessing file over xrootd
file_path="${input_path}"
else
file_path=$(readlink -f ${input_path})
fi
cmd="root -l ${file_path}"
fi
docker run --rm -ti \
-e DISPLAY="${DISPLAY}" \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v /tmp/"krb5cc_$(id -u ${USER})":/tmp/krb5cc_0 \
-v /tmp/:/tmp/ \
-v "${HOME}":"${HOME}" \
atlasamglab/stats-base:root6.24.06 "${cmd}"
}
which then allow for
$ root-docker <path to .root file>
- Thanks to Giordon Stark (@kratsg) for figuring out most of this with me at US-ATLAS Hadronic Final State Forum 2019.
- Thanks to Danny Antrim (@dantrim) for debugging things with me again in 2021 and providing me some motivation to write this up. Danny also linked me to this example Gist for X11 forwarding for macOS.