For the purposes of this question, please assume this Dockerfile requires a multistage build where step one utilizes rocker/r-ver:4.1.1
as a base image and step two utilizes mcr.microsoft.com/mssql/server:2017-latest
as a base image. Below is the Dockerfile:
# Stage 1: Build stage
FROM rocker/r-ver:4.1.1 AS build
RUN /rocker_scripts/install_tidyverse.sh
# Declare environment variables
ENV PATH=/root/.local/bin:$PATH \
ACCEPT_EULA=y \
DEBIAN_FRONTEND=noninteractive \
TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00/
# Install system libraries
RUN apt-get update \
&& apt-get install -y --allow-downgrades binutils \
&& apt-get install -y gnupg apt-transport-https curl apt-utils libgsl-dev libfontconfig1-dev libmariadb-dev tdsodbc \
libmagick++-dev libavfilter-dev cmake cargo libpoppler-cpp-dev libtesseract-dev libleptonica-dev tesseract-ocr-eng
# Install R packages
RUN Rscript -e "install.packages(c('RMySQL', 'ragg', 'RMariaDB', 'nloptr', 'tidyverse', 'lme4', 'phyr', 'odbc', \
'MEMSS', 'mlmRev', 'gamm4', 'pbkrtest', 'semEff', 'merDeriv', 'car', 'rr2', 'magick', 'av'), dependencies = TRUE)"
# Copy the files
COPY . /app
# Stage 2: Final stage
FROM mcr.microsoft.com/mssql/server:2017-latest
# Set environment variables
ENV ACCEPT_EULA=Y PATH=/usr/local/bin/R:$PATH DEBIAN_FRONTEND=noninteractive
# Install system libraries
# Need to do this a second time since the libraries don't get copied over properly as the SQL image erases some
# Still it's necessary in the first step as well since the R packages can't be installed otherwise
RUN apt-get update \
&& apt-get install -y --allow-downgrades binutils \
&& apt-get install -y gnupg apt-transport-https curl apt-utils libgsl-dev libfontconfig1-dev libmariadb-dev tdsodbc \
libmagick++-dev libavfilter-dev cmake cargo libpoppler-cpp-dev libtesseract-dev libleptonica-dev tesseract-ocr-eng \
r-base r-base-dev
# Copy the files
COPY --from=build /app /app
# Set working directory
WORKDIR /app
# Set the entrypoint command
CMD ["Rscript", "run.R"]
As you can see, I am installing the system libraries in both the first and second stage. I am wondering if it is possible to avoid this redundancy by copying the installation over somehow. The COPY --from=build /app /app
does not accomplish this, and I have tried adding the below commands instead of installing the system libraries again to no avail:
# Copy the R binary from build stage
COPY --from=build /usr/local/bin/R /usr/local/bin/R
# Copy the R libraries from build stage
COPY --from=build /usr/local/lib/R /usr/local/lib/R
Does anyone have any idea how I can avoid running the install system libraries command in both stages?