5

I'm trying to create a multi-stage build for an R application based on rocker/r-ubuntu:20.04 image.

The reason that I'm based on that image is to install binary packages via apt-get as suggested in order to improve building time.

If I build the image without multi-stage build, the final image size is 2.32GB, so i need to decrease the final size with multi-stage build.

The approach that I follow is create an alpine:latest image and copy the app folder from builder, and the R libraries installed (/usr/local/lib/R/site-library/) and the binaries packages located in /usr/share/doc.

The final image doesn't work, because no commands to execute the app is installed.

The Dockerfile is the following:

FROM rocker/r-ubuntu:20.04 as builder


# # system libraries of general use
RUN apt-get update && apt-get install -y \
    pandoc \
    ...
    libxml2-dev 

RUN apt-get update && \
    apt-get install -y -qq \
    r-cran-config \
    ...
    r-cran-tidyverse 

RUN R -e "install.packages(c('other-packages'), dependencies=T)"    


# copy the app to the image
RUN mkdir -p /root/bloomapp/tmp
COPY . /root/bloomapp
COPY .Renviron Rprofile.site /usr/lib/R/etc/


FROM alpine:latest

#Copy app to alpine
COPY --from=builder /root/bloomapp /root/bloomapp
COPY --from=builder /usr/local/lib/R/site-library/ /usr/local/lib/R/site-library/
COPY --from=builder /usr/share/doc /usr/share/doc

WORKDIR /root/bloomapp/

EXPOSE 3838

Is reasonable this approach? or exist any other better way to do multi-stage build for a R app image?

Thanks.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
falberto
  • 83
  • 7
  • Are you sure you need a multi-stage build to decrease the file size? One simple trick would be to delete the package repository cache after package installation with `apt` (and to do it in the same `RUN` command, to avoid creating large intermediate layers — see [Dockerfile best practices](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#apt-get)). (That being said, a multi-stage build is in principle not a bad idea.) – Konrad Rudolph Jan 15 '21 at 16:51
  • Adding ```rm -rf /var/lib/apt/lists/*``` at the end of ```RUN``` commands, the image size decrease from ```2.32GB``` to ```2.28GB```, helps, but is not the solution. – falberto Jan 15 '21 at 17:15

1 Answers1

0

Alternative to starting with a rocker build, use a slim image & install R from source in the builder stage of the multistage build.

See R Installation docs here for reference: https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-R-under-Unix_002dalikes

  • Choose a directory to install the R tree (R is not just a binary, but has additional data sets, help files, font metrics etc). Let us call this place R_HOME. Untar the source code. This should create directories src, doc, and several more under a top-level directory... If the configure and make commands execute successfully, a shell-script front-end called R will be created and copied to R_HOME/bin.
Etienne Jacquot
  • 176
  • 1
  • 9