Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Docker containers in R

Using Docker containers in R

Docker containers are everywhere nowadays, and there are many use cases for containerizing R code. In this deck, I go over the benefits of using containers based on examples from the R universe, and share a few tips and tricks for efficient Docker based workflows.

Tamas Szilagyi

August 14, 2018

More Decks by Tamas Szilagyi

Other Decks in Technology


  1. 2 whoami @tudosgar | tamaszilagyi.com • EX - Sr. Analyst

    ING • blog: http:/ /tamaszilagyi.com/ • love hiking!
  2. 3 • Docker Basics • Our first Docker container: 2

    usecases • Best practices, tools and resources Agenda @tudosgar | tamaszilagyi.com
  3. 6 @tudosgar | tamaszilagyi.com Common problems • Different OS •

    Missing system-level dependencies • Missing packages • Different versions • Missing keys • …
  4. @tudosgar | tamaszilagyi.com Infrastructure Host OS Docker engine app B

    bin / lib bin / lib app A Infrastructure Host OS Hypervisor guest OS guest OS bin / lib bin / lib app A app B Containers Virtual Machines Unlike VM’s, containers are not “real computers”, just a combination of Linux kernel features that isolate what the processes inside can see and use.
  5. Benefits @tudosgar | tamaszilagyi.com • OS independent • Your code

    is all of a sudden portable • Your analysis is reproducible • Consistency in production • Prototype -> deployment is faster • Relatively lightweight footprint
  6. 15 @tudosgar | tamaszilagyi.com A shiny app consists of two

    components: 1. A ui.R that defines the frontend logic. Shiny App assets
  7. 16 @tudosgar | tamaszilagyi.com A shiny app consists of two

    components: 1. A ui.R that defines the frontend logic. 2. And a server.R for the backend. Shiny App assets
  8. 18 @tudosgar | tamaszilagyi.com The Dockerfile is a text file

    that contains all the instructions on how to build and assemble the image. Each is instruction is a layer. We start with a (larger) read-only layers, this is our base image. Everything ]after are (small) read-write layers. read-only base layers read-write layers Write a Dockerfile!
  9. 19 @tudosgar | tamaszilagyi.com The base image contains the read-only

    layers and is preceded by FROM. Specifically for R, the rocker project (https:/ /www.rocker- project.org/images/) maintains many useful images, for example for shiny apps. The read-write layers are what we add, and here we simply COPY our app into the relevant folder inside the container. Dockerfile
  10. 22 Interlude: docker commands @tudosgar | tamaszilagyi.com • docker build

    builds a docker image from our Dockerfile. • docker run instantiates the container from our image. • docker ps list (running) containers • docker kill terminates a container. • docker rm deletes a container. • docker login login to Dockerhub (to upload our image). • docker push uploads the image back to Dockerhub. • docker pull pulls an image from the registry (Dockerhub).
  11. 24 @tudosgar | tamaszilagyi.com We can create Web API’s by

    creating a plumber.R file by “merely decorating your existing R source code with special comments” Using Plumber
  12. 25 @tudosgar | tamaszilagyi.com We can then plumb our function

    and expose it to the outside world (in this case only locally). Using Plumber
  13. 26 @tudosgar | tamaszilagyi.com We don’t always have everything we

    need for our application to run. In this case we need to install additional packages and its system level dependencies. We can also EXPOSE ports from within the Dockerfie and run commands using CMD. Dockerfile
  14. 33 Tools & resources @tudosgar | tamaszilagyi.com • R-specific base

    images: https:/ /hub.docker.com/r/rocker/ • Free interactive tutorials: https:/ /katacoda.com/courses/docker • Packages containerit and liftr to automatically containerize your R-code • Follow @jessfraz and @abbyfuller