Dockerfile 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. FROM python:3.10-slim
  2. WORKDIR /app
  3. # https://github.com/aptible/supercronic
  4. ARG TARGETARCH
  5. ENV SUPERCRONIC_VERSION=v0.2.34
  6. RUN set -ex && \
  7. apt-get update && \
  8. # 添加 locales 包
  9. apt-get install -y --no-install-recommends curl ca-certificates locales && \
  10. # 配置中文 locale
  11. sed -i -e 's/# zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/' /etc/locale.gen && \
  12. sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
  13. locale-gen && \
  14. # 下载 supercronic(只支持 amd64 和 arm64)
  15. case ${TARGETARCH} in \
  16. amd64) \
  17. export SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/${SUPERCRONIC_VERSION}/supercronic-linux-amd64; \
  18. export SUPERCRONIC_SHA1SUM=e8631edc1775000d119b70fd40339a7238eece14; \
  19. export SUPERCRONIC=supercronic-linux-amd64; \
  20. ;; \
  21. arm64) \
  22. export SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/${SUPERCRONIC_VERSION}/supercronic-linux-arm64; \
  23. export SUPERCRONIC_SHA1SUM=4ab6343b52bf9da592e8b4bb7ae6eb5a8e21b71e; \
  24. export SUPERCRONIC=supercronic-linux-arm64; \
  25. ;; \
  26. *) \
  27. echo "Unsupported architecture: ${TARGETARCH}. Only amd64 and arm64 are supported."; \
  28. exit 1; \
  29. ;; \
  30. esac && \
  31. echo "Downloading supercronic for ${TARGETARCH} from ${SUPERCRONIC_URL}" && \
  32. # 添加重试机制和超时设置
  33. for i in 1 2 3 4 5; do \
  34. echo "Download attempt $i/5"; \
  35. if curl --fail --silent --show-error --location --retry 3 --retry-delay 2 --connect-timeout 30 --max-time 120 -o "$SUPERCRONIC" "$SUPERCRONIC_URL"; then \
  36. echo "Download successful"; \
  37. break; \
  38. else \
  39. echo "Download attempt $i failed, exit code: $?"; \
  40. if [ $i -eq 5 ]; then \
  41. echo "All download attempts failed"; \
  42. exit 1; \
  43. fi; \
  44. sleep $((i * 2)); \
  45. fi; \
  46. done && \
  47. echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - && \
  48. chmod +x "$SUPERCRONIC" && \
  49. mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" && \
  50. ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic && \
  51. # 验证安装
  52. supercronic -version && \
  53. apt-get remove -y curl && \
  54. apt-get clean && \
  55. rm -rf /var/lib/apt/lists/*
  56. COPY requirements.txt .
  57. RUN pip install --no-cache-dir -r requirements.txt
  58. COPY main.py .
  59. COPY docker/manage.py .
  60. # 复制 entrypoint.sh 并强制转换为 LF 格式
  61. COPY docker/entrypoint.sh /entrypoint.sh.tmp
  62. RUN sed -i 's/\r$//' /entrypoint.sh.tmp && \
  63. mv /entrypoint.sh.tmp /entrypoint.sh && \
  64. chmod +x /entrypoint.sh && \
  65. chmod +x manage.py && \
  66. mkdir -p /app/config /app/output
  67. # 添加 locale 相关环境变量
  68. ENV PYTHONUNBUFFERED=1 \
  69. CONFIG_PATH=/app/config/config.yaml \
  70. FREQUENCY_WORDS_PATH=/app/config/frequency_words.txt \
  71. LANG=zh_CN.UTF-8 \
  72. LANGUAGE=zh_CN:zh:en_US:en \
  73. LC_ALL=zh_CN.UTF-8 \
  74. PYTHONIOENCODING=utf-8
  75. ENTRYPOINT ["/entrypoint.sh"]