benmarwick / testcontainerit

Other
1 stars 1 forks source link

How can I RUN something, e.g. render an R Markdown document in a RUN statement? #3

Open benmarwick opened 4 years ago

benmarwick commented 4 years ago

I can only see an option to use CMD in containerit::dockerfile(), how can I write a dockerfile that runs things using containerit::dockerfile()?

Like this, for example, where I can install some pkgs and render the Rmd file while building the Docker container. I like this because then docker build becomes a kind-of code quality check for the Rmd file. Can we get the RUN argument fully exposed in the R function?

# get the base image, the rocker/verse has R, RStudio and pandoc
FROM rocker/geospatial:3.6.1

# required
MAINTAINER Your Name <your_email@somewhere.com>

COPY . /compendium

# go into the repo directory
RUN . /etc/environment \
  # Install linux depedendencies here
  # e.g. need this for ggforce::geom_sina
  && sudo apt-get update \
  && sudo apt-get install libudunits2-dev -y \
  # build this compendium package
  && R -e "devtools::install_github('trinker/pacman'); devtools::install('/compendium', dep=TRUE)" \
  # render the manuscript into a docx, you'll need to edit this if you've
  # customised the location and name of your main Rmd file
  && R -e "devtools::check('/compendium',error_on = 'error')" \
 && R -e "rmarkdown::render('/compendium/analysis/paper.Rmd')"
nuest commented 4 years ago

You can add instructions manually, but they will be added "at the end" of all instructions:

addInstruction(df_description) <- Run(
    exec = "R",
    params = c("-e", "devtools::install_github('trinker/pacman'); devtools::install('/compendium', dep=TRUE)"))

> print(df_description)
FROM rocker/verse:3.6.2
LABEL maintainer="daniel"
# CRAN packages skipped because they are in the base image:
RUN ["install2.r", "compendium", "here"]
WORKDIR compendium/
COPY ["./", "./"]
RUN ["R", "-e", "devtools::install_github('trinker/pacman'); devtools::install('/compendium', dep=TRUE)"]
CMD ["R"]

Run_shell supports multiple commands if you are keen on reducing the number of RUN instructions:

> addInstruction(df_description) <- Run_shell(c("R -e \"devtools::install_github('trinker/pacman'); devtools::install('/compendium', dep=TRUE)\"",
+             "R -e \"devtools::check('/compendium',error_on = 'error')\""))

> print(df_description)
FROM rocker/verse:3.6.2
LABEL maintainer="daniel"
# CRAN packages skipped because they are in the base image:
RUN ["install2.r", "compendium", "here"]
WORKDIR compendium/
COPY ["./", "./"]
RUN ["R", "-e", "devtools::install_github('trinker/pacman'); devtools::install('/compendium', dep=TRUE)"]
RUN R -e "devtools::install_github('trinker/pacman'); devtools::install('/compendium', dep=TRUE)" \
  && R -e "devtools::check('/compendium',error_on = 'error')"
CMD ["R"]

I'd be open to add another constructor function that does something similar for a given executable to make the escaping less of a hassle, e.g. Run(exec = "R", params = list(c("-e", "..."), c("-e", "another..."))) ?

_Would you prefer an parameter extra_instructions as part of the dockerfile(..) function?_