Using docker to deploy an R plumber API

Get your R code to run anywhere as a service

Jacqueline Nolis
Nov 6, 2018 · 10 min read

The ☁️cloud☁️ and virtual machines

As a data scientist, you sometimes want to have code running in places that are not your computer. In our case, we want to have our R script run an API continuously, regardless of if our laptop runs out of battery.

  1. Install the correct version of R.
  2. Install the right R libraries, some of which may come from GitHub.
  3. Put the R scripts for your plumber API on the instance.
  4. Open port 80 within the virtual machine’s firewall.
  • Virtual machines take a lot of computing resources. They are entirely simulated computers, so they require an entire OS running and all the programs that come with it.
Docker will leave you feeling as calm and serene as this photo (copyright of flickr user stanimir.stoyanov)

Enter Docker

Docker is a way to make the process of configuring and running computers smoother. With docker, you can create a single document that specifies how to set up the computer. The document lets you run these steps at a moments notice. More formally, docker is a way to run virtual machines as containers, which are lightweight executable packages of software. A container is an instance of an image, which is a snapshot of a computer at a moment in time. Images can be full snapshots, or they can just be a small addition to an earlier image. A dockerfile is the specification document for how to build the image.

  • An image is a file that’s a snapshot of that computer. Each step of the docker file constructs a new image. That image is used as the starting place for the next step in the docker file.
  • A container is when you take an image file and begin running it. Running an image creates a container.

Using Docker for R and plumber

We’ll use the API we created in part 1 of this series as the code we want in our docker container. You can find the R code in our GitHub repository. Let’s try making a container out of it and running it on our own machine. In our case we’ll want to set up a computer following roughly the steps above:

  1. Install R on it.
  2. Install our R libraries.
  3. Transfer our R scripts to the computer.
Building our beautiful R plumber API image from other, smaller images
Your new best nautical friend
docker pull rocker/r-ver:3.5.0
docker run -ti rocker/r-ver:3.5.0 
FROM rocker/r-ver:3.5.0
RUN apt-get update -qq && apt-get install -y \
libssl-dev \
libcurl4-gnutls-dev
RUN R -e "install.packages('plumber')"
COPY / /
EXPOSE 80
ENTRYPOINT ["Rscript", "main.R"]
docker build -t plumber-demo .
docker run --rm -p 80:80 plumber-demo
docker stop $(docker ps -a -q)

  • You’ll want to make the container as small as reasonably possible
  • You definitely want your web service to support encryption via HTTPS, which isn’t supported by plumber.
  • You should make the dockerfile something that you never have to touch (so remove things like the list of R libraries to install).

T-Mobile Tech

turning telecoms on its head (and looking way cool while doing it)

Jacqueline Nolis

Written by

Professional data scientist, amateur software developer, really excited about functional programming

T-Mobile Tech

turning telecoms on its head (and looking way cool while doing it)