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.

Fa8c8d5560f95f32cb78e164ba2d222e?s=128

Tamas Szilagyi

August 14, 2018
Tweet

Transcript

  1. Using Docker containers in R @tudosgar | tamaszilagyi.com

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

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

    usecases • Best practices, tools and resources Agenda @tudosgar | tamaszilagyi.com
  4. 4 @tudosgar | tamaszilagyi.com Your program you not you

  5. 5 @tudosgar | tamaszilagyi.com Your program dev prod

  6. 6 @tudosgar | tamaszilagyi.com Common problems • Different OS •

    Missing system-level dependencies • Missing packages • Different versions • Missing keys • …
  7. The solution? @tudosgar | tamaszilagyi.com

  8. The solution? @tudosgar | tamaszilagyi.com

  9. @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.
  10. 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
  11. 11 Your first Docker container. @tudosgar | tamaszilagyi.com

  12. 12 @tudosgar | tamaszilagyi.com Dockerfile Image Container

  13. 13 @tudosgar | tamaszilagyi.com Dockerfile Image Container write command command

  14. 14 Usecase #1: Shiny App @tudosgar | tamaszilagyi.com

  15. 15 @tudosgar | tamaszilagyi.com A shiny app consists of two

    components: 1. A ui.R that defines the frontend logic. Shiny App assets
  16. 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
  17. 17 How do we package this up? @tudosgar | tamaszilagyi.com

  18. 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!
  19. 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
  20. 20 @tudosgar | tamaszilagyi.com Dockerfile Image Container write command command

  21. 21 @tudosgar | tamaszilagyi.com If your app is large, use

    volumes!
  22. 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).
  23. 23 Usecase #2: Web API @tudosgar | tamaszilagyi.com

  24. 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
  25. 25 @tudosgar | tamaszilagyi.com We can then plumb our function

    and expose it to the outside world (in this case only locally). Using Plumber
  26. 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
  27. 27 @tudosgar | tamaszilagyi.com Dockerfile Image Container write command command

  28. 28 @tudosgar | tamaszilagyi.com Tips & Tricks

  29. 29 @tudosgar | tamaszilagyi.com Use shared base layer images

  30. 30 @tudosgar | tamaszilagyi.com Less layers is better

  31. 31 @tudosgar | tamaszilagyi.com Use volumes

  32. 32 @tudosgar | tamaszilagyi.com Push down layers that invalidate the

    cache
  33. 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
  34. http:/ /tamaszilagyi.com/ @tudosgar @tudosgar | tamaszilagyi.com Keep in touch!