3 min read

Running RStudio in Browser with Docker Containers

Running RStudio in Browser with Docker Containers
The Rocker Project

Why Use R in Containers?

  • Solves system library conflicts - Avoid BLAS/LAPACK mismatches, conflicting system dependencies
  • Consistent everywhere - Same environment locally, in CI/CD, and production, sharing the exact setups and dependencies
  • No more Windows installation hell

The Rocker Project

The Rocker project provides a widely-used suite of Docker images with customized R environments for particular tasks. It includes but not limited to:

  • rocker/r-ver - Base image of R compiled from source
  • rocker/rstudio - Adds RStudio Server
  • rocker/tidyverse - Adds tidyverse
  • rocker/cuda - Adds CUDA support to rocker/r-ver
The Rocker Project – Rocker Project
Docker Containers for the R Environment
The Rocker Images – Rocker Project
Choose a container for your needs.

Prerequisite

Docker / Podman installation on WSL / MacOS / Linux.

Running R Scripts in Container

Let’s say the script is stored in ~/myworkspace/hello.R .

cat ~/myworkspace/hello.R
"Hello World!"
docker run --rm -it -v ~/myworkspace:/workspace -w /workspace  rocker/r-ver Rscript hello.R
[1] "Hello World!"

Breakdown of the command:

  • --rm - automatically removes the container when it exits
  • -it - use an interactive terminal
  • -v ~/myworkspace:/workspace - mount local directory to container directory
  • -w /workspace - set the working directory inside the container
  • rocker/r-ver - the R Docker Image
  • Rscript hello.R - command to run R script

Running R Studio in Container

TLDR

Docker run the container with port mapping, then go to browser at localhost:8787 to access.

docker run --rm -it -p 8787:8787 rocker/rstudio

Authentication

By default, a non-root user rstudio is set as RStudio Server user. A password will be randomly generated and displayed in the console (or in Docker container log) when starting the RStudio Server. When login, enter the username rstudio and the password.

To use a custom password instead of random password, override the PASSWORD environment variable.

To add default non-root rstudio user to sudoers group for commands like apt update, set ROOT to true .

docker run --rm -ti -e PASSWORD=nopassword -e ROOT=true -p 8787:8787 rocker/rstudio

Disable Authentication

To skip RStudio login authentication form, set the DISABLE_AUTH environment variable to true.

However, when using sudo command, password is still required.

In addition, if on a shared environment (e.g. on servers), disabling authentication is strongly discouraged.

UID & GID

When mapping a volume for

The UID and GID of the default non-root user can be changed by overriding USERID and GROUPID environment variable respectively.

This is important when mapping volume to the container to ensure proper read/write/execute permission settings.

docker run --rm -ti -e USERID=$(id -u) -e GROUPID=$(id -g) -p 8787:8787 rocker/rstudio

Volume Mapping

  1. Recent RStudio Server’s configuration files are saved in the ~/.config/rstudio/ directory
  2. It is not recommended to bind-mount whole home directory on the container (/home/rstudio); RStudio Server may not work properly.
  3. Since RStudio Server opens the user’s home directory (/home/rstudio) by default, it is easier to use if a working directory is set up under /home/rstudio, e.g. /home/rstudio/workspace.

Sample Docker Run Command

docker run --rm -it \\
	-p 8787:8787 \\
	-e USERID=$(id -u) -e GROUPID=$(id -g) \\
	-e PASSWORD=nopassword -e ROOT=true \\
	-v ~/myproject/config:/home/rstudio/.config/rstudio \\
	-v ~/myproject/workspace:/home/rstudio/workspace \\
	-v ~/mnt/data:/data \\
	rocker/rstudio:latest

To find out more about Rocker images:

rstudio, tidyverse, verse, geospatial – Rocker Project

Install More Packages

To install more packages, either temporally modify the container by running commands as root user, or build new Docker image from the base image.

FROM rocker/tidyverse:4.3.2

RUN install2.r --error \
    --deps TRUE \
    plotly \
    && rm -rf /tmp/downloaded_packages/