Home > OS >  Docker image does not include pre-installed R package
Docker image does not include pre-installed R package

Time:02-03

I dockerize an R analysis and want to have an initial Docker layer, with all the required R packages, on the top of which I am going to add the analysis script. Despite having read the various documentation and blogs I am stunned by the following behavior:

Here is a Dockerfile, named Dockerfile_build

FROM r-base

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

I build the layer:

docker build -t mycreate_od/libraries -f vault/Dockerfile_build3 .

All going fine

> install.packages('rjson',dependencies=TRUE, repos='http://cran.rstudio.com/')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL 'http://cran.rstudio.com/src/contrib/rjson_0.2.21.tar.gz'
Content type 'application/x-gzip' length 116509 bytes (113 KB)
==================================================
downloaded 113 KB

* installing *source* package ‘rjson’ ...
** package ‘rjson’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
g   -std=gnu  14 -I"/usr/share/R/include" -DNDEBUG      -fpic  -g -O2 -ffile-prefix-map=/build/r-base-PT7Nxy/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c dump.cpp -o dump.o
gcc -std=gnu99 -std=gnu11 -I"/usr/share/R/include" -DNDEBUG      -fpic  -g -O2 -ffile-prefix-map=/build/r-base-PT7Nxy/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c parser.c -o parser.o
gcc -std=gnu99 -std=gnu11 -I"/usr/share/R/include" -DNDEBUG      -fpic  -g -O2 -ffile-prefix-map=/build/r-base-PT7Nxy/r-base-4.1.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c register.c -o register.o
g   -std=gnu  14 -shared -L/usr/lib/R/lib -Wl,-z,relro -o rjson.so dump.o parser.o register.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/00LOCK-rjson/00new/rjson/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (rjson)
> 
> 

The downloaded source packages are in
    ‘/tmp/RtmpYvKXqB/downloaded_packages’
Removing intermediate container 7834d6587dbd
 ---> 7eb3564ac66f
Successfully built 7eb3564ac66f
Successfully tagged mycreate_od/libraries:latest

I have now an image

REPOSITORY              TAG       IMAGE ID       CREATED             SIZE
mycreate_od/libraries   latest    7eb3564ac66f   26 seconds ago      767MB

I use now the analysis Dockerfile, let's make it most simple

The R script is "includes/sessionInfo.R"

print(sessionInfo())

The Dockerfile

FROM mycreate_od/libraries

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

## Copy files
COPY includes /home/analysis/includes

## Run the analysis
CMD R -e "source('/home/analysis/includes/sessionInfo.R')"

Now we build the image and run it

docker build -t mycreate_od/test -f vault/Dockerfile_test .
docker run -ti --rm mycreate_od/test

And I see that:

> source('/home/analysis/includes/sessionInfo.R')
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux bookworm/sid

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.1.2

The rjson package previously installed is gone. What am I misunderstanding?

CodePudding user response:

sessionInfo only lists the loaded packages, but you haven't done this for rjson. You could change your "includes/sessionInfo.R" to

library(rjson)
sessionInfo()
  •  Tags:  
  • Related