---
title: "Rocker & Dockerfiles"
author: "Colin Rundel"
date: "2019-01-24"
output:
  xaringan::moon_reader:
    css: "slides.css"
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false

---
exclude: true

```{setup r, message=FALSE, warning=FALSE, include=FALSE}
options(crayon.enabled = TRUE)
```

---
class: middle

# rocker

---

## rocker

.center[
  .large[`r icon::fa("docker")` + `r icon::fa("r-project")`] = https://www.rocker-project.org/
]

> The rocker project provides a collection of containers suited for different needs. find a base image to extend or images with popular software and optimized libraries pre-installed. Get the latest version or a reproducibly fixed environment.



.columns[ 
.col[ 
  ![](imgs/Lec03/rocker1.png)
]
.col[ 
  ![](imgs/Lec03/rocker2.png)
]
]

.columns[ 
.col[ 
  https://github.com/rocker-org/rocker-versioned
]
.col[ 
  https://github.com/rocker-org/rocker
]
]

---
background-image: url(imgs/Lec03/rocker_ver_sketch.png)
background-position: center
background-size: contain

---

## rocker + rstudio

```shell
$ docker pull rocker/rstudio
Using default tag: latest
latest: Pulling from rocker/rstudio
54f7e8ac135a: Pull complete
021dd68904f6: Pull complete
8d3e964cf18d: Pull complete
81bbbedbef33: Pull complete
c7878d347b3f: Pull complete
a7aae3dbd958: Pull complete
da950bb11a69: Pull complete
Digest: sha256:b9e6acea945ca28ec14b3de9925372bdd6c5e3828a8575b34d4247f619b66dd5
Status: Downloaded newer image for rocker/rstudio:latest
```

--

```shell
$ docker run -e PASSWORD=SECRET --rm -p 8787:8787 rocker/rstudio
...
```
.center[ http://rundel-rhel.colab.duke.edu:8787 ]


---

## rocker + rstudio + tidyverse + latex + sf

```shell
$ docker pull rocker/geospatial
Using default tag: latest
latest: Pulling from rocker/geospatial
54f7e8ac135a: Already exists
021dd68904f6: Already exists
8d3e964cf18d: Already exists
81bbbedbef33: Already exists
c7878d347b3f: Already exists
a7aae3dbd958: Already exists
da950bb11a69: Already exists
9802011fbe26: Pull complete
334f65727953: Pull complete
e506f9195a59: Pull complete
Digest: sha256:c3158846628b07d4616fafa30ef1d97cdc1b706d1c9aa30d62e7862ffd75703e
Status: Downloaded newer image for rocker/geospatial:latest34d4247f619b66dd5
Status: Downloaded newer image for rocker/rstudio:latest
```

--

```shell
$ docker run -e PASSWORD=SECRET --rm -p 8787:8787 rocker/geospatial
...
```

.center[ http://rundel-rhel.colab.duke.edu:8787 ]

---

## rocker github

---
class: middle

# Dockerfile(s)

---

## Common commands

| command   | Description                                                     |
|-----------|:----------------------------------------------------------------|
| `FROM`    | specify a base image                                            |
| `RUN`     | run commands (e.g. apt or yum), changes saved to image          |
| `COPY`    | copy a local file into the image                                |
| `ENV`     | set environment variables for Dockerfile and image              |
| `USER`    | set user to use (affects subsequent `RUN`, `CMD`, `ENDPOINT`)   |
| `WORKDIR` | set the working directory                                       |
| `EXPOSE`  | specify which ports will be used (not published automatically)  |
| `CMD`     | specify what (default) action should run when running the image |
| ...       |

---

## A tidy example

`tidy1/Dockerfile`:
```docker
FROM ubuntu:18.10

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update
RUN apt install -y r-base
RUN Rscript -e "install.packages('tibble')"

CMD ["R"]
```
---

## Building

.small[
```shell
*$ docker build -t tidy1 .
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM ubuntu:18.10
 ---> d4452947e3fa
Step 2/6 : ENV DEBIAN_FRONTEND=noninteractive
 ---> Using cache
 ---> e0d77908f64a
Step 3/6 : RUN apt update
 ---> Running in fa6969a427d9
...
 ---> 979edce6cc0b
Step 4/6 : RUN apt install -y r-base
 ---> Running in 1cc3e97a8258
...
 ---> 87a882be95d7
Step 5/6 : RUN Rscript -e "install.packages('tibble')"
 ---> Running in e9de97c98208
...
Removing intermediate container e9de97c98208
 ---> 5f1666e4e342
Step 6/6 : CMD ["R"]
 ---> Running in 07f9ea769466
...
 ---> 67b0f0ee7f5c
Successfully built 67b0f0ee7f5c
Successfully tagged tidy1:latest
```
]

---

```shell
*$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tidy1               latest              b29115e0a5d2        29 seconds ago      697MB
ubuntu              18.10               d4452947e3fa        3 weeks ago         73.7MB
ubuntu              latest              1d9c17228a9e        3 weeks ago         86.7MB

*$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              598100ea4878        25 seconds ago      697MB
tidy1               latest              b29115e0a5d2        25 seconds ago      697MB
<none>              <none>              75296565d476        56 seconds ago      690MB
<none>              <none>              d82af2f4083b        2 minutes ago       96.3MB
<none>              <none>              6bb4e2b36ade        2 minutes ago       73.7MB
ubuntu              18.10               d4452947e3fa        3 weeks ago         73.7MB
ubuntu              latest              1d9c17228a9e        3 weeks ago         86.7MB
```

---

## A better tidy example

`tidy2/Dockerfile`:
```docker
FROM ubuntu:18.10

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update \
 && apt install -y r-base \
 && Rscript -e "install.packages('tibble')"

CMD ["R"]
```

---

## Building

.small[
```shell
*$ docker build -t tidy2 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM ubuntu:18.10
 ---> d4452947e3fa
Step 2/4 : ENV DEBIAN_FRONTEND=noninteractive
 ---> Running in be9c52bb8db9
Removing intermediate container be9c52bb8db9
 ---> e0d77908f64a
Step 3/4 : RUN apt update  && apt install -y r-base  && Rscript -e "install.packages('tibble')"
...
Step 4/4 : CMD ["R"]
 ---> Running in d98129eb112c
Removing intermediate container d98129eb112c
 ---> be418f99be82
Successfully built be418f99be82
Successfully tagged tidy2:latest
*$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tidy2               latest              be418f99be82        7 seconds ago       697MB
ubuntu              18.10               d4452947e3fa        3 weeks ago         73.7MB
ubuntu              latest              1d9c17228a9e        3 weeks ago         86.7MB
*$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
tidy2               latest              be418f99be82        9 seconds ago        697MB
<none>              <none>              7869e0549108        10 seconds ago       697MB
<none>              <none>              e0d77908f64a        About a minute ago   73.7MB
ubuntu              18.10               d4452947e3fa        3 weeks ago          73.7MB
ubuntu              latest              1d9c17228a9e        3 weeks ago          86.7MB
```
]

---

## Dangling images

```shell
*$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              83384d4f0d1c        14 minutes ago      714MB
<none>              <none>              b656b4674ab9        15 minutes ago      209MB
<none>              <none>              c02c49f3aed2        15 minutes ago      96.3MB
<none>              <none>              cff3a89309ff        15 minutes ago      73.7MB
ubuntu              18.10               d4452947e3fa        3 weeks ago         73.7MB
ubuntu              latest              1d9c17228a9e        3 weeks ago         86.7MB

*$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
deleted: sha256:83384d4f0d1c6989ad492aee44dd44ca0a6aa013c0b80a78294d2d63e13c6d0c
deleted: sha256:ada707994b5f0aacd91d44b221e78fb4674c2f53afed87ac9c8f3db39b993d48
deleted: sha256:b656b4674ab989f57e1dc3dab682bcbfe6b8634206935cb630e09cfcecf5f645
deleted: sha256:8769da78ba480413ba7b3ee505008b88e549214a96238744bc328901367c38c1
deleted: sha256:c02c49f3aed2b3131f4e636745f713aab28ba870e9ec2ac2c0b13b008e9a2fd3
deleted: sha256:095edb1ee39242ed717542420ab9a4911f7cd805713bfd94b321d52d25091eda
deleted: sha256:cff3a89309ffcc08fe80a3558e1c6715574a4ec3ac926bbd49cd90519aa62241

Total reclaimed space: 640.4MB

*$ docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              18.10               d4452947e3fa        3 weeks ago         73.7MB
ubuntu              latest              1d9c17228a9e        3 weeks ago         86.7MB
```