File size: 8,168 Bytes
0914710
 
 
bfaaa1e
0914710
 
 
 
 
1355a26
0914710
 
6d98d37
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a37cc7
0914710
ac6ac74
009261a
 
 
 
 
65803bd
0914710
6d98d37
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65803bd
7650159
 
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65803bd
 
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86f85c7
 
 
 
 
0914710
ac6ac74
 
65803bd
 
009261a
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
fcb8c81
0914710
7d75d30
bccfe66
5e5ed73
0c9e62a
0914710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcb8c81
0914710
86f85c7
 
 
 
 
 
 
 
0914710
0c9e62a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# 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 DEPENDENCY_GROUP=fastapi


FROM pytorch/pytorch:2.2.2-cuda12.1-cudnn8-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
ARG DEPENDENCY_GROUP

RUN echo "ARCH: $ARCH ..."

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 upgrade -y && apt install -y libgl1 curl python3-pip git-lfs && apt clean
COPY ./dockerfiles/apt_preferences_ubuntu /etc/apt/preferences
COPY ./dockerfiles/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources
#RUN echo "run update noble..."
#RUN apt update
#RUN apt update && apt install -t noble zlib1g -y
RUN git lfs install
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}/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 --with ${DEPENDENCY_GROUP} --no-root

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.2.2-cuda12.1-cudnn8-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 "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


### 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 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


# 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}

COPY samgis_lisa_on_cuda ${LAMBDA_TASK_ROOT}/samgis_lisa_on_cuda
COPY wrappers ${LAMBDA_TASK_ROOT}/wrappers
COPY scripts ${LAMBDA_TASK_ROOT}/scripts
RUN chmod +x ${LAMBDA_TASK_ROOT}/scripts/entrypoint.sh
RUN chmod +x  ${LAMBDA_TASK_ROOT}/scripts/docker_entrypoint.sh
RUN ls -l ${LAMBDA_TASK_ROOT}/scripts/entrypoint.sh ${LAMBDA_TASK_ROOT}/scripts/docker_entrypoint.sh

RUN ls -l /usr/bin/which
RUN /usr/bin/which python
RUN python -v
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 onnxruntime"
RUN python -c "import rasterio"
RUN python -c "import uvicorn"
RUN df -h
RUN ls -l ${LAMBDA_TASK_ROOT}/samgis_lisa_on_cuda/
RUN ls -l ${LAMBDA_TASK_ROOT}/wrappers/
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

CMD ["/var/task/scripts/docker_entrypoint.sh"]