Skip to content

Instantly share code, notes, and snippets.

@ryanbehdad
Last active March 3, 2021 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbehdad/6f2cf34593e506fa2c8bc59b95f08946 to your computer and use it in GitHub Desktop.
Save ryanbehdad/6f2cf34593e506fa2c8bc59b95f08946 to your computer and use it in GitHub Desktop.
Running RStudio in docker

Running RStudio in docker

Run docker

To start the docker container, run the following command:

docker run -d -p 8787:8787 -v C:\source:/home/rstudio -e ROOT=TRUE -e PASSWORD=rstudio rocker/rstudio

In the above command, I'm mounting "C:\source" to "/home/rstudio", thus providing the container access to all the contents of "C:\source".

If you want to mount multiple paths use -v multiple time. Example:

docker run -d -p 8787:8787 -v C:\source:/home/rstudio -v C:\Temp:/data --name rstudio -e ROOT=TRUE -e PASSWORD=rstudio rocker/rstudio

You can use tidyverse image instead of rstudio:

docker run -d -p 8787:8787 -v C:\source:/home/rstudio -v C:\Temp:/data --name rstudio -e ROOT=TRUE -e PASSWORD=rstudio rocker/tidyverse

Accessing RStudio

To access RStudio, visit localhost:8787 in your browser and log in with username "rstudio" and password "rstudio"

Restarting container after restart

The container won’t start automatically after restarting your machine (Docker). You’ll have to start it again explicitly:

docker start rstudio

To have the container start automatically, you have to add the following option to the run command above:

--restart always

Stopping container

docker stop rstudio

Removing container

docker rm rstudio

Building a docker file

To build a docker file using a specific version of R with RStudio and tidyverse:

  1. Build an R script (install_packages.R) with instructions on the libraries needed.
install.packages("data.table")
install.packages("lubridate")
install.packages("zoo")
install.packages("fst")
install.packages("R.utils")
install.packages("doParallel")
install.packages("devtools")
remotes::install_github("traversc/qs@legacy")
devtools::install_github("tidyverse/multidplyr")
  1. build a Dockerfile with the following content.
FROM rocker/rstudio:3.5.1

RUN mkdir -p /R
ADD install_packages.R /R

RUN apt-get update -qq && apt-get -y --no-install-recommends install \
  libxml2-dev \
  libcairo2-dev \
  libsqlite-dev \
  libmariadbd-dev \
  libmariadbclient-dev \
  libpq-dev \
  libssh2-1-dev \
  unixodbc-dev \
  libsasl2-dev \
  && install2.r --error \
    --deps TRUE \
    tidyverse \
    dplyr \
    devtools \
    formatR \
    remotes \
    selectr \
    caTools \
    BiocManager \
  && Rscript /R/install_packages.R
  1. Run the following command to build the image.

docker build -t aasb9 .

  1. Run the following command to build the container. docker run -d -p 8787:8787 -v C:\source\Nostra:/home/rstudio -v C:\Temp:/data --name a1 -e ROOT=TRUE -e PASSWORD=alskd aasb9

Relevant links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment