Slide 1

Slide 1 text

Using Docker containers in R @tudosgar | tamaszilagyi.com

Slide 2

Slide 2 text

2 whoami @tudosgar | tamaszilagyi.com • EX - Sr. Analyst ING • blog: http:/ /tamaszilagyi.com/ • love hiking!

Slide 3

Slide 3 text

3 • Docker Basics • Our first Docker container: 2 usecases • Best practices, tools and resources Agenda @tudosgar | tamaszilagyi.com

Slide 4

Slide 4 text

4 @tudosgar | tamaszilagyi.com Your program you not you

Slide 5

Slide 5 text

5 @tudosgar | tamaszilagyi.com Your program dev prod

Slide 6

Slide 6 text

6 @tudosgar | tamaszilagyi.com Common problems • Different OS • Missing system-level dependencies • Missing packages • Different versions • Missing keys • …

Slide 7

Slide 7 text

The solution? @tudosgar | tamaszilagyi.com

Slide 8

Slide 8 text

The solution? @tudosgar | tamaszilagyi.com

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

11 Your first Docker container. @tudosgar | tamaszilagyi.com

Slide 12

Slide 12 text

12 @tudosgar | tamaszilagyi.com Dockerfile Image Container

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

17 How do we package this up? @tudosgar | tamaszilagyi.com

Slide 18

Slide 18 text

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!

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

23 Usecase #2: Web API @tudosgar | tamaszilagyi.com

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

25 @tudosgar | tamaszilagyi.com We can then plumb our function and expose it to the outside world (in this case only locally). Using Plumber

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

28 @tudosgar | tamaszilagyi.com Tips & Tricks

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

30 @tudosgar | tamaszilagyi.com Less layers is better

Slide 31

Slide 31 text

31 @tudosgar | tamaszilagyi.com Use volumes

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

http:/ /tamaszilagyi.com/ @tudosgar @tudosgar | tamaszilagyi.com Keep in touch!