samgis-lisa-on-cuda / Dockerfile
alessandro trinca tornidor
ci: docker, try installing nodejs from noble from sources using ubuntu.sources file
422b339
# 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
FROM pytorch/pytorch:2.4.1-cuda12.4-cudnn9-runtime as builder_global
LABEL authors="alessandro@trinca.tornidor.com"
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
RUN echo "ARCH: $ARCH ..."
RUN echo "ARG POETRY_CACHE_DIR: ${POETRY_CACHE_DIR} ..."
RUN echo "ARG PYTHONPATH: $PYTHONPATH ..."
# Set working directory to function root directory
WORKDIR ${LAMBDA_TASK_ROOT}
RUN apt update && apt install git git-lfs software-properties-common -y
RUN git lfs install
RUN git clone https://huggingface.co/spaces/aletrn/samgis-lisa-on-cuda ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda && \
ls -l ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/dockerfiles/apt_preferences_ubuntu && \
cp ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/dockerfiles/apt_preferences_ubuntu /etc/apt/preferences && \
ls -l ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/pyproject.toml && \
cp ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/pyproject.toml ${LAMBDA_TASK_ROOT}/pyproject.toml && \
ls -l ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/poetry.lock && \
cp ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/poetry.lock ${LAMBDA_TASK_ROOT}/poetry.lock
COPY ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/dockerfiles/ubuntu.sources /etc/apt/ubuntu.sources
RUN echo "ls samgis-lisa-on-cuda ..."
RUN ls -l /etc/apt/preferences ${LAMBDA_TASK_ROOT}/pyproject.toml ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda
RUN ls -l ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/static
RUN add-apt-repository "deb http://archive.ubuntu.com/ubuntu jammy main universe restricted multiverse" && \
add-apt-repository "deb http://archive.ubuntu.com/ubuntu jammy-security main universe restricted multiverse" && \
add-apt-repository "deb http://archive.ubuntu.com/ubuntu jammy-updates main universe restricted multiverse" && \
add-apt-repository "deb http://archive.ubuntu.com/ubuntu noble main universe restricted multiverse" && \
add-apt-repository "deb http://archive.ubuntu.com/ubuntu noble-security main universe restricted multiverse" && \
add-apt-repository "deb http://archive.ubuntu.com/ubuntu noble-updates main universe restricted multiverse"
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 upgrade -y && apt install -y libgl1 curl python3-pip && apt clean
#RUN echo "run update noble..."
#RUN apt update
#RUN apt update && apt install -t noble zlib1g -y
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*
# poetry installation path is NOT within ${LAMBDA_TASK_ROOT}: not needed for runtime docker image
RUN python -m pip install -r ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/requirements_poetry.txt
RUN which poetry && poetry --version && poetry config --list
RUN poetry config virtualenvs.path ${LAMBDA_TASK_ROOT}
RUN poetry config installer.max-workers 7
RUN echo "# poetry config --list #" && poetry config --list
RUN ls -ld ${LAMBDA_TASK_ROOT}/
#RUN . ${LAMBDA_TASK_ROOT}/.venv/bin/activate && ${LAMBDA_TASK_ROOT}/.venv/bin/python --version && ${LAMBDA_TASK_ROOT}/.venv/bin/python -m pip install pip wheel setuptools --upgrade
RUN poetry run python -m pip install pip wheel setuptools --upgrade
RUN poetry install --no-root --no-cache
RUN git clone https://huggingface.co/aletrn/sam-quantized/ ${LAMBDA_TASK_ROOT}/sam-quantized
RUN git clone -n --depth=1 --filter=tree:0 https://huggingface.co/spaces/aletrn/lisa-on-cuda ${LAMBDA_TASK_ROOT}/lisa-on-cuda && \
cd ${LAMBDA_TASK_ROOT}/lisa-on-cuda && \
git sparse-checkout set --no-cone resources && \
git checkout
WORKDIR ${LAMBDA_TASK_ROOT}
RUN ls -l ${LAMBDA_TASK_ROOT}/*
FROM pytorch/pytorch:2.4.1-cuda12.4-cudnn9-runtime 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 "COPY --from=builder_global ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/scripts/docker_entrypoint.sh ${LAMBDA_TASK_ROOT}/"
COPY --from=builder_global ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/scripts/docker_entrypoint.sh ${LAMBDA_TASK_ROOT}/
RUN echo "COPY --from=builder_global ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/scripts/entrypoint.sh ${LAMBDA_TASK_ROOT}/"
COPY --from=builder_global ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/scripts/entrypoint.sh ${LAMBDA_TASK_ROOT}/
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}/
RUN echo "content of LAMBDA_TASK_ROOT/.venv => ${LAMBDA_TASK_ROOT}/.venv"
RUN ls -ld ${LAMBDA_TASK_ROOT}/.venv
RUN ls -lA ${LAMBDA_TASK_ROOT}/.venv
RUN ls -lA ${LAMBDA_TASK_ROOT}/docker_entrypoint.sh
RUN ls -lA ${LAMBDA_TASK_ROOT}/entrypoint.sh
RUN cat /etc/lsb-release
# deb-src http://it.archive.ubuntu.com/ubuntu/ noble main universe restricted multiverse
# add-apt-repository "deb http://archive.ubuntu.com/ubuntu noble main universe restricted multiverse"
RUN echo "install software-properties-common ..."
COPY --from=builder_global /etc/apt/ubuntu.sources /etc/apt/ubuntu.sources
RUN apt update && apt install software-properties-common -y
RUN echo "install nodejs from source ..."
RUN add-apt-repository "deb-src http://archive.ubuntu.com/ubuntu noble main universe restricted multiverse"
RUN apt update && apt build-dep nodejs && apt -b source -t noble nodejs && apt remove software-properties-common -y && \
apt autoremove && apt clean
RUN which node
RUN which npm
### conditional section
FROM node:20-slim AS node_fastapi
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ARG LAMBDA_TASK_ROOT
RUN corepack enable
COPY --from=builder_global ${LAMBDA_TASK_ROOT}/samgis-lisa-on-cuda/static /appnode
WORKDIR /appnode
# RUN echo "pnpm store path:" && pnpm store path
RUN ls -l /appnode
RUN ls -l /appnode/list_files.html
FROM node_fastapi AS node_prod_deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
# here multiple conditions concatenated to avoid failing on check
RUN if [ ! -d /appnode/node_modules ]; then echo "no node_modules folder" && exit 1; fi
FROM node_fastapi AS node_build
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 pnpm install --frozen-lockfile
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm tailwindcss -i /appnode/src/input.css -o /appnode/dist/output.css
RUN if [ ! -d /appnode/dist ]; then echo "no dist folder" && exit 1; fi
FROM runtime
ARG FASTAPI_STATIC
RUN echo "show disk space, df -h ..."
RUN df -h
RUN echo "creating FASTAPI_STATIC folder: ${FASTAPI_STATIC}, use `mkdir -p` to avoid failure if missing parent folder ..."
RUN mkdir -p ${FASTAPI_STATIC}
COPY --from=builder_global ${LAMBDA_TASK_ROOT}/sam-quantized/machine_learning_models \
${LAMBDA_TASK_ROOT}/machine_learning_models
RUN ls -ld ${LAMBDA_TASK_ROOT}/machine_learning_models
RUN ls -lh ${LAMBDA_TASK_ROOT}/machine_learning_models
COPY --from=builder_global ${LAMBDA_TASK_ROOT}/lisa-on-cuda/resources ${LAMBDA_TASK_ROOT}/resources
COPY --from=node_prod_deps /appnode/node_modules* ${FASTAPI_STATIC}/node_modules
COPY --from=node_build /appnode/dist* ${FASTAPI_STATIC}/dist
COPY --from=node_build /appnode/list_files.html ${FASTAPI_STATIC}/list_files.html
RUN ls -l ${FASTAPI_STATIC}/
RUN ls -l ${FASTAPI_STATIC}/list_files.html
# Include global arg in this stage of the build
ARG LAMBDA_TASK_ROOT="/var/task"
ARG PYTHONPATH="${LAMBDA_TASK_ROOT}:${PYTHONPATH}:/usr/local/lib/python3/dist-packages"
ENV VIRTUAL_ENV=${LAMBDA_TASK_ROOT}/.venv \
PATH="${LAMBDA_TASK_ROOT}/.venv/bin:$PATH"
ENV IS_AWS_LAMBDA=""
# Set working directory to function root directory
WORKDIR ${LAMBDA_TASK_ROOT}
RUN ls -l /usr/bin/which
RUN /usr/bin/which python
RUN python --version
RUN node --version
RUN npm --version
RUN echo "PYTHONPATH: ${PYTHONPATH}."
RUN echo "PATH: ${PATH}."
RUN echo "LAMBDA_TASK_ROOT: ${LAMBDA_TASK_ROOT}."
RUN ls -l ${LAMBDA_TASK_ROOT}
RUN ls -ld ${LAMBDA_TASK_ROOT}
RUN ls -l ${LAMBDA_TASK_ROOT}/machine_learning_models
RUN python -c "import sys; print(sys.path)"
RUN python -c "import cv2"
RUN python -c "import fastapi"
RUN python -c "import geopandas"
RUN python -c "import loguru"
RUN python -c "import rasterio"
RUN python -c "import uvicorn"
RUN df -h
RUN echo "LAMBDA_TASK_ROOT /static/:"
RUN ls -l ${LAMBDA_TASK_ROOT}/static/ || true
RUN ls -l ${LAMBDA_TASK_ROOT}/static/dist || true
RUN ls -l ${LAMBDA_TASK_ROOT}/static/node_modules || true
RUN echo "FASTAPI_STATIC:"
RUN ls -l ${FASTAPI_STATIC}/ || true
RUN ls -l ${FASTAPI_STATIC}/dist || true
RUN ls -l ${FASTAPI_STATIC}/node_modules || true
RUN ls -ld ${LAMBDA_TASK_ROOT}/
RUN ls -lA ${LAMBDA_TASK_ROOT}/
RUN ls -l ${LAMBDA_TASK_ROOT}/.venv
RUN ls -l ${LAMBDA_TASK_ROOT}/.venv/bin/activate
#CMD [
# "source", "/var/task/.venv/bin/activate", "&&",
# "uvicorn", "app:app", "--host","0.0.0.0", "--port", "7860"
#]
# CMD ["/usr/bin/bash", "-c", "source /var/task/.venv/bin/activate && uvicorn app:app --host 0.0.0.0 --port 7860"]
CMD ["/var/task/docker_entrypoint.sh"]