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
Tweet

More Decks by Tamas Szilagyi

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  7. The solution?
    @tudosgar | tamaszilagyi.com

    View Slide

  8. The solution?
    @tudosgar | tamaszilagyi.com

    View Slide

  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.

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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!

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  31. 31
    @tudosgar | tamaszilagyi.com
    Use volumes

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide