
- Docker - Home
- Docker - Overview
- Docker - Installing on Linux
- Docker - Installation
- Docker - Hub
- Docker - Images
- Docker - Containers
- Docker - Registries
- Docker - Compose
- Docker - Working With Containers
- Docker - Architecture
- Docker - Layers
- Docker - Container & Hosts
- Docker - Configuration
- Docker - Containers & Shells
- Docker - Dockerfile
- Docker - Building Files
- Docker - Public Repositories
- Docker - Managing Ports
- Docker - Web Server
- Docker - Commands
- Docker - Container Linking
- Docker - Data Storage
- Docker - Volumes
- Docker - Networking
- Docker - Security
- Docker - Toolbox
- Docker - Cloud
- Docker - Build Cloud
- Docker - Logging
- Docker - Continuous Integration
- Docker - Kubernetes Architecture
- Docker - Working of Kubernetes
- Docker - Generative AI
- Docker - Hosting
- Docker - Best Practices
- Docker - Setting Node.js
- Docker - Setting MongoDB
- Docker - Setting NGINX
- Docker - Setting ASP.Net
- Docker - Setting MySQL
- Docker - Setting Go
- Docker - Setting Rust
- Docker - Setting Apache
- Docker - Setting MariaDB
- Docker - Setting Jupyter
- Docker - Setting Portainer
- Docker - Setting Rstudio
- Docker - Setting Plex
- Docker Setting - Flame
- Docker Setting - PostgreSql
- Docker Setting - Mosquitto
- Docker Setting - Grafana
- Docker Setting - Nextcloud
- Docker Setting - Pawns
- Docker Setting - Ubuntu
- Docker Setting - RabbitMQ
- Docker - Setting Python
- Docker - Setting Java
- Docker - Setting Redis
- Docker - Setting Alpine
- Docker - Setting BusyBox
- Docker Setting - Traefik
- Docker Setting - WordPress
- Docker Useful Resources
- Docker - Quick Guide
- Docker - Useful Resources
- Docker - Discussion
Docker - Setting Rstudio
RStudio is an integrated development environment (IDE) for R, a programming language popular for statistical computing and graphics. It provides a comfortable UI for developing R, including an editor for syntax highlighting, plotting, history, debugging, and workspace management.
Running RStudio in a Docker container offers several advantages: RStudio will be consistent across different development environments. Docker minimizes the hassle of managing dependencies, and sharing R projects and their environments is much easier. The RStudio Dockerization allows you to insulate an R development environment from your system, avoiding conflicts among different project dependencies.
Prerequisites for Dockerizing RStudio
Before you start Dockerizing RStudio, ensure that the following prerequisites are met −
- Docker Installed − Make sure Docker is installed on your machine. You can download it from the official Docker website and follow the installation instructions regarding your operating system.
- Docker Basics − A basic understanding of Docker commands, Dockerfile, and Docker Container concepts is needed for a smooth walk-through of this tutorial.
- RStudio − Though not required to be installed locally, some hands-on experience on the RStudio will help understand how this works internally inside a Docker container.
- R Programming Knowledge − Little familiarity with the R programming language and how RStudio interacts with it.
- RStudio-Server Image Access − We will be pulling an official image from Docker Hub, so make sure you have access to pull images from Docker Hub.
Once you have these prerequisites ready, you are all set to begin the setup and Dockerization of RStudio to have a seamless and constant reproducible R environment.
Setting up an RStudio Project
Let's set up a basic RStudio project that we can run inside a Docker container. Well create a directory structure for our project thatll include the necessary files needed by RStudio to work within Docker.
Create a Project Directory
Lets create a new directory for our RStudio project.
$ mkdir rstudio-docker-project $ cd rstudio-docker-project

Initialize Your RStudio Project
Inside this project directory, lets initialize an RStudio project by creating a new .Rproj file. This will help RStudio recognize the project environment when we run the container.
$ touch my_project.Rproj

Add Sample R Scripts
Lets create a few R scripts or data files inside our project directory. For example, well create a simple hello_world.R script with below contents −
# hello_world.R print("Hello, Dockerized RStudio!")
Project Structure and Dependencies
Lets understand a typical RStudion project structure and dependencies.
Project Structure
The RStudio project directory can have a simple, organized structure, such as −
rstudio-docker-project/ my_project.Rproj # RStudio project file Dockerfile # Dockerfile to create the RStudio image hello_world.R # Sample R script data/ # Directory for datasets sample_data.csv scripts/ # Additional R scripts analysis.R
Dependencies
R Packages − We should always list any R packages needed for our project in a separate R script (e.g., install_packages.R). For example −
# install_packages.R install.packages(c("dplyr", "ggplot2", "tidyverse"))
System Dependencies − If the R scripts require system-level dependencies (e.g., specific libraries), we can include them later on in the Dockerfile.
RStudio Configuration − Moreover, any other RStudio settings or configurations (e.g., user profiles, R environment variables) can also be included in the Docker setup.
For simplicity, in this chapter, well just create the hello_world.R file that prints a simple message.
Running RStudio in Local
We can run RStudio locally on our machine before Dockerizing it. This will ensure everything works as it should. Itll help validate project dependencies and the project structure.
Install R and RStudio
If you have not installed it on your local machine, download and install recent versions of the environment and RStudio from:
- R − The R Project for Statistical Computing
- RStudio − Integrated Development for R
Open RStudio Project
Open RStudio and then open your project from within the RStudio interface by selecting the `.Rproj` file (`my_project.Rproj`). This will make sure RStudio recognizes that you've set up a project.
Dependency Check
Please ensure you run your scripts on your local machine so that you don't miss installing any packages/dependencies. For example, you could try running the `hello_world.R` script we created earlier −
source("hello_world.R")
If after running the script, there are no error messages and the output is as expected, then the local setup is correct.
Install Additional Packages
If there is a requirement for specific R packages for your project, install them directly from the RStudio console. For example −
install.packages(c("dplyr", "ggplot2", "tidyverse"))
Run a Test Analysis
Run a simple test analysis or a visualization to ensure all works as expected. This may be running sample data through your analysis script (`scripts/analysis.R`) or testing package functionality −
library(ggplot2) ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() + ggtitle("Sample Plot: Weight vs. MPG")
Creating the Dockerfile
Now that weve our RStudio project set up and running locally, lets create a Dockerfile.
Steps to Create the Dockerfile
1. Create the Dockerfile − Lets create a file named Dockerfile inside our project directory (rstudio-docker-project).
$ touch Dockerfile
2. Choose the Base Image − Lets specify the base image for RStudio. Well use the official RStudio Server image from Rocker (a popular set of Docker images for R).
# Use the official RStudio Server base image FROM rocker/rstudio:latest
3. Set Environment Variables − Now, lets define the environment variables that RStudio needs. This includes setting up a default user and password for RStudio Server access. For example:
# Set environment variables for RStudio user ENV USER=rstudio ENV PASSWORD=yourpassword
4. Install R Packages − Lets include an instruction to run an R script to install R packages when we build the image −
# Install required R packages RUN R -e "install.packages(c('dplyr', 'ggplot2', 'tidyverse'), repos='http://cran.rstudio.com/')"
5. Copy Project Files − Now, lets copy our RStudio project files into the Docker image.
# Copy the entire project directory into the Docker image COPY . /home/rstudio/rstudio-docker-project WORKDIR /home/rstudio/rstudio-docker-project
6. Expose Ports − RStudio Server typically runs on port 8787. We can expose this port in our Dockerfile to allow access to the RStudio Server −
# Expose the default RStudio Server port EXPOSE 8787
Complete Dockerfile Example
# Use the official RStudio Server base image FROM rocker/rstudio:latest # Set environment variables for RStudio user ENV USER=rstudio ENV PASSWORD=yourpassword # Install required R packages RUN R -e "install.packages(c('dplyr', 'ggplot2', 'tidyverse'), repos='http://cran.rstudio.com/')" # Copy the entire project directory into the Docker image COPY . /home/rstudio/rstudio-docker-project WORKDIR /home/rstudio/rstudio-docker-project # Expose the default RStudio Server port EXPOSE 8787
Building the RStudio Image
Navigate to Project Directory − Navigate to the project directory that has the Dockerfile.
$ cd rstudio-docker-project
Build the Docker Image − We can use the docker build command to create the Docker image.
$ docker build -t rstudio-docker-image .
- -t rstudio-docker-image − Tags the image with the specified name.
- .: It specifies the current directory as the build context.

We can verify whether the image was created or not, using the docker images command to list all the images.
$ docker images

Running the RStudio Docker Container
Run the Docker Container − Now that we have our Docker image ready, lets run a container.
$ docker run -d -p 8787:8787 --name rstudio-container rstudio-docker-image
- -d − Runs the container in detached mode (in the background).
- -p 8787:8787 − Maps the container's port 8787 to your host's port 8787 for accessing RStudio Server.
- --name rstudio-container − Names the running container.
- rstudio-docker-image − Specifies the image to use for the container.

Access RStudio − You can access the RStudio Server by opening a web browser and navigating to http://localhost:8787.

You can use the username rstudio and the password you set in the Dockerfile.

Conclusion
In this chapter, we learned how to Dockerize RStudio, starting from the creation of a project to the writing of a Dockerfile, building the Docker image, and running the RStudio Server from within a Docker container.
By Dockerizing RStudio, we can create a portable, consistent, and reproducible R development environment that seamlessly runs across different platforms.
You can further extend your RStudio Docker setup by adding additional tools, persistence data volume configuration, or deploying your RStudio Server on cloud platforms to increase accessibility and scalability.
FAQs on Dockerizing RStudio
We have collected here a set of Frequently Asked Questions on how to dockerize RStudio. Please check the following FAQs −
1. How do I connect to a Dockerized RStudio Server?
To access the Dockerized RStudio Server, open a web browser and point it to the IP address of the Docker host at the specified port. This may involve port forwarding or network settings depending on your environment.
2. How do I install additional R packages in a Dockerized RStudio Server?
You can install additional R packages by using the install.packages() function from within R. You can add these installation commands of packages into your Dockerfile or execute them interactively inside the RStudio environment. Make sure that the packages are compatible with the R version in your base image.
3. How do I configure RStudio Server to use a specific R version within the Docker container?
To set up the RStudio Server to work with a specific R version, choose the base image version that has the desired R version. You can also install additional versions of R in the container using package managers or other ways supported by the base image.