Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Last active October 7, 2021 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewfeickert/498cd93af35b6664caab5dece20342e1 to your computer and use it in GitHub Desktop.
Save matthewfeickert/498cd93af35b6664caab5dece20342e1 to your computer and use it in GitHub Desktop.
X11 Forwarding to Docker for Linux and macOS

X11 Forwarding to Docker

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/

Linux

On Linux things are pretty straightforward

$ xhost +
$ docker run --rm -ti \
>    -e DISPLAY="${DISPLAY}" \
>    -v /tmp/.X11-unix:/tmp/.X11-unix \
>    <image-name:tag>

Example:

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#

TBrowser_linux

macOS

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>

Example:

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

Shell commands

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>

Acknowledgments

#!/bin/bash
xhost +
docker run --rm -ti \
-e DISPLAY="${DISPLAY}" \
-v /tmp/.X11-unix:/tmp/.X11-unix \
atlasamglab/stats-base:root6.24.06
#!/bin/bash
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
# cleanup
unset ip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment