Skip to content

Allowing GUI windows

eitsupi edited this page Nov 14, 2024 · 6 revisions

Warning

The content of this wiki may be outdated. Please check the Rocker Project website for the most up-to-date information.

By default, docker cannot display GUI windows, such as the native R plot window. This can be addressed by linking the graphics device of the host machine. At this time, this is most straight-forward by linking the X11 graphics system on a linux machine. We are not familiar with nor have tested any workarounds for other operating systems using boot2docker.

Further discussion of this can be found in rocker issue #122

Or, if your goal is just to display plots of R, it may be easiest to use the httpgd package, which was released in 2020.
All you need is the -p option when docker run and install the httpgd package. Please check the httpgd's vignette for the use of the httpgd package in docker containers.

X11

Linux hosts

See this stackoverflow answer for a current work-around. Note the need for the XAuthority file on newer platforms (e.g. ubuntu >= 15.04).

Because this is somewhat verbose, it can be nice to create an alias to set this.

For instance, I put the following in my ~/.bash_aliases file:

## an alias for `docker run` that links the X11 device
alias docker-run-gui='XSOCK=/tmp/.X11-unix && XAUTH=/tmp/.docker.xauth && xauth nlist :0 | sed -e "s/^..../ffff/" | xauth -f $XAUTH nmerge - && docker run  -v $XSOCK:$XSOCK -v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH  -e DISPLAY=$DISPLAY'

## Use that alias in defining an alias that runs R from the hadleyverse container
alias R='docker-run-gui --rm -it -P --user 1000 -v $(pwd):/home/rstudio/`basename $PWD` -w /home/rstudio/`basename $PWD` rocker/hadleyverse R'

The first line creates an alias based on the StackOverflow answer. The second line then defines R as an alias that actually launches a docker container with linked local volume and linked graphics device, running the hadleyverse instance of R. This behaves almost exactly like a local installation of R (X11 plots work, starts R in the working directory and files from that directory are visible to R, doesn't mess up file permissions), with the only exception that files above the working directory are not available (do to linking only the working directory -- we could avoid that by linking the home directory, but I prefer the greater isolation).

Mac OSX Host

Install socat and XQuartz using brew, if not installed already:

brew install socat
brew cask install xquartz

Run the following in a terminal shell (this will run in foreground, so leave running and open a second shell window to continue)

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

Make sure XQuartz is running (may require restart if you've only just installed xquartz(?))

open -a XQuartz

Run the docker container while linking the display variable to your host IP address (e.g. see System Preferences -> Networking or use ifconfig, then run with the addition of the :0 on the IP as follows, e.g.

docker run -e DISPLAY=192.168.1.5:0  --rm -it r-base R -e "capabilities()"

If successful, X11 capability should be TRUE, like so:

> capabilities()
       jpeg         png        tiff       tcltk         X11        aqua 
       TRUE        TRUE        TRUE        TRUE        TRUE       FALSE 
   http/ftp     sockets      libxml        fifo      cledit       iconv 
       TRUE        TRUE        TRUE        TRUE       FALSE        TRUE 
        NLS     profmem       cairo         ICU long.double     libcurl 
      FALSE        TRUE        TRUE        TRUE        TRUE        TRUE 
>