From 37ff58100dd08bc8420646985a23180a56c458cb Mon Sep 17 00:00:00 2001 From: Atharva Date: Thu, 13 Feb 2025 00:06:24 +0530 Subject: [PATCH] multi-stage dockerfile: This reduces image size to half compare to earlier image version size --- Dockerfile | 73 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4913c2a5..6fce152e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,9 @@ -FROM mcr.microsoft.com/devcontainers/cpp:dev-ubuntu-22.04 +# First Stage: Builder +FROM mcr.microsoft.com/devcontainers/cpp:dev-ubuntu-22.04 AS builder ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="none" -# Optionally install the cmake for vcpkg +# Optionally install CMake for vcpkg COPY ./reinstall-cmake.sh /tmp/ RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \ @@ -10,25 +11,65 @@ RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \ fi \ && rm -f /tmp/reinstall-cmake.sh -RUN sed -i.bak "/^#.*deb-src.*universe$/s/^# //g" /etc/apt/sources.list \ - && apt -y update \ - && apt install -y --no-install-recommends \ +# Enable deb-src for dependencies +RUN sed -i.bak "/^#.*deb-src.*universe$/s/^# //g" /etc/apt/sources.list && apt update + +# Install necessary build dependencies +RUN apt install -y --no-install-recommends \ software-properties-common \ subversion \ - && add-apt-repository --enable-source --yes "ppa:marutter/rrutter4.0" \ - && wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc \ + wget \ + gpg \ + build-essential \ + gfortran \ + libxml2-dev \ + libcurl4-openssl-dev \ + libssl-dev \ + libblas-dev \ + liblapack-dev + +# Add R repository and install R +RUN add-apt-repository --enable-source --yes "ppa:marutter/rrutter4.0" \ + && wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc \ + && apt update \ && apt -y build-dep r-base-dev \ - && apt -y install r-base-dev \ - && Rscript -e "install.packages('languageserver', repos='https://cran.rstudio.com')" \ - && Rscript -e "install.packages('httpgd', repos='https://cran.rstudio.com')" \ - && rm -rf /var/lib/apt/lists/* + && apt -y install r-base-dev -RUN apt install shellcheck -RUN apt install -y ccache -#RUN /usr/sbin/update-ccache-symlinks -#RUN echo 'export PATH="/usr/lib/ccache:$PATH"' | tee -a /home/vscode/.bashrc +# Install R packages +RUN Rscript -e "install.packages('languageserver', repos='https://cran.rstudio.com')" \ + && Rscript -e "install.packages('httpgd', repos='https://cran.rstudio.com')" +# Install shellcheck and ccache +RUN apt install -y shellcheck ccache +# Second Stage: Final Image +FROM mcr.microsoft.com/devcontainers/cpp:dev-ubuntu-22.04 AS final ARG CONTAINER_VERSION -ENV CONTAINER_VERSION ${CONTAINER_VERSION} +ENV CONTAINER_VERSION=${CONTAINER_VERSION} + +# Install minimal runtime dependencies +RUN apt update && apt install -y --no-install-recommends \ + libxml2 \ + libcurl4-openssl-dev \ + libssl-dev \ + libblas-dev \ + liblapack-dev \ + gfortran + +# Copy R installation from builder stage +COPY --from=builder /usr/lib/R /usr/lib/R +COPY --from=builder /usr/bin/R /usr/bin/R +COPY --from=builder /usr/local/lib/R /usr/local/lib/R +COPY --from=builder /usr/share/R /usr/share/R +COPY --from=builder /etc/R /etc/R + +# Copy shared libraries +COPY --from=builder /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu + +# Set environment variables +ENV PATH="/usr/lib/R/bin:$PATH" +ENV LD_LIBRARY_PATH="/usr/lib/R/lib:/usr/lib/x86_64-linux-gnu" + +# Verify installation +RUN ldconfig && R --version