May 22nd 2019

Content

  • What is Docker
  • How can Docker be useful to R users
  • How Docker works, how to use it
  • Examples
  • Docker Best Practices

What is Docker

What is a container

A standardized unit of software

Docker website

  • Containers package a piece of software
  • Containers wrap all dependencies for this software to run
  • Containers are invisible to each other
  • Containers can be combined to create more complex modular systems

  • Containers are nice little boxes with reproducibe software inside

Containerisation vs. Virtualisation

For Data Scientist and R users?

Why use Docker

  • Repeatability
    • Analysis
    • Environment
  • Application and data packaging
  • Development / Production environment management
  • Deployment on well known PaaS
  • Easy services deployment

How Docker works
and how to use it

Dockerfile, images, containers…

  • Dockerfile: describes an image (software, dependencies, …)
  • image: the software with all its dependencies
  • container: (running) instance of an image

In practice

Standing on the shoulders of giants

  • Choose a base image (for ex. r-base)

    FROM r-base:latest
  • Describe your customisations (add R packages, your code, …)

    RUN R -e "install.packages('shiny')"
    COPY build/mypackage.tar.gz .
  • Build the image

    $ docker build -t my-image-name .
  • Start a container

    $ docker run my-image-name

Persistence: bind mounts

  • Each new container starts from the image, data are lost when the container is deleted
  • Bind mounts are filesystem locations outside the container where data can be read or saved

    $ docker run --mount type=bind,source=/path/to/my/data,target=/data image-name

    Docker Documentation: Use bind mounts

Examples

Existing image

  • Install Docker

    Docker Install Documentation

  • Run an image

    $ docker run --rm -p 8787:8787 -e PASSWORD=GoodPassword
      --mount type=bind,source=~/Documents/Dev/Docker4R,target=/home/rstudio/data
      rocker/tidyverse

Your Shiny app

    FROM r-base:latest
    MAINTAINER Lynda Metref "lynda.metref@gmail.com"

    RUN R -e "install.packages('shiny')"

    # Expect a single file in the build directory
    COPY build/RBoilerplateShinyDocker*.tar.gz .
    RUN R -e "install.packages(dir(pattern='RBoilerplateShinyDocker.*'))"

    EXPOSE 8888
    ENTRYPOINT ["R", "-e", "library(RBoilerplateShinyDocker); launch(8888,'0.0.0.0')"]

Source

Docker Best Practices

Docker security: it's like your home

  • Don't let strangers in
  • Only give your keys to someone you know well

Use base images from known sources

  • Docker Hub Certified Images and Verified Publisher
  • Reputable communities
  • Check Dockerfiles

When running …

  • Do not run your application in the container as root (the default)

    $ docker run --user my_user ...
  • Check network parameters

    Docker Network Documentation

When creating your own …

  • Don't put credentials in Dockerfile or image, load them from environment.

Thanks for your attention!

Aknowledgements