## Warning: package 'knitr' was built under R version 4.1.2

Lesson Objectives

  • Launch RStudio inside of a Docker container
  • Linking a volume to a Docker container
  • Load into the container plotting it


First things first: install Docker. The install guide links to a bunch of introductory material after installation is complete; it’s not necessary to complete those tutorials for this lesson, but they are an excellent introduction to basic Docker usage.

Launching RStudio in Docker

The first thing we need to do to launch Docker is to open a Unix Shell. If you’re on Mac or Windows, in the last step you installed something called the Docker Quickstart Terminal; open that up now - it should look like a plain shell prompt (~$), but really it’s pointing at a linux virtual machine that Docker likes to run in, and this is where you should do everything for the rest of this tutorial unless otherwise noted. If you’re on a linux machine, then you can use a plain old terminal prompt.

On a Mac you can also go to your terminal of choice and configure it for Docker usage. Especially if you get the error Cannot connect to the Docker daemon. Is the docker daemon running on this host? at some point in the tutorial, running the following command might fix your problem:

eval "$(docker-machine env default)"

Next, we will ask Docker to run an image that already exists, we will use the verse Docker image from Rocker which will allow us to run RStudio inside the container and has many useful R packages already installed. You need to set a password for RStudio using the -e environment flag. You will be asked to enter this when the container launches.

docker run --rm -p 8787:8787 -e PASSWORD=YOURNEWPASSWORD rocker/verse

Optional: *p and --rm are flags that allow you to customize how you run the container. p tells Docker that you will be using a port to see RStudio in your web browser (at a location which we specify afterwards as port 8787:8787). Finally, –rm ensures that when we quit the container, the container is deleted. If we did not do this, everytime we run a container, a version of it will be saved to our local computer. This can lead to the eventual wastage of a lot of disk space until we manually remove these containers. Later we will show you how to save your container if you want to do so.

If you try to run a Docker container which you have not installed locally then Docker will automatically search for the container on Docker Hub (an online repository for docker images) and download it if it exists.*

The command above will lead RStudio-Server to launch invisibly. To connect to it, open a browser and enter http://, followed by your ip address, followed by :8787. If you are running a Mac or Windows machine, you will find the ip address on the first line of text that appeared in your terminal when you launched the Docker Quickstart Terminal. For example, you should see:

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/

docker is configured to use the default machine with IP
For help getting started, check out the docs at https://docs.docker.com

Thus, you would enter in your browser as the url.

If you are running a Mac or Linux machine, you can use localhost as the ip address. For example: http://localhost:8787

This should lead to you being greeted by the RStudio welcome screen. Log in using:

username: rstudio password: YOURNEWPASSWORD

Now you should be able to work with RStudio in your browser in much the same way as you would on your desktop. The password you should use is the one you created when you used docker run above.

The image below shows RStudio server running within a docker image. You should see something similar on your machine.