Dockerfile 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. FROM python:3.12-slim-bookworm
  2. WORKDIR /app
  3. # Latest releases available at https://github.com/aptible/supercronic/releases
  4. ARG TARGETARCH
  5. ENV SUPERCRONIC_VERSION=v0.2.39
  6. RUN set -ex && \
  7. apt-get update && \
  8. apt-get install -y --no-install-recommends curl ca-certificates && \
  9. case ${TARGETARCH} in \
  10. amd64) \
  11. export SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/${SUPERCRONIC_VERSION}/supercronic-linux-amd64; \
  12. export SUPERCRONIC_SHA1SUM=c98bbf82c5f648aaac8708c182cc83046fe48423; \
  13. export SUPERCRONIC=supercronic-linux-amd64; \
  14. ;; \
  15. arm64) \
  16. export SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/${SUPERCRONIC_VERSION}/supercronic-linux-arm64; \
  17. export SUPERCRONIC_SHA1SUM=5ef4ccc3d43f12d0f6c3763758bc37cc4e5af76e; \
  18. export SUPERCRONIC=supercronic-linux-arm64; \
  19. ;; \
  20. *) \
  21. echo "Unsupported architecture: ${TARGETARCH}"; \
  22. exit 1; \
  23. ;; \
  24. esac && \
  25. echo "Downloading supercronic for ${TARGETARCH} from ${SUPERCRONIC_URL}" && \
  26. # 重试机制:最多3次,每次超时30秒
  27. for i in 1 2 3; do \
  28. echo "Download attempt $i/3"; \
  29. if curl -fsSL --connect-timeout 30 --max-time 60 -o "$SUPERCRONIC" "$SUPERCRONIC_URL"; then \
  30. echo "Download successful"; \
  31. break; \
  32. else \
  33. echo "Download attempt $i failed"; \
  34. if [ $i -eq 3 ]; then \
  35. echo "All download attempts failed"; \
  36. exit 1; \
  37. fi; \
  38. sleep 2; \
  39. fi; \
  40. done && \
  41. echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - && \
  42. chmod +x "$SUPERCRONIC" && \
  43. mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" && \
  44. ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic && \
  45. supercronic -version && \
  46. apt-get remove -y curl && \
  47. apt-get clean && \
  48. rm -rf /var/lib/apt/lists/*
  49. # 从官方镜像拷贝 uv 二进制
  50. COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
  51. # 先安装依赖(利用 Docker 层缓存)
  52. COPY pyproject.toml uv.lock ./
  53. RUN --mount=type=cache,target=/root/.cache/uv \
  54. uv sync --locked --no-install-project --no-dev
  55. # 再复制项目代码并安装项目本身
  56. COPY docker/manage.py .
  57. COPY trendradar/ ./trendradar/
  58. RUN --mount=type=cache,target=/root/.cache/uv \
  59. uv sync --locked --no-dev
  60. ENV PATH="/app/.venv/bin:$PATH"
  61. # 复制 entrypoint.sh 并强制转换为 LF 格式
  62. COPY docker/entrypoint.sh /entrypoint.sh.tmp
  63. RUN sed -i 's/\r$//' /entrypoint.sh.tmp && \
  64. mv /entrypoint.sh.tmp /entrypoint.sh && \
  65. chmod +x /entrypoint.sh && \
  66. chmod +x manage.py && \
  67. mkdir -p /app/config /app/output
  68. ENV PYTHONUNBUFFERED=1 \
  69. CONFIG_PATH=/app/config/config.yaml \
  70. FREQUENCY_WORDS_PATH=/app/config/frequency_words.txt
  71. ENTRYPOINT ["/entrypoint.sh"]