RStudio Environment on DigitalOcean with Docker
I’ll be running a training course in a few weeks which will use RStudio as the main computational tool. Since it’s a short course I don’t want to spend a lot of time sorting out technical issues. And with multiple operating systems (and versions) these issues can be numerous and pervasive. Setting up a RStudio server which everyone can access (and that requires no individual configuration!) makes a lot of sense.
These are some notes about how I got this all set up using a Docker container on DigitalOcean. This idea was inspired by this article. I provide some additional details about the process.
I began by trying things out on my local machine. The first step was to install Docker. On my Linux machine this was a simple procedure. I added my user to the
docker group and I was ready to roll.
Being my first serious foray into the world of Docker I spent some time getting familiar with the tools. First it makes sense to validate that Docker is correctly configured and operational. Check the version.
Check the current status of the Docker service. This should indicate that Docker is loaded, running and active.
To see further system information about Docker:
Finally run a quick test to ensure that Docker is able to download and launch images.
A selection of RStudio Docker containers are hosted by the Rocker project. We’ll install the verse container which contains base R, RStudio, tidyverse, devtools and some packages related to publishing.
That will download a load of content. Depending on the speed of your connection it might take a couple of minutes. Once the downloads are complete we can spin it up.
Now point your browser at http://localhost:80/. You should see a login dialog. Login with username
rstudio and password
Once you’ve satisfied yourself that the RStudio server is working properly, we’ll shut it down. Check on the running Docker containers.
The ID in the output from the previous command is used to stop the container.
Creating a New Container Image
We’re now going to create a custom Docker image based on the
rocker/verse image we used above. We do this by creating a
Dockerfile. You can take a look at the one that I am using in this GitHub repository. It adds a few minor features to the
- a small shell script for generating new user profiles;
whoispackage for the
apgcommand (although I am currently using
opensslfor password generation); and
- a few extra R packages.
Check out the best practices for creating a Dockerfile.
We need to build the image before we can launch it. Navigate to the folder which contains the
Dockerfile and then do the following:
That will step through the instructions in the
Dockerfile, building up the new image as a series of layers. We can get an idea of which components contributed the most to the resulting image.
We can now test the new container.
Once you are satisfied that it works, stop the container.
Deploy on DigitalOcean
We’re now in a position to deploy the image on DigitalOcean. If you don’t already have an account, go ahead and create one now,
Create a Droplet
Once you’ve logged in to your DigitalOcean account, create a new Droplet and choose the Docker one-click app (I chose Docker 17.06.0-ce on 16.04). Make sure that you provide your SSH public key.
Connect as root
Once the Droplet is live (give it a moment or two, even after it claims to be “Good to go!”), use the IP address from the DigitalOcean dashboard to make a SSH connection. You’ll connect initially as the
Docker containers use the kernel, memory and swap from the host. So if you’ve created a relatively small Droplet then you might want to add swap space.
docker user and add it to the
Add your SSH public key to
.ssh/authorized_keys for the
docker user. Terminate your
root connection and reconnect as the
Build the Container
Clone the GitHub repository. Navigate to the folder which contains the RStudio
Dockerfile. Now build the image on the Droplet.
And then launch a container.
Connect to the Droplet using the IP address from the DigitalOcean dashboard.
Sign in using the same credentials as before. Sweet: you’re connected to an instance of RStudio running somewhere out in the cloud.
Accessing Usernames and Passwords
Obviously the default credentials we’ve been using are a security hole. We’ll need to fix that. We’ll also need to create a brace of new accounts which we can give to the course delegates. These new accounts need to be created on the container not the host!
To accomplish all of this we’ll need to connect to the running Docker container. Again use
docker ps to find the ID of the running container. Then connect a
bash shell using
docker exec, providing the container ID as the
Now create some new users using the
generate-users.sh scripts packaged with the image. For example, to generate five new users:
This will create the user profiles and home folders. The usernames and passwords are dumped to the terminal in CSV formay. Record these and then assign a pair to each of the course delegates.
Persisting User Data
You’ll probably want to use a mechanism for persisting user data. There are a couple of options for doing this. A simple technique which I have found helpful is documented here.
Finish and Klaar
Feel free to fork the repository and customise the
Dockerfile to suit your requirements. Let me know how this works out for you. I’m rather excited to run a course which will not be plagued by technical issues!