For running RStudio Server in a container, I've written about how to run RStudio Server with Docker; in the post I also listed out some reasons why you would want to do that instead of simply installing RStudio on your computer. This post demonstrates one way of running RStudio Server with Singularity. I learned that this was a possibility from an ex-colleague (Charles). For those unfamiliar with Singularity, the Nextflow docs has a nice description:
Singularity is a container engine alternative to Docker. The main advantages of Singularity is that it can be used with unprivileged permissions and doesn't require a separate daemon process. These, along other features, like for example the support for autofs mounts, makes Singularity a container engine better suited the requirements of HPC workloads. Singularity is able to use existing Docker images, and pull from Docker registries.
One main advantage, as noted above, is that unprivileged users (i.e. non-root) can run containers. Docker has a rootless mode that also allows non-root users to run containers but I've been having problems using it, hence my preference for Singularity. (For example, I can't use the Rocker Project's R images using Docker in rootless mode. This is a substantial problem because many projects and people use the Rocker images, like Bioconductor. The problem seems to be that Docker's rootless mode removes the rstudio user, which the Rocker images requires. Perhaps I can work around that but I've had problems with other Docker images too.)
The implementation of using Singularity to run RStudio Server in this post is based off the work done by Charles. Take a look at that repository to see which implementation you prefer. I use a Docker image from the Rocker Project (rocker/verse:4.3.2) to bootstrap my build where as Charles uses a Debian image, which requires more setting up in the Singularity Definition File.
For this post, I'm using Debian 11 (bullseye) and singularity-ce version 3.11.3. To get started, clone my learning Singularity repo:
git clone https://github.com/davetang/learning_singularity.git
cd learning_singularity/rstudio/
Next, build the Singularity image:
./build.sh
# snipped
# INFO: Adding environment to container
# INFO: Adding runscript
# INFO: Creating SIF file...
# INFO: Build complete: rstudio_server_verse_4.3.2.sif
Hopefully you see the Build complete message. Now, run a container using the run script.
./run.sh
# RStudio Username: dtang
# RStudio Password: password
# Port: 8888
# TTY detected. Printing informational message about logging configuration. Logging configuration loaded from '/etc/rstudio/logging.conf'. Logging to 'syslog'.
Now head over to http://localhost:8888 or substitute localhost with the IP address of the machine running the container and you should come to the Sign in to RStudio page. Enter the credentials (your username will be different) outputted from the run script.
You should now see RStudio!
Note that packages will be installed to /home/rstudio as shown above by the .libPaths() function. This directory is mounted to $HOME/rstudio on your host (done by run.sh), so when you install a package, it will be installed there.
To stop the container, go back to the terminal and hit <ctrl+c>. And that's it!
For more information, check out Charles's repo and my repo or leave a comment here!

This work is licensed under a Creative Commons
Attribution 4.0 International License.


If you like it and work with an HPC cluster where your favourite tools are not installed you can also check https://github.com/oist/BioinfoUgrp_UnixGoodies_Images
Thanks for sharing! You may also be interested in: https://github.com/mhoye/moderntools