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

shiny_apps

R Notebooks

helicopters

Learnr tutorials

Make your own code acadamy type of courses.

learnr

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.

rstudio_cloud

API with Plumber

plumber

Host models

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

model

Host models

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

election

What are your options?

  1. Hosting Rmarkdown for free: Github Pages
  2. Hosting Shiny apps (not free): shinyapps.io

shinyio

Host it yourself

Digital Ocean for Personal Use and AWS at Work

Digital Ocean

AWS (or whatever your news org uses)

Steps for setting up an R server on Digital Ocean

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

shinyio

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

  1. Dockerfile - Describes the steps needed to create an environment. This is the recipe.
  2. 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.
  3. Registry - Stores built images, so that others can use them. This is akin to a liquor store.
  4. 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

  1. You can easily pour many “replicas” of the same beer.
  2. 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.
  3. As a result, the same bartender can offer many different types of beers

docker

Layers in a Docker Container

  1. Base Operating System
  2. System Dependencies
  3. R
  4. R Packages
  5. Code
  6. Data

Base Operating System

FROM rocker/shiny

System Dependencies

docker_sys

The directory

files

R packages, Code and Data

docker_code

docker-compose.yml

docker_yml

Run Docker locally

  1. Install Docker
  2. Navigate to folder
  3. Build out project
$ docker build -t docker_sample_project .

Run Docker locally

  1. First time building will take a while

packages

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.

docker_browse

Host docker locally

shiny_local

Host docker online

Upload repo to Github

github

Sign up for an account on hub.docker.com

docker

Create a Docker Repository

github

Connect your Github and Docker repos

Let it start building.

github

Builds now connected to Github activity

docker_build_online

Create a Docker Droplet on Digital Ocean

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

shiny_local

Create a Docker Droplet

signup

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

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).

final

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

That’s it!

docker_file