Dockerfile 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # 1. Should I use `syntax` parser directive?
  2. # 2. Can I use `COPY --link` to optimize caching?
  3. # 3. Create a non-root user for deployment?
  4. # 4. Should I use APT repository mirrors?
  5. # 5. Use cache mounts for `pip`?
  6. ARG DEVICE_TYPE=gpu
  7. ARG ENV_TYPE=prod
  8. ARG PIP_INDEX_URL=https://pypi.org/simple
  9. # GPU base
  10. # nvcr.io/nvidia/cuda:11.8.0-devel-ubuntu20.04
  11. # We use the `devel` version because the CUDA toolchain is needed for JIT.
  12. # However, can we use a smaller base image by not including tools and
  13. # libraries that are not required?
  14. FROM nvcr.io/nvidia/cuda@sha256:03681bbd11fea044ff3e3a5d65e190a6f935af30c56e03e740b27e4563f61e0f AS gpu-base
  15. # CPU base
  16. # ubuntu:20.04
  17. FROM ubuntu:20.04@sha256:8feb4d8ca5354def3d8fce243717141ce31e2c428701f6682bd2fafe15388214 AS cpu-base
  18. # Base
  19. FROM ${DEVICE_TYPE}-base AS base
  20. ARG PIP_INDEX_URL
  21. # Should I add `SHELL ["/bin/bash", "-c"]`?
  22. ENV DEBIAN_FRONTEND=noninteractive
  23. RUN mkdir /paddlex
  24. RUN apt-get update \
  25. && apt-get install -y --no-install-recommends software-properties-common \
  26. && add-apt-repository -y ppa:deadsnakes/ppa \
  27. && apt-get update \
  28. && apt-get install -y --no-install-recommends python3.10 python3.10-venv \
  29. && python3.10 -m venv /paddlex/py310 \
  30. && rm -rf /var/lib/apt/lists/*
  31. ENV PATH="/paddlex/py310/bin:${PATH}"
  32. ENV PIP_DISABLE_PIP_VERSION_CHECK=1
  33. ENV PIP_NO_CACHE_DIR=0
  34. ENV PYTHONUNBUFFERED=1
  35. ENV PYTHONDONTWRITEBYTECODE=1
  36. ENV PIP_INDEX_URL=${PIP_INDEX_URL}
  37. # Requirement collection
  38. FROM base AS rc
  39. RUN python -m pip install pip-tools==7.4.1
  40. # `ENTRYPOINT` and `CMD`?
  41. # Build stage 1
  42. # Can we use another base image?
  43. FROM base AS gpu-build1
  44. ARG DEVICE_TYPE=gpu
  45. RUN apt-get update \
  46. && apt-get install -y --no-install-recommends wget unzip \
  47. && rm -rf /var/lib/apt/lists/*
  48. RUN mkdir -p /paddlex/libs /paddlex/tensorrt
  49. RUN --mount=type=bind,source=./cudnn-linux-x86_64-8.9.7.29_cuda11-archive.tar.xz,target=/tmp/cudnn-linux-x86_64-8.9.7.29_cuda11-archive.tar.xz \
  50. tar -xf /tmp/cudnn-linux-x86_64-8.9.7.29_cuda11-archive.tar.xz -C /paddlex \
  51. && mv /paddlex/cudnn-linux-x86_64-8.9.7.29_cuda11-archive/lib/* /paddlex/libs/
  52. RUN --mount=type=bind,source=./TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz,target=/tmp/TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz \
  53. tar -xf /tmp/TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz -C /paddlex \
  54. && mv /paddlex/TensorRT-8.6.1.6 /paddlex/tensorrt \
  55. && rm -rf /paddlex/tensorrt/data /paddlex/tensorrt/doc /paddlex/tensorrt/python /paddlex/tensorrt/samples
  56. RUN wget -P /paddlex "https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/deploy/deps/tritonserver/tritonserver-2.15.0-${DEVICE_TYPE}.zip" \
  57. && unzip "/paddlex/tritonserver-2.15.0-${DEVICE_TYPE}.zip" -d /paddlex \
  58. && mv "/paddlex/tritonserver-2.15.0-${DEVICE_TYPE}" /paddlex/tritonserver \
  59. && rm "/paddlex/tritonserver-2.15.0-${DEVICE_TYPE}.zip"
  60. COPY ./paddlex-hps-server /paddlex/paddlex_hps_libs/paddlex-hps-server
  61. RUN python -m pip wheel -w /paddlex/paddlex_hps_wheels --no-deps /paddlex/paddlex_hps_libs/paddlex-hps-server
  62. # Build Stage for CPU
  63. FROM base AS cpu-build1
  64. ARG DEVICE_TYPE=cpu
  65. RUN apt-get update \
  66. && apt-get install -y --no-install-recommends wget unzip \
  67. && rm -rf /var/lib/apt/lists/*
  68. RUN mkdir -p /paddlex/libs
  69. RUN wget -P /paddlex "https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/deploy/deps/tritonserver/tritonserver-2.15.0-${DEVICE_TYPE}.zip" \
  70. && unzip "/paddlex/tritonserver-2.15.0-${DEVICE_TYPE}.zip" -d /paddlex \
  71. && mv "/paddlex/tritonserver-2.15.0-${DEVICE_TYPE}" /paddlex/tritonserver \
  72. && rm "/paddlex/tritonserver-2.15.0-${DEVICE_TYPE}.zip"
  73. COPY ./paddlex-hps-server /paddlex/paddlex_hps_libs/paddlex-hps-server
  74. RUN python -m pip wheel -w /paddlex/paddlex_hps_wheels --no-deps /paddlex/paddlex_hps_libs/paddlex-hps-server
  75. FROM ${DEVICE_TYPE}-build1 AS build2
  76. ARG DEVICE_TYPE
  77. ARG ENV_TYPE
  78. RUN --mount=type=bind,source=requirements/${DEVICE_TYPE}.txt,target=/tmp/requirements.txt \
  79. --mount=type=bind,source=requirements/${DEVICE_TYPE}_hpi.txt,target=/tmp/hpi_requirements.txt \
  80. --mount=type=bind,source=requirements/${DEVICE_TYPE}_dev.txt,target=/tmp/dev_requirements.txt \
  81. python -m pip install --requirement /tmp/requirements.txt --requirement /tmp/hpi_requirements.txt \
  82. && if [ "${ENV_TYPE}" = 'dev' ]; then \
  83. python -m pip install --requirement /tmp/dev_requirements.txt; \
  84. fi
  85. RUN python -m pip install --no-deps /paddlex/paddlex_hps_wheels/*.whl
  86. # GPU Deployment
  87. FROM base AS gpu-deployment
  88. RUN apt-get update \
  89. && apt-get install -y --no-install-recommends datacenter-gpu-manager \
  90. && rm -rf /var/lib/apt/lists/*
  91. COPY --from=build2 --chown=paddlex:paddlex /paddlex/tensorrt /paddlex/tensorrt
  92. ENV LD_LIBRARY_PATH="/paddlex/tensorrt/lib:${LD_LIBRARY_PATH}"
  93. RUN mkdir /paddlex/libs
  94. RUN ln -s /usr/local/cuda-11.8/targets/x86_64-linux/lib/libcublas.so.11 /paddlex/libs/libcublas.so \
  95. && ln -s /usr/local/cuda-11.8/targets/x86_64-linux/lib/libcufft.so.10 /paddlex/libs/libcufft.so \
  96. && ln -s /usr/local/cuda-11.8/targets/x86_64-linux/lib/libcurand.so.10 /paddlex/libs/libcurand.so \
  97. && ln -s /usr/local/cuda-11.8/targets/x86_64-linux/lib/libcusolver.so.11 /paddlex/libs/libcusolver.so \
  98. && ln -s /usr/local/cuda-11.8/targets/x86_64-linux/lib/libcusparse.so.11 /paddlex/libs/libcusparse.so
  99. # CPU Deployment
  100. FROM base AS cpu-deployment
  101. # Deployment
  102. FROM ${DEVICE_TYPE}-deployment AS deployment
  103. # Should we use an environment variable to store the version?
  104. RUN groupadd -g 1000 paddlex \
  105. && useradd -m -s /bin/bash -u 1000 -g 1000 paddlex
  106. # Not sure if all these deps are necessary
  107. RUN apt-get update \
  108. && apt-get install -y --no-install-recommends libre2-5 libssl1.1 libb64-0d libnuma1 libarchive13 python3.10-dev libgl1-mesa-glx libglib2.0-0 libgomp1 ccache binutils build-essential texlive texlive-latex-base texlive-latex-extra \
  109. && rm -rf /var/lib/apt/lists/*
  110. COPY --from=build2 --chown=paddlex:paddlex /paddlex/libs /paddlex/libs
  111. # Should I use `ldconfig`?
  112. ENV LD_LIBRARY_PATH="/paddlex/libs:${LD_LIBRARY_PATH}"
  113. COPY --from=build2 --chown=paddlex:paddlex /paddlex/tritonserver /opt/tritonserver
  114. ENV PATH="/opt/tritonserver/bin:${PATH}"
  115. # Is the merging safe?
  116. COPY --from=build2 --chown=paddlex:paddlex /paddlex/py310 /paddlex/py310
  117. RUN mkdir /paddlex/var \
  118. && chown paddlex:paddlex /paddlex/var
  119. USER paddlex
  120. USER root
  121. CMD ["/bin/bash"]