repo: github.com/andrewbtran/docker_sample_project
slides: andrewbtran.github.io/docker_sample_project/presentation/docker_and_r.html
doc: andrewbtran.github.io/docker_sample_project/presentation/docker_and_r.html
What’s the point of an R server?
You can host:
Shiny apps

R Notebooks

Learnr tutorials
Make your own code acadamy type of courses.

Rstudio Cloud
Spend a few dollars to spin up a server with 32 cores and 192 GB of ram for a few hours for complex data analysis.

API with Plumber

Host models
Washington Post Python data munging + R modeling + Plumber API = Live election predictions

Host models
Washington Post Python data munging + R modeling + Plumber API = Live election predictions

Host it yourself
- Digital Ocean
- Heroku
- AWS
- Google Cloud Run
Digital Ocean for Personal Use and AWS at Work
Digital Ocean
AWS (or whatever your news org uses)
- Engineering experts to help
- Can host internally
Steps for setting up an R server on Digital Ocean
- Create a Droplet for your project
- Choose an image wiith at least 1GB of ram
- Choose a plan
- Add block storage
- Choose data center
- Select SSH keys for authentication
- Choose a hostname for your droplet
- Assign a floating IP address
- Point your domain to the floating IP address
More specific instructions here
Steps for setting up an R server on Digital Ocean
- Access Droplet via Command Line
- Configure proper SSH credentials via root access
- More SysAdmin tasks
- Set up non-root user with ssh login that has sudo privileges
- Set up SSL certificates
- Write the configuration Caddyfile for ports, proxies, websockets, etc)
Steps for setting up an R server on Digital Ocean
- More SysAdmin tasks to install R
- from the command line, download and install R
$ sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
$ sudo apt update
$ sudo apt install r-base
More specific instructions here
Move data over and R scripts
Create/Organize your folders through the command line
Copy a local file to your server:
$ scp depends.R remote_username@10.10.0.2:/remote/directory
$ Rscript depends.R
Set up the server based on what you want
Shiny server
- Install Nginx
- Install the shiny packages
- Set up appropriate ports to listen/forward
Rstudio cloud
- Install the Rstudio package
- Create users for logging in to the service
Simplifying the process with Docker
Brewing and drinking beer
- Dockerfile - Describes the steps needed to create an environment. This is the recipe.
- Image - When you execute the steps in a Dockerfile, you build the Dockerfile into an image which contains the environment you described. This is the batch of beer.
- Registry - Stores built images, so that others can use them. This is akin to a liquor store.
- Container - At a specific moment, you can start a container from the image, which amounts to running a process in the built environment. This is drinking a pint from the batch of beer.
Simplifying the process with Docker
- You can easily pour many “replicas” of the same beer.
- The bartender (a server, in computer terms), is decoupled from the beer we want - we don’t have to go to the brewer and brew a new beer each time we want a pint.
- As a result, the same bartender can offer many different types of beers
Layers in a Docker Container
- Base Operating System
- System Dependencies
- R
- R Packages
- Code
- Data
Base Operating System
FROM rocker/shiny
R packages, Code and Data
Run Docker locally
- Install Docker
- Navigate to folder
- Build out project
$ docker build -t docker_sample_project .
Run Docker locally
- First time building will take a while
Host docker locally
Last line in my dockerfile:
EXPOSE 3838
In the command line:
docker-compose down && docker-compose rm && docker-compose up --force-recreate --build
Host docker locally
You can quickly figure out the local hosting address through your Docker Desktop App.
Host docker online
Upload repo to Github

Sign up for an account on hub.docker.com

Create a Docker Repository

Connect your Github and Docker repos
Let it start building.

Builds now connected to Github activity

Create a Docker Droplet on Digital Ocean
Or follow the instructions on whatever hosting platform (Heroku, AWS, Cloud Run) you use to install Docker.

Create a Docker Droplet

SSH into server
$ ssh root@xxx.xx.xxx.x
Deploy docker container
Make sure the R server processes are running after ending ssh session
$ tmux
$ docker run --rm -p 80:3838 abtran/docker_sample_project:latest
Deploy docker container
Carefully exit the server.
Leave/detach the tmux session by typing Ctrl+b and then d
Visit your page online (in your hosting you can point it a domain name, etc).
Need to manually re deploy after every Github update
If you want to update later, ssh into your server and type
$ tmux attach
$ docker run --rm -p 80:3838 abtran/docker_sample_project:latest
Leave/detach the tmux concurrent session by typing Ctrl+b and then d