--- title: "Getting started with dockerfiler" author: "Colin Fay" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Getting started with dockerfiler} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` # dockerfiler Easy Dockerfile Creation from R. ## Installation You can install dockerfiler from GitHub with: ```{r gh-installation, eval = FALSE} # install.packages("remotes") remotes::install_github("ThinkR-open/dockerfiler") ``` Or from CRAN with : ```{r, eval = FALSE} install.packages("dockerfiler") ``` ## Basic workflow By default, `Dockerfile$new()` creates a Dockerfile with `FROM "rocker/r-base"`. (The high-level generators `dock_from_desc()` and `dock_from_renv()` use a different default: `rocker/r-ver` tagged with your R version; see below.) You can set another FROM in `new()` ```{r} library(dockerfiler) # Create a dockerfile template my_dock <- Dockerfile$new() my_dock$MAINTAINER("Colin FAY", "contact@colinfay.me") ``` Wrap your raw R Code inside the `r()` function to turn it into a bash command with `R -e`. ```{r} my_dock$RUN(r(install.packages("attempt", repos = "https://cloud.r-project.org"))) ``` Classical Docker stuffs: ```{r} my_dock$RUN("mkdir /usr/scripts") my_dock$RUN("cd /usr/scripts") my_dock$COPY("plumberfile.R", "/usr/scripts/plumber.R") my_dock$COPY("torun.R", "/usr/scripts/torun.R") my_dock$EXPOSE(8000) my_dock$CMD("Rscript /usr/scripts/torun.R ") ``` See your Dockerfile : ```{r} my_dock ``` If you've made a mistake in your script, you can switch lines with the `switch_cmd` method. This function takes as arguments the positions of the two cmd you want to switch : ```{r} # Switch line 8 and 7 my_dock$switch_cmd(8, 7) my_dock ``` You can also remove a cmd with `remove_cmd`: ```{r} my_dock$remove_cmd(8) my_dock ``` This also works with a vector: ```{r} my_dock$remove_cmd(5:7) my_dock ``` `add_after` add a command after a given line. ```{r} my_dock$add_after( cmd = "RUN R -e 'remotes::install_cran(\"rlang\")'", after = 3 ) ``` Save your Dockerfile: ```{r eval = FALSE} my_dock$write() ``` ## Create a Dockerfile from a DESCRIPTION You can use a DESCRIPTION file to create a Dockerfile that installs the dependencies and the package. ```{r, eval = FALSE} my_dock <- dock_from_desc("DESCRIPTION") my_dock$CMD(r(library(dockerfiler))) my_dock$add_after( cmd = "RUN R -e 'remotes::install_cran(\"rlang\")'", after = 3 ) ``` `dock_from_desc()` defaults to `FROM rocker/r-ver:` and pulls Linux binaries from Posit Public Package Manager (`https://p3m.dev/cran/latest`). Pass `FROM = "rocker/r-base"` or `repos = c(CRAN = "https://cran.rstudio.com/")` to opt out. ## Create a Dockerfile from a renv.lock If your project uses `{renv}`, `dock_from_renv()` turns the `renv.lock` into a Dockerfile that restores the exact pinned versions. ```{r, eval = FALSE} my_dock <- dock_from_renv(lockfile = "renv.lock") my_dock ``` By default the generated Dockerfile is `FROM rocker/r-ver:` (multi-arch amd64 + arm64), pulls Linux binaries from Posit Public Package Manager, and runs the container as the non-root `rstudio` user. Pass `FROM = "rocker/r-base"`, `repos = c(CRAN = "https://cran.rstudio.com/")`, or `user = NULL` to restore the previous behaviour. ## Parse an existing Dockerfile Already have a Dockerfile? `parse_dockerfile()` reads it back into a `Dockerfile` object you can edit and re-`$write()`. ```{r, eval = FALSE} my_dock <- parse_dockerfile("Dockerfile") my_dock my_dock$RUN(r(library(dockerfiler))) my_dock$write("Dockerfile") ```