Create Dockerfile
Browse files- Dockerfile +129 -0
Dockerfile
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ====================
|
| 2 |
+
# 基礎映像檔設定
|
| 3 |
+
# ====================
|
| 4 |
+
# 使用輕量版 Python(固定具體版本避免意外更新)
|
| 5 |
+
FROM python:3.12.7-slim
|
| 6 |
+
|
| 7 |
+
# 安裝系統相依套件
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
build-essential \
|
| 10 |
+
libglib2.0-0 \
|
| 11 |
+
libgl1-mesa-glx \
|
| 12 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
+
|
| 14 |
+
# ====================
|
| 15 |
+
# 環境變數設定
|
| 16 |
+
# ====================
|
| 17 |
+
# 設定 Python 環境變數以提升安全性和效能
|
| 18 |
+
ENV PYTHONUNBUFFERED=1 \
|
| 19 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 20 |
+
PIP_NO_CACHE_DIR=1 \
|
| 21 |
+
PIP_DISABLE_PIP_VERSION_CHECK=1
|
| 22 |
+
# PYTHONUNBUFFERED=1: 確保 Python 輸出立即顯示
|
| 23 |
+
# PYTHONDONTWRITEBYTECODE=1: 防止生成 .pyc 檔案,減少映像檔大小
|
| 24 |
+
# PIP_NO_CACHE_DIR=1: 禁用 pip 快取,減少映像檔大小
|
| 25 |
+
# PIP_DISABLE_PIP_VERSION_CHECK=1: 禁用 pip 版本檢查,加速安裝
|
| 26 |
+
|
| 27 |
+
# ====================
|
| 28 |
+
# 使用者和群組設定
|
| 29 |
+
# ====================
|
| 30 |
+
# 建立專用的應用程式使用者和群組(早期建立以確保安全)
|
| 31 |
+
RUN groupadd -r appgroup && \
|
| 32 |
+
useradd -r -g appgroup -u 1001 -d /app -s /sbin/nologin appuser
|
| 33 |
+
# -r: 建立系統使用者/群組
|
| 34 |
+
# -u 1001: 指定 UID,避免與 host 使用者衝突
|
| 35 |
+
# -d /app: 設定家目錄
|
| 36 |
+
# -s /sbin/nologin: 禁止 shell 登入,增強安全性
|
| 37 |
+
|
| 38 |
+
# ====================
|
| 39 |
+
# 系統套件安裝
|
| 40 |
+
# ====================
|
| 41 |
+
# 安裝必要的系統相依套件並立即清理,減少映像檔大小和攻擊面
|
| 42 |
+
RUN apt-get update && \
|
| 43 |
+
apt-get install -y --no-install-recommends \
|
| 44 |
+
build-essential \
|
| 45 |
+
libglib2.0-0 \
|
| 46 |
+
libgl1-mesa-glx && \
|
| 47 |
+
apt-get clean && \
|
| 48 |
+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
| 49 |
+
# --no-install-recommends: 只安裝必要套件,不安裝建議套件
|
| 50 |
+
# 立即清理 apt 快取和臨時檔案
|
| 51 |
+
|
| 52 |
+
# ====================
|
| 53 |
+
# 工作目錄和權限設定
|
| 54 |
+
# ====================
|
| 55 |
+
# 設定應用程式工作目錄
|
| 56 |
+
WORKDIR /app
|
| 57 |
+
|
| 58 |
+
# 建立必要目錄並設定適當權限(避免使用危險的 777 權限)
|
| 59 |
+
# RUN mkdir -p /app/tmp /app/logs /app/data && \
|
| 60 |
+
# chown -R appuser:appgroup /app && \
|
| 61 |
+
# chmod 777 /app && \
|
| 62 |
+
# chmod 777 /app/tmp /app/logs /app/data
|
| 63 |
+
# 755: 擁有者可讀寫執行,群組和其他人可讀執行
|
| 64 |
+
# 750: 擁有者可讀寫執行,群組可讀執行,其他人無權限
|
| 65 |
+
|
| 66 |
+
# ====================
|
| 67 |
+
# Python 依賴安裝
|
| 68 |
+
# ====================
|
| 69 |
+
# 先複製 requirements.txt(利用 Docker 層快取優化)
|
| 70 |
+
COPY --chown=appuser:appgroup requirements.txt .
|
| 71 |
+
# --chown: 複製時直接設定擁有者,避免額外的 chown 指令
|
| 72 |
+
|
| 73 |
+
# 升級 pip 並安裝 Python 套件
|
| 74 |
+
RUN pip install --upgrade pip && \
|
| 75 |
+
pip install --no-cache-dir -r requirements.txt
|
| 76 |
+
# --no-cache-dir: 不使用快取,減少映像檔大小
|
| 77 |
+
|
| 78 |
+
# ====================
|
| 79 |
+
# 應用程式代碼複製
|
| 80 |
+
# ====================
|
| 81 |
+
# 複製應用程式代碼並設定擁有者
|
| 82 |
+
COPY --chown=appuser:appgroup . .
|
| 83 |
+
|
| 84 |
+
# ====================
|
| 85 |
+
# 清理和優化
|
| 86 |
+
# ====================
|
| 87 |
+
# 移除不必要的 Python 快取檔案,進一步減少映像檔大小
|
| 88 |
+
RUN find /app -name "*.pyc" -delete && \
|
| 89 |
+
find /app -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null || true
|
| 90 |
+
# 刪除所有 .pyc 檔案和 __pycache__ 目錄
|
| 91 |
+
# 2>/dev/null || true: 忽略錯誤訊息
|
| 92 |
+
|
| 93 |
+
# ====================
|
| 94 |
+
# 使用者切換
|
| 95 |
+
# ====================
|
| 96 |
+
# 切換到非特權使用者執行應用程式(重要安全措施)
|
| 97 |
+
USER appuser
|
| 98 |
+
|
| 99 |
+
# ====================
|
| 100 |
+
# 網路設定
|
| 101 |
+
# ====================
|
| 102 |
+
# 暴露應用程式端口
|
| 103 |
+
EXPOSE 7860
|
| 104 |
+
|
| 105 |
+
# ====================
|
| 106 |
+
# 健康檢查
|
| 107 |
+
# ====================
|
| 108 |
+
# 設定容器健康檢查,確保應用程式正常運作
|
| 109 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
| 110 |
+
CMD curl -f http://localhost:7860/_stcore/health || exit 1
|
| 111 |
+
# --interval: 檢查間隔
|
| 112 |
+
# --timeout: 檢查超時時間
|
| 113 |
+
# --start-period: 啟動寬限期
|
| 114 |
+
# --retries: 重試次數
|
| 115 |
+
|
| 116 |
+
# ====================
|
| 117 |
+
# 應用程式啟動
|
| 118 |
+
# ====================
|
| 119 |
+
# 啟動 Streamlit 應用程式(啟用安全功能)
|
| 120 |
+
CMD ["gradio", "app.py", \
|
| 121 |
+
# "--server.port=7860", \
|
| 122 |
+
# "--server.address=0.0.0.0", \
|
| 123 |
+
# "--server.enableXsrfProtection=false", \
|
| 124 |
+
# "--server.enableCORS=false", \
|
| 125 |
+
# "--server.headless=true"
|
| 126 |
+
]
|
| 127 |
+
# enableXsrfProtection=true: 啟用 CSRF 保護
|
| 128 |
+
# enableCORS=false: 禁用跨域請求,增強安全性
|
| 129 |
+
# headless=true: 無頭模式,適合容器環境
|