Reduce Docker Image Size with R-installation & dependencies

Viewed 472

I am using images to run R in its base form with certain packages and its dependencies installed. For this, I create an intermediary (basic) image which I subsequently use to build the final image on.

Below you find a dockerfile to build this intermediary image (and not the final image)

Goal

I want to reduce the total size AND the amount of layers (and thus reduce time to 'pull' the image from the registry.

Question

What can I do to further reduce size and layers in the final image?

Current approach

Please look at the docker file enclosed:

  1. I used a multi-stage approach to remove as much build-dependend libraries in stage 2 as I could find.
  2. Some R packages are available as binary, but some are not. Thats why some packages are installed using the 'Rscript -e commmand'. And some are installed using the apt-get update / install commands. The latter is faster & takes up less space.
  3. I only install the libraries the image needs to actually run the R-session using all the packages provided.

Multistage Dockerfile to create an intermediary image

# Base image
FROM rocker/r-base:latest AS stage1

#install binary & build dependencies

RUN apt-get update && apt-get install -y -qq --no-install-recommends --purge \
r-cran-pdftools \
r-cran-dplyr \
r-cran-knitr \
r-cran-magick \
r-cran-purrr \
r-cran-tidyr \
r-cran-tm \
r-cran-lubridate \
r-cran-ggplot2 \
r-cran-readxl \
pandoc \
libxml2 \
libssl1.1 \
tesseract-ocr-eng \
tesseract-ocr-nld \
liblept5 \
libgit2-dev \
libtesseract4 && \
apt-get autoclean && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /tmp/*
 
##Build the second stage
FROM stage1 AS stage2
RUN apt-get update && apt-get install -y -qq --no-install-recommends --purge \
libtesseract-dev \
libleptonica-dev \
libxml2-dev \
libgit2-dev \
libssl-dev && \
Rscript -e "install.packages(c('rmarkdown', 'forcats', 'tesseract', 'AzureStor', 'AzureKeyVault', 'stopwords', 'SnowballC', 'NbClust', 'flexdashboard', 'formattable', 'htmlwidgets', 'xgboost'))"
    
#use stage 1 as base and copy run time libraries needed for final version
FROM stage1
COPY --from=stage2 /usr/local/lib/R/site-library /usr/local/lib/R/site-library

Any help is much appreciated!

0 Answers
Related