1

I create a docker image to run R scripts on a VM server with no access to the internet.

For the first layer I load R and all libraries

Dockerfile1

FROM r-base

## Needed to access R
ENV R_HOME /usr/lib/R

## install required libraries
RUN apt-get update
RUN apt-get -y install libgdal-dev

## install R-packages
RUN R -e "install.packages('dplyr',dependencies=TRUE, repos='http://cran.rstudio.com/')"
...

and create it

docker build -t mycreate_od/libraries -f Dockerfile1 .

Then I use this library layer to load the R script

Dockerfile2

FROM mycreate_od/libraries

## Create directory
RUN mkdir -p /home/analysis/

## Copy files
COPY my_script_dir /home/analysis/

## Run the script
CMD R -e "source('/home/analysis/my_script_dir/main.R')"

Create the analysis layer

docker build -t mycreate_od/analysis -f vault/Dockerfile2 .

On my master VM, this runs and suceeds, but on the fresh VM I get

docker run mycreate_od/analysis

R docker ERROR: R_HOME ('/usr/lib/R') not found - Recherche Google

From a previous bug search I have set the ENV variable in the Docker (see Dockerfile1), but it looks like docker installs R on some other place.

Xavier Prudent
  • 1,570
  • 3
  • 25
  • 54
  • `r-base` maintainer/author here. First things first: you do not need `ENV`. You can install `dplyr` more cheaply via `sudo r-cran-dplyr`. I would then recommend trying things interactively first -- many of us have published dozens of containers derived from `r-base`. I would also recommend `Rscript /path/to/script/script.R` over `source(...)`. – Dirk Eddelbuettel Feb 07 '22 at 18:56
  • Thanks, I will then start following these recomandations: https://stackoverflow.com/questions/51500385/how-to-speed-up-r-packages-installation-in-docker – Xavier Prudent Feb 07 '22 at 18:58
  • Call me crazy but I do not see what the requirement has to do with the errors you claim seeing or the somewhat non-standard. I suggest to 'make it work' first, then ensure you can export your container as a tarfile and import it on the airgapped machine. Good luck! – Dirk Eddelbuettel Feb 07 '22 at 19:00
  • you re right, comments corrected too late – Xavier Prudent Feb 07 '22 at 19:00
  • Sure. Note that `r-base` aka `rocker/r-base` is _Debian_ based where the answer you linked to takes advantage of _Ubuntu_ repos. Debian has binaries too, just not the PPA with about 5k packages, Don't mix Debian and Ubuntu -- use `rocker/r-bspm:20.04` for easiest access to most binaries in .deb form. – Dirk Eddelbuettel Feb 07 '22 at 19:03
  • The VM is indeed Ubuntu, can I then use r-base? – Xavier Prudent Feb 07 '22 at 21:23
  • There is no relationship with the underlying VM and the Docker base. Anything that runs, runs. You can do Fedora on CentOS or Debian or Ubuntu or vice versa. What I warned you about was _inside_ container. If a container is derived from _Debian_ as r-base is then you cannot use Ubuntu-specific extensions in it. Hope this helps. – Dirk Eddelbuettel Feb 07 '22 at 21:42
  • Does that mean that 'R_HOME ('/usr/lib/R') not found ' may be linked to the misuse of the redirection for an Ubuntu-based container? – Xavier Prudent Feb 08 '22 at 13:46
  • I doubt it is that, but I remain steadfast that you are doing something wrong as these containers _do_ compose nicely and are used daily by lots of people as building blocks. Simplify, reduce, ... til it no longer breaks. Standard debugging. I cannot tell from here what may be wrong. – Dirk Eddelbuettel Feb 08 '22 at 13:57
  • 1
    Words of wisdom. I debug and will document the result for archives. – Xavier Prudent Feb 08 '22 at 14:10

1 Answers1

0

Thanks to Dirk advice I managed to get it done using r-apt (see Dockerfile below).

The image get then built and can be run without the R_HOME error.

BTW much faster and with a significantly smaller resulting image.

FROM rocker/r-apt:bionic

RUN apt-get update && \
    apt-get -y install libgdal-dev && \
    apt-get install -y -qq \
    r-cran-dplyr \
    r-cran-rjson \   
    r-cran-data.table  \
    r-cran-crayon \
    r-cran-geosphere \
    r-cran-lubridate  \
    r-cran-sp \
    r-cran-R.utils

RUN R -e 'install.packages("tools")'
RUN R -e 'install.packages("rgdal", repos="http://R-Forge.R-project.org")'

This is unfortunately a cargo-boat solution, as I am unable to explain why the previous Dockerfile failed.

Xavier Prudent
  • 1,570
  • 3
  • 25
  • 54