RStudio Server in Docker with Remote Access

For a lot of applications, running RStudio (desktop edition) locally is the best choice. But what if you want to run more complex statistical analysis or machine learning tasks that require more resources. In that case, it might be interesting for you to run a dedicated RStudio server on adequate hardware and docker offers an easy option to quickly setup and run an instance. A possible scenario is that you have remote access to a more powerful computer (Desktop or HPC). Here, I will give a simple recipe that worked great for me.

Prerequisites

I assume that the docker container environment is set up already and you can access the respective machine via ssh. If not, ask your administrator to do this or consult the documentation to find out how you can install and setup docker on your own computer. In my case, it was very easy. I simply installed docker via the package manager of my Linux distribution and started the docker background service. Ready to go!

Setup the Environment

So, let’s log in (ssh) and create a new folder for the RStudio docker configuration and data. I named it RStudio-docker. Within the folder, I created two sub-folders named home/ and packages/. The first directory will serve as a permanent storage location for RStudio projects. The second directory will keep our manually installed R packages.

./RStudio-docker/
├── home/
└── packages/Code language: Bash (bash)

The Docker Command

We will be using the RStudio server image from rocker. Starting an instance with this image is very straight forward. We only have to run the following command within the RStudio-docker/ folder.

docker run --rm -d -p 8787:8787 -e PASSWORD=pword -v $(pwd)/home/:/home/rstudio -v $(pwd)/packages/:/usr/local/lib/R/site-library rocker/rstudioCode language: Bash (bash)

This command will start the RStudio container and use our previously created directories as docker volumes for persistent storage. Of course, change the password ;).

Of course, you can also save this command as a bash script.

Remote Access

As soon as the RStudio container is running, we can access it from any computer in the same network through a browser, such as Firefox. Just type the IP address or hostname followed by the port into the address bar. If you want to access the RStudio interface from the same computer, just type localhost:8787. Log in with the username rstudio and your chosen password.

If you work on a different computer, for instance, a laptop you might also want to have access to the file system. It turned out that sshfs is ideal for that. Just mount the remote RStudio-docker folder with a command in this form

sshfs remoteMachine:/home/user/RStudio-docker /home/user/RStudio-dockerCode language: Bash (bash)

Directly viewing saved plots or copying R-scripts to the RStudio folder works perfectly with that for me.