I highly recommend using RStudio if you use R because it makes working with R so much easier. I primarily use RStudio for writing up my analyses in R Markdown. Some RStudio features I couldn’t live without include: Vim keybindings, code completion, and code highlighting (rainbow parentheses are awesome!). Other nice features I like to use include the re-indent code shortcut, insert chunk shortcut, and the file explorer. RStudio v1.4 comes with a Visual R Markdown editor but I haven’t tried it out yet. This post is about how you can use RStudio by running RStudio Server inside a Docker container.
Firstly, why? You can install RStudio on your computer by simply downloading an installation file for your operating system and then you’re done. Why complicate your life with Docker? (If you aren’t familiar with Docker, I gave a workshop on it.) Below are some reasons I have resorted to using RStudio by running RStudio Server inside a Docker container.
1. I was given a Windows computer for work and some R packages don’t work well on Windows.
2. I work in different environments and I wanted the same RStudio installation
3. My computer at home did not have enough compute resources for my RStudio session
There are certain requirements necessary for each of the points above. For point 1. you will need administrator privileges to install and use Docker and not everyone has this privilege. Point 2. is the same as point 1. but for the different environments you work on. For point 3. you will need access to a better computational resource like a compute server or a cloud instance that has Docker installed and where you have permission to use Docker. Unfortunately, having Docker access is the equivalent of gaining root access, so you may not be able to convince your system administrator to install and give you access to Docker. Fortunately for me, I do have Docker access on the server and have a container running RStudio Server on said server that I can access from different computers.
# pull the image docker pull rocker/rstudio:4.0.5 # run container docker run --rm \ -p 8888:8787 \ -e PASSWORD=password \ rocker/rstudio:4.0.5
Now open your favourite browser and type http://localhost:8888/. You should see a login page: using the username “rstudio” and password “password”. And that’s it! You are running RStudio Server inside a Docker container.
Adding to the Docker image
Sometimes installing R packages requires additional libraries. You can build on top of rocker/rstudio:4.0.5 to include the necessary libraries by specifying your own Dockerfile. I have created my own Dockerfile that includes some necessary libraries for common bioinformatics tools and also included some R packages I always use. Below is my Dockerfile.
FROM rocker/rstudio:4.0.5 MAINTAINER Dave Tang <email@example.com> RUN apt-get clean all && \ apt-get update && \ apt-get upgrade -y && \ apt-get install -y \ libhdf5-dev \ libcurl4-gnutls-dev \ libssl-dev \ libxml2-dev \ libpng-dev \ libxt-dev \ zlib1g-dev \ libbz2-dev \ liblzma-dev \ libglpk40 \ libgit2-28 \ && apt-get clean all && \ apt-get purge && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN Rscript -e "install.packages(c('rmarkdown', 'tidyverse', 'workflowr', 'BiocManager'));" RUN Rscript -e "BiocManager::install(version = '3.12')" COPY user-settings /home/rstudio/.rstudio/monitored/user-settings/user-settings COPY .Rprofile /home/rstudio/
You may have noticed the user-settings line in my Dockerfile. As I mentioned at the start of the post, I have some settings that I really like, such as Vim keybindings. Each time we start a new container, we start with the default preferences and naturally I don’t want to manually change them each time. Luckily, the settings are saved in a specific file inside the container: /home/rstudio/.rstudio/monitored/user-settings/user-settings. Start a container and make all your preferred settings and then save the user-settings file back to your local computer. You can overwrite the default settings just like in my Dockerfile above.
Another line you may have noticed in my Dockerfile is the .Rprofile line. My R profile file simply contains the line “.libPaths(“/packages/”)”, which tells R to look for packages in /packages/. When I start my RStudio Server container, I mount a volume that keeps all my R packages from local to the container. This way I don’t need to re-install packages each time I start a new container. Below is an example:
docker run --rm \ -p 8888:8787 \ -d \ --name rstudio_server \ -v /home/dtang/r_packages/:/packages \ -e PASSWORD=password \ -e USERID=$(id -u) \ -e GROUPID=$(id -g) \ davetang/rstudio:4.0.5
I run the container in detached mode (“-d”), so if you run your container this way, make sure you stop the container when you’re done by running “docker stop container_name”.
I wrote a helper script to run my Docker image. It uses my Docker image that was built using the Dockerfile shown in this post. You can specify directories to mount to the script and it will mount them to /data/ inside the container.
You can limit the resource usage of your Docker container if you’re running your container in a shared environment and want to make sure you don’t use all the resources.
If you are running your Docker container on a server and want to access it on your local computer, you can use SSH port forwarding.
# -N Do not execute a remote command. This is useful for just forwarding ports # -f Requests ssh to go to background just before command execution # -Y Enables trusted X11 forwarding # -L Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port ssh -N -f -Y -L 8888:localhost:8888 firstname.lastname@example.org
I have more notes in my GitHub repo, so check it out if you’re interested.
This work is licensed under a Creative Commons
Attribution 4.0 International License.