Skip to content

Writable Rstudio container

1. Preparing and running a base container

This tutorial utilizes the Rocker project Rstudio server ready container solution. In the new "base" container, there are few additional libraries, that allows easier installation of additional R packages.

rstudio-base.def
Bootstrap: docker
From: rocker/rstudio:4.5.3

%files

%environment
export LC_ALL=C.utf8
export PYTHONNOUSERSITE=True

%post
export LC_ALL=C.utf8
export PYTHONNOUSERSITE=True
export DEBIAN_FRONTEND=noninteractive

# apt packages cached in /tmp
mkdir -p /tmp/apt24 &&  echo "Dir::Cache "/tmp/apt24";" > /etc/apt/apt.conf.d/singularity-cache.conf

apt-get update && apt-get -y dist-upgrade && \
apt-get install -y wget git curl build-essential vim nano less \
                    libz-dev ffmpeg libnss3 libpng16-16t64 \
                    liblzma-dev libbz2-dev libcurl4-openssl-dev \ 
                    libxml2-dev pkg-config \
                    libpng-dev libgsl-dev libglpk40

# Install R libraries
Rscript - << EOF
Sys.setenv(MAKEFLAGS = "-j8")

# Installing with pak installs system dependencies as well
#install.packages("pak")
#pak::pkg_install(c("ggpubr","ggrepel","patchwork","gridExtra","ggplot2","dplyr","tidyr","purrr","readr","readxl","rlang"))

if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install(version = "3.22")

# Install your own R packages here 
#BiocManager::install(c("GenomicFeatures", "AnnotationDbi"))
EOF

%runscript
    exec rserver --www-address=127.0.0.1 --server-user=$(whoami)

You can build the container on Pelle or other computer with compatible hardware and Singularity/Apptainer.

apptainer build rstudio-base.sif rstudio-base.def

Info

The base container is available temporarily on Pelle (and later on Bianca) at /sw/apps/pm-tools/latest/rackham/singularity/rstudio-base.sif.

Warning

Note that the container is running "Ubuntu 24.04" and "gcc 13.3.0", which means that the R packages from the module system on Bianca or Pelle@UPPMAX are incompatible. You need to install/compile everything you need with the tools in the container, in order to use it with this solution.

Danger

Make sure you understand the security complications of running without authentication (less of an issue on Bianca) and the possibility that the port 8787 might be occupied by another instance.

At this point, you can transfer the container to Bianca in your project folder and run it, following the original documentation of the project. Start or reserve a compute node

interactive -A naissXXXX -c 8 
On the compute node, start the RStudio server. Note the different --www-address=0.0.0.0 which will allow you to open a browser to the server directly from the login node (example: http://naissXXXX-b9:8787)
singularity exec \
   --scratch /run,/var/lib/rstudio-server \
   --workdir $(mktemp -d) \
   rstudio-bas.sif \
   rserver --www-address=0.0.0.0 --server-user=$(whoami)

2. (Optional) Writable container - you can install R packages interactively from Rstudio.

I strongly encourage you to install everything you need in the base container in step 1. This optional step will allow you to install packages into the container even if you do not have the necessary rights to build containers.

# Copy the rstudio-bas.sif to a new container
cp rstudio-base.sif rstudio-1GB.sif

# Add 1GB writable overlay in the container
apptainer overlay create --size 1024 rstudio-1GB.sif

On Pelle or another compatible computer with Singularity/Apptainer, start rserver from the container by adding --writable to the original apptainer command.

The packages are installed in the system folder via the writable overlay. All changes are saved in the container and will be preserved and you can transfer the container to Bianca/Maya and use it as it was prepared on Pelle. Remember to run without --writable.

image

3. Advanced

If you want, you can explore more of the benefits of overlays and containers. Here is the original documentation - one can use overlays outside the base container and easily have multiple setups.

# Attach pre-build Bioconductor core utils overlay Bioconductor-core-3.22.img
# BiocManager::install(c("GenomicFeatures", "AnnotationDbi"))
apptainer exec --scratch /run,/var/lib/rstudio-server \
    --workdir $(mktemp -d) \
    --overlay /sw/apps/pm-tools/latest/rackham/singularity/Bioconductor-core-3.22.img \
    rstudio-base.sif \
    rserver --www-address=0.0.0.0 --server-user=$(whoami)

This makes it extremely easy to switch between different setups/versions. All changes will be saved into this overlay, and you need to transfer only the overlay file Bioconductor-core-3.22.img to bring the changes to Bianca/Maya.

  • If you have the overlay integrated into the container, you can still manipulate the content - the overlay got ID 5 in the example below.
    apptainer sif list  rstudio-1GB.sif
    ------------------------------------------------------------------------------
    ID   |GROUP   |LINK    |SIF POSITION (start-end)  |TYPE
    ------------------------------------------------------------------------------
    1    |1       |NONE    |32176-33535               |Def.FILE
    2    |1       |NONE    |33535-38143               |JSON.Generic
    3    |1       |NONE    |38143-39146               |JSON.Generic
    4    |1       |NONE    |40960-976146432           |FS (Squashfs/*System/amd64)
    5    |1       |NONE    |976146432-2049888256      |FS (Ext3/Overlay/amd64)
    
    One can dump the existing layer into a file, delete the existing layer, and add another - please refer to the documentation.
    apptainer sif --help
    
    Usage:
      apptainer sif
    
    Description:
      A set of commands are provided to display elements such as the SIF global
      header, the data object descriptors and to dump data objects. It is also
      possible to modify a SIF file via this tool via the add/del commands.
    
    Options:
      -h, --help   help for sif
    
    Available Commands:
      add         Add data object
      del         Delete data object
      dump        Dump data object
      header      Display global header
      info        Display data object info
      list        List data objects
      new         Create SIF image
      setprim     Set primary system partition