From 7e25aa5a46344f43102a256306e0905764f7b178 Mon Sep 17 00:00:00 2001 From: Adam Blake Date: Fri, 5 Jan 2024 13:01:56 -0800 Subject: [PATCH] refactor(datascience): simplify stages --- datascience-notebook/Dockerfile | 74 +++++++-------------------------- 1 file changed, 16 insertions(+), 58 deletions(-) diff --git a/datascience-notebook/Dockerfile b/datascience-notebook/Dockerfile index fb93889..0b3601c 100644 --- a/datascience-notebook/Dockerfile +++ b/datascience-notebook/Dockerfile @@ -22,12 +22,13 @@ ARG APT_LIB_DIR=/var/lib/apt ARG SHARED_LIBS_LIST=/tmp/shared-libs.txt -# py-builder +# base # ------------------------------------- -FROM ${PARENT_IMAGE} as py-builder +FROM ${PARENT_IMAGE} as base USER root +# install cmdstan dependencies ARG APT_CACHE_DIR APT_LIB_DIR # hadolint ignore=DL3008,DL3009 RUN --mount=type=cache,target="${APT_CACHE_DIR}",sharing=locked \ @@ -35,15 +36,20 @@ RUN --mount=type=cache,target="${APT_CACHE_DIR}",sharing=locked \ apt-get update && apt-get upgrade -y && apt-get -y install --no-install-recommends \ g++ +USER ${NB_UID} + +# install cmdstan +RUN Rscript -e 'remotes::install_cran("cmdstanr", repos = "https://mc-stan.org/r-packages/", dependencies = TRUE)' \ + && Rscript -e 'cmdstanr::install_cmdstan()' + + +# py-builder +# ------------------------------------- +FROM base as py-builder + RUN --mount=type=cache,target=${CONDA_DIR}/pkgs,uid=${NB_UID},sharing=locked \ mamba install --yes \ - "gdal" \ - "pkg-config" \ - "proj" \ - && fix-permissions "${CONDA_DIR}" \ - && fix-permissions "/home/${NB_USER}" - -USER ${NB_UID} + "gdal" # make wheels for the Python packages ARG PIP_CACHE_DIR PIP_WHEEL_DIR PY_REQUIREMENTS @@ -53,59 +59,11 @@ RUN --mount=type=bind,source="${PY_REQUIREMENTS}",target=/tmp/requirements.txt \ && pip3 wheel --cache-dir="${PIP_CACHE_DIR}" --wheel-dir="${PIP_WHEEL_DIR}" -r /tmp/requirements.txt -# r-builder -# ------------------------------------- -FROM ${PARENT_IMAGE} as r-builder - - -# pre-shared-libs -# ------------------------------------- -FROM ${BASE_IMAGE} as pre-shared-libs -ARG SHARED_LIBS_LIST -RUN find-shared-libs "${R_HOME}/library" > "${SHARED_LIBS_LIST}" - - -# new-shared-libs -# ------------------------------------- -FROM r-builder as new-shared-libs -ARG SHARED_LIBS_LIST -COPY --from=pre-shared-libs "${SHARED_LIBS_LIST}" /tmp/pre-shared-libs.txt -RUN find-shared-libs "${R_HOME}/library" > /tmp/post-shared-libs.txt \ - && diff-shared-libs /tmp/pre-shared-libs.txt /tmp/post-shared-libs.txt > "${SHARED_LIBS_LIST}" - - # final # ------------------------------------- -FROM ${BASE_IMAGE} as final +FROM base LABEL maintainer="CourseKata " -USER root - -# install cmdstan dependencies -ARG APT_CACHE_DIR APT_LIB_DIR -# hadolint ignore=DL3008,DL3009 -RUN --mount=type=cache,target="${APT_CACHE_DIR}",sharing=locked \ - --mount=type=cache,target="${APT_LIB_DIR}",sharing=locked \ - apt-get update && apt-get upgrade -y && apt-get -y install --no-install-recommends \ - g++ - -# copy over R packages and shared libraries -ARG SHARED_LIBS_LIST -COPY --from=r-builder --chown=${NB_UID}:${NB_GID} "${R_HOME}/" "${R_HOME}" -RUN --mount=type=cache,from=r-builder,source=/,target=/mounted-r-builder \ - --mount=type=bind,from=new-shared-libs,source="${SHARED_LIBS_LIST}",target="${SHARED_LIBS_LIST}" \ - copy-shared-libs "${SHARED_LIBS_LIST}" /mounted-r-builder / \ - # this is hard to copy over from the r-builder image - && Rscript -e 'remotes::install_cran("V8", force = TRUE)' \ - # install cmdstan - && Rscript -e 'remotes::install_cran("cmdstanr", repos = "https://mc-stan.org/r-packages/", dependencies = TRUE)' \ - && Rscript -e 'cmdstanr::install_cmdstan()' \ - # ensure user has full control of package and home dirs - && fix-permissions "${CONDA_DIR}" \ - && fix-permissions "/home/${NB_USER}" - -USER ${NB_UID} - # install Python packages from wheels ARG PIP_CACHE_DIR PIP_WHEEL_DIR PY_REQUIREMENTS RUN --mount=type=bind,source="${PY_REQUIREMENTS}",target=/tmp/requirements.txt \