alessandro trinca tornidor
[feat] prepare entire docker build with nvidia GPU on hf space cloning https://huggingface.co/spaces/aletrn/samgis
0914710
# Include global ARGs at the dockerfile top | |
ARG ARCH="x86_64" | |
ARG LAMBDA_TASK_ROOT="/var/task" | |
ARG FASTAPI_STATIC="${LAMBDA_TASK_ROOT}/static" | |
ARG PYTHONPATH="${LAMBDA_TASK_ROOT}:${PYTHONPATH}:/usr/local/lib/python3/dist-packages" | |
ARG POETRY_NO_INTERACTION=1 | |
ARG POETRY_VIRTUALENVS_IN_PROJECT=1 | |
ARG POETRY_VIRTUALENVS_CREATE=1 | |
ARG POETRY_CACHE_DIR=/tmp/poetry_cache | |
ARG RIE="https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie" | |
FROM nvcr.io/nvidia/pytorch:24.01-py3 as builder_global | |
ARG ARCH | |
ARG LAMBDA_TASK_ROOT | |
ARG PYTHONPATH | |
ARG POETRY_NO_INTERACTION | |
ARG POETRY_VIRTUALENVS_IN_PROJECT | |
ARG POETRY_VIRTUALENVS_CREATE | |
ARG POETRY_CACHE_DIR | |
ARG RIE | |
ARG DEPENDENCY_GROUP | |
ARG ZLIB1G="http://ftp.it.debian.org/debian/pool/main/z/zlib/zlib1g_1.3.dfsg-3+b1_amd64.deb" | |
RUN echo "ARCH: $ARCH ..." | |
RUN echo "ARG RIE: $RIE ..." | |
RUN echo "ARG POETRY_CACHE_DIR: ${POETRY_CACHE_DIR} ..." | |
RUN echo "ARG PYTHONPATH: $PYTHONPATH ..." | |
RUN test -n ${DEPENDENCY_GROUP:?} | |
RUN echo "python DEPENDENCY_GROUP: ${DEPENDENCY_GROUP} ..." | |
RUN echo "arg dep:" | |
# Set working directory to function root directory | |
WORKDIR ${LAMBDA_TASK_ROOT} | |
COPY requirements_poetry.txt pyproject.toml poetry.lock README.md ${LAMBDA_TASK_ROOT}/ | |
RUN cat /etc/lsb-release | |
# avoid segment-geospatial exception caused by missing libGL.so.1 library | |
RUN echo "BUILDER: check libz.s* before start" && ls -l /usr/lib/${ARCH}-linux-gnu/libz.so* | |
RUN apt update && apt install -y libgl1 curl python3-pip && apt clean | |
COPY ./dockerfiles/apt_preferences_debian /etc/apt/preferences | |
COPY ./dockerfiles/debian.sources /etc/apt/sources.list.d/debian.sources | |
RUN apt update && apt install -t trixie zlib1g -y && apt clean | |
RUN echo "BUILDER: check libz.s* after install from trixie" && ls -l /usr/lib/${ARCH}-linux-gnu/libz.so* | |
RUN ls -l /etc/apt/sources* /etc/apt/preferences* | |
RUN curl -Lo /usr/local/bin/aws-lambda-rie ${RIE} | |
# poetry installation path is NOT within ${LAMBDA_TASK_ROOT}: not needed for runtime docker image | |
RUN python -m pip install -r ${LAMBDA_TASK_ROOT}/requirements_poetry.txt | |
RUN which poetry && poetry --version && poetry config --list | |
RUN poetry config virtualenvs.path ${LAMBDA_TASK_ROOT} | |
RUN echo "# poetry config --list #" && poetry config --list | |
RUN poetry install --with ${DEPENDENCY_GROUP} --no-root | |
FROM python:3.11-slim-bookworm as runtime | |
ARG ARCH | |
ARG LAMBDA_TASK_ROOT | |
ENV VIRTUAL_ENV=${LAMBDA_TASK_ROOT}/.venv \ | |
PATH="${LAMBDA_TASK_ROOT}/.venv/bin:$PATH" | |
RUN echo "COPY --from=builder_global /usr/lib/${ARCH}-linux-gnu/libGL.so* /usr/lib/${ARCH}-linux-gnu/" | |
COPY --from=builder_global /usr/lib/${ARCH}-linux-gnu/libGL.so* /usr/lib/${ARCH}-linux-gnu/ | |
RUN echo "RUNTIME: check libz.s* before upgrade" && ls -l /usr/lib/${ARCH}-linux-gnu/libz.so* | |
RUN echo "RUNTIME: remove libz.s* to force upgrade" && rm /usr/lib/${ARCH}-linux-gnu/libz.so* | |
COPY --from=builder_global /usr/lib/${ARCH}-linux-gnu/libz.so* /usr/lib/${ARCH}-linux-gnu/ | |
RUN echo "RUNTIME: check libz.s* after copy" && ls -l /usr/lib/${ARCH}-linux-gnu/libz.so* | |
COPY --from=builder_global ${LAMBDA_TASK_ROOT}/.venv ${LAMBDA_TASK_ROOT}/.venv | |
RUN echo "new LAMBDA_TASK_ROOT after hidden venv copy => ${LAMBDA_TASK_ROOT}" | |
RUN ls -ld ${LAMBDA_TASK_ROOT}/ | |
RUN ls -lA ${LAMBDA_TASK_ROOT}/ | |
COPY --from=builder_global /usr/local/bin/aws-lambda-rie /usr/local/bin/aws-lambda-rie | |
RUN chmod +x /usr/local/bin/aws-lambda-rie | |
COPY ./scripts/lambda-entrypoint.sh /lambda-entrypoint.sh | |
RUN chmod +x /lambda-entrypoint.sh | |
### conditional section | |
FROM node:20-slim AS node_fastapi | |
ARG DEPENDENCY_GROUP | |
ENV PNPM_HOME="/pnpm" | |
ENV PATH="$PNPM_HOME:$PATH" | |
RUN corepack enable | |
COPY ./static /appnode | |
WORKDIR /appnode | |
# RUN if [ "${DEPENDENCY_GROUP}" = "fastapi" ]; then echo "pnpm store path:" && pnpm store path; fi | |
FROM node_fastapi AS node_prod_deps | |
ARG DEPENDENCY_GROUP | |
RUN --mount=type=cache,id=pnpm,target=/pnpm/store if [ "${DEPENDENCY_GROUP}" = "fastapi" ]; then \ | |
pnpm install --prod --frozen-lockfile; else \ | |
echo "DEPENDENCY_GROUP 1: ${DEPENDENCY_GROUP} ..."; fi | |
# here multiple conditions concatenated to avoid failing on check | |
RUN if [ "${DEPENDENCY_GROUP}" = "fastapi" ]; then if [ ! -d /appnode/node_modules ]; then echo "no node_modules folder" && exit 1; fi; fi | |
FROM node_fastapi AS node_build | |
ARG DEPENDENCY_GROUP | |
ARG VITE__MAP_DESCRIPTION | |
ARG VITE__SAMGIS_SPACE | |
RUN echo "VITE__MAP_DESCRIPTION:" ${VITE__MAP_DESCRIPTION} | |
RUN echo "VITE__SAMGIS_SPACE:" ${VITE__SAMGIS_SPACE} | |
RUN --mount=type=cache,id=pnpm,target=/pnpm/store if [ "${DEPENDENCY_GROUP}" = "fastapi" ]; then \ | |
pnpm install --frozen-lockfile; else \ | |
echo "DEPENDENCY_GROUP 2: ${DEPENDENCY_GROUP} ..."; fi | |
RUN --mount=type=cache,id=pnpm,target=/pnpm/store if [ "${DEPENDENCY_GROUP}" = "fastapi" ]; then pnpm build; fi | |
RUN --mount=type=cache,id=pnpm,target=/pnpm/store if [ "${DEPENDENCY_GROUP}" = "fastapi" ]; then \ | |
pnpm tailwindcss -i /appnode/src/input.css -o /appnode/dist/output.css; fi | |
RUN if [ "${DEPENDENCY_GROUP}" = "fastapi" ]; then if [ ! -d /appnode/dist ]; then echo "no dist folder" && exit 1; fi; fi | |
FROM runtime | |
ARG FASTAPI_STATIC | |
RUN mkdir ${FASTAPI_STATIC} | |
COPY ./machine_learning_models ${LAMBDA_TASK_ROOT}/machine_learning_models | |
COPY --from=node_prod_deps /appnode/node_modules* ${FASTAPI_STATIC}/node_modules | |
COPY --from=node_build /appnode/dist* ${FASTAPI_STATIC}/dist | |