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


  1. Using Docker containers in R @tudosgar |

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

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

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

  5. 5 @tudosgar | Your program dev prod

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

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

  8. The solution? @tudosgar |

  9. @tudosgar | 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 | • 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 |

  12. 12 @tudosgar | Dockerfile Image Container

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

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

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

    components: 1. A ui.R that defines the frontend logic. Shiny App assets
  16. 16 @tudosgar | 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 |

  18. 18 @tudosgar | 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 | The base image contains the read-only

    layers and is preceded by FROM. Specifically for R, the rocker project (https:/ /www.rocker- 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 | Dockerfile Image Container write command command

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

  22. 22 Interlude: docker commands @tudosgar | • 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 |

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

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

  28. 28 @tudosgar | Tips & Tricks

  29. 29 @tudosgar | Use shared base layer images

  30. 30 @tudosgar | Less layers is better

  31. 31 @tudosgar | Use volumes

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

  33. 33 Tools & resources @tudosgar | • R-specific base

    images: https:/ / • Free interactive tutorials: https:/ / • Packages containerit and liftr to automatically containerize your R-code • Follow @jessfraz and @abbyfuller
  34. http:/ / @tudosgar @tudosgar | Keep in touch!