|
| 1 | +# Copyright (c) MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +# This is a slimmed down version of the MONAI Docker image using a smaller base image and multi-stage building. Not all |
| 13 | +# NVIDIA tools will be present but all libraries and compiled code are included. This image isn't provided through |
| 14 | +# Dockerhub so users must build locally: `docker build -t monai_slim -f Dockerfile.slim .` |
| 15 | +# Containers may require more shared memory, eg.: `docker run -ti --rm --gpus all --shm-size=10gb monai_slim /bin/bash` |
| 16 | + |
| 17 | +ARG IMAGE=debian:12-slim |
| 18 | + |
| 19 | +FROM ${IMAGE} AS build |
| 20 | + |
| 21 | +ARG TORCH_CUDA_ARCH_LIST="7.5 8.0 8.6 8.9 9.0+PTX" |
| 22 | + |
| 23 | +ENV DEBIAN_FRONTEND=noninteractive |
| 24 | +ENV APT_INSTALL="apt install -y --no-install-recommends" |
| 25 | + |
| 26 | +RUN apt update && apt upgrade -y && \ |
| 27 | + ${APT_INSTALL} ca-certificates python3-pip python-is-python3 git wget libopenslide0 unzip python3-dev && \ |
| 28 | + wget https://developer.download.nvidia.com/compute/cuda/repos/debian12/x86_64/cuda-keyring_1.1-1_all.deb && \ |
| 29 | + dpkg -i cuda-keyring_1.1-1_all.deb && \ |
| 30 | + apt update && \ |
| 31 | + ${APT_INSTALL} cuda-toolkit-12 && \ |
| 32 | + rm -rf /usr/lib/python*/EXTERNALLY-MANAGED /var/lib/apt/lists/* && \ |
| 33 | + python -m pip install --upgrade --no-cache-dir --no-build-isolation pip |
| 34 | + |
| 35 | +# TODO: remark for issue [revise the dockerfile](https://github.com/zarr-developers/numcodecs/issues/431) |
| 36 | +RUN if [[ $(uname -m) =~ "aarch64" ]]; then \ |
| 37 | + CFLAGS="-O3" DISABLE_NUMCODECS_SSE2=true DISABLE_NUMCODECS_AVX2=true python -m pip install numcodecs; \ |
| 38 | + fi |
| 39 | + |
| 40 | +# NGC Client |
| 41 | +WORKDIR /opt/tools |
| 42 | +ARG NGC_CLI_URI="https://ngc.nvidia.com/downloads/ngccli_linux.zip" |
| 43 | +RUN wget -q ${NGC_CLI_URI} && unzip ngccli_linux.zip && chmod u+x ngc-cli/ngc && \ |
| 44 | + find ngc-cli/ -type f -exec md5sum {} + | LC_ALL=C sort | md5sum -c ngc-cli.md5 && \ |
| 45 | + rm -rf ngccli_linux.zip ngc-cli.md5 |
| 46 | + |
| 47 | +WORKDIR /opt/monai |
| 48 | + |
| 49 | +# copy relevant parts of repo |
| 50 | +COPY requirements.txt requirements-min.txt requirements-dev.txt versioneer.py setup.py setup.cfg pyproject.toml ./ |
| 51 | +COPY LICENSE CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md README.md MANIFEST.in runtests.sh ./ |
| 52 | +COPY tests ./tests |
| 53 | +COPY monai ./monai |
| 54 | + |
| 55 | +# install full deps |
| 56 | +RUN python -m pip install --no-cache-dir --no-build-isolation -r requirements-dev.txt |
| 57 | + |
| 58 | +# compile ext |
| 59 | +RUN CUDA_HOME=/usr/local/cuda FORCE_CUDA=1 USE_COMPILED=1 BUILD_MONAI=1 python setup.py develop |
| 60 | + |
| 61 | +# recreate the image without the installed CUDA packages then copy the installed MONAI and Python directories |
| 62 | +FROM ${IMAGE} AS build2 |
| 63 | + |
| 64 | +ENV DEBIAN_FRONTEND=noninteractive |
| 65 | +ENV APT_INSTALL="apt install -y --no-install-recommends" |
| 66 | + |
| 67 | +RUN apt update && apt upgrade -y && \ |
| 68 | + ${APT_INSTALL} ca-certificates python3-pip python-is-python3 git libopenslide0 && \ |
| 69 | + apt clean && \ |
| 70 | + rm -rf /usr/lib/python*/EXTERNALLY-MANAGED /var/lib/apt/lists/* && \ |
| 71 | + python -m pip install --upgrade --no-cache-dir --no-build-isolation pip |
| 72 | + |
| 73 | +COPY --from=build /opt/monai /opt/monai |
| 74 | +COPY --from=build /opt/tools /opt/tools |
| 75 | +ARG PYTHON_VERSION=3.11 |
| 76 | +COPY --from=build /usr/local/lib/python${PYTHON_VERSION}/dist-packages /usr/local/lib/python${PYTHON_VERSION}/dist-packages |
| 77 | +COPY --from=build /usr/local/bin /usr/local/bin |
| 78 | + |
| 79 | +RUN rm -rf /opt/monai/build /opt/monai/monai.egg-info && \ |
| 80 | + find /opt /usr/local/lib -type d -name __pycache__ -exec rm -rf {} + |
| 81 | + |
| 82 | +# flatten all layers down to one |
| 83 | +FROM ${IMAGE} |
| 84 | +LABEL maintainer="monai.contact@gmail.com" |
| 85 | + |
| 86 | +COPY --from=build2 / / |
| 87 | + |
| 88 | +WORKDIR /opt/monai |
| 89 | + |
| 90 | +ENV PATH=${PATH}:/opt/tools:/opt/tools/ngc-cli |
| 91 | +ENV POLYGRAPHY_AUTOINSTALL_DEPS=1 |
| 92 | +ENV CUDA_HOME=/usr/local/cuda |
| 93 | +ENV BUILD_MONAI=1 |
0 commit comments