Dockerfile
· 2.2 KiB · Docker
Raw
# Dockerfile
# 使用 Python 官方的 slim-bookworm 映像作為基礎 (Python 3.12, Debian 12)
# slim 版本比較輕量,適合部署
FROM python:3.12-slim-bookworm
# 安裝系統依賴套件。這些是常見用於網頁自動化 (Playwright) 和影像處理 (OpenCV) 的依賴。
# 請根據你的專案需求,移除不需要的套件,以減少映像檔大小。
# apt-get update 用來更新套件清單,apt-get install -y --no-install-recommends 安裝指定的套件
# --no-install-recommends 會避免安裝非必要的建議套件
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
libsecret-1-0 \
libgtk-3-0 \
libgdk-pixbuf2.0-0 \
libxss1 \
libasound2 \
libdrm-dev \
libgbm-dev \
libnss3-dev \
libatk-bridge2.0-0 \
libcups2 \
libxrandr2 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrender1 \
libxtst6 \
curl \
gnupg \
ca-certificates \
libgl1 \
tesseract-ocr \
tesseract-ocr-eng \
&& rm -rf /var/lib/apt/lists/* # 清理 apt 快取,讓映像檔更小
# 設定應用程式在容器內的工作目錄
WORKDIR /app
# 複製你的 Python 依賴清單到工作目錄
COPY requirements.txt ./
# 安裝 Python 依賴套件
# --no-cache-dir 會避免快取 pip 下載的套件,再次減少映像檔大小
RUN pip install --no-cache-dir -r requirements.txt
# 如果你的應用程式需要用到 Playwright,這裡會安裝其瀏覽器驅動。
# 如果不需要,你可以把這行註解掉或移除。
# 這會把瀏覽器安裝到 /root/.cache/ms-playwright
RUN python -m playwright install chromium firefox webkit
# 如果使用 Playwright,設定環境變數,讓 Playwright 知道瀏覽器在哪裡
# 如果不需要 Playwright,這行也可以移除。
ENV PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright
# 複製你的應用程式碼到 Image 中
# 通常會把整個專案複製進去
# 請記得更新這裡的來源和目的地,例如 `COPY . .` 如果你的 Dockerfile 在專案根目錄
# COPY . .
# 定義容器啟動時執行的命令
# 將 'your_main_app_file.py' 替換成你的主要執行檔名稱
CMD ["python", "your_main_app_file.py"]
| 1 | # Dockerfile |
| 2 | |
| 3 | # 使用 Python 官方的 slim-bookworm 映像作為基礎 (Python 3.12, Debian 12) |
| 4 | # slim 版本比較輕量,適合部署 |
| 5 | FROM python:3.12-slim-bookworm |
| 6 | |
| 7 | # 安裝系統依賴套件。這些是常見用於網頁自動化 (Playwright) 和影像處理 (OpenCV) 的依賴。 |
| 8 | # 請根據你的專案需求,移除不需要的套件,以減少映像檔大小。 |
| 9 | # apt-get update 用來更新套件清單,apt-get install -y --no-install-recommends 安裝指定的套件 |
| 10 | # --no-install-recommends 會避免安裝非必要的建議套件 |
| 11 | RUN apt-get update && apt-get install -y --no-install-recommends \ |
| 12 | build-essential \ |
| 13 | libsecret-1-0 \ |
| 14 | libgtk-3-0 \ |
| 15 | libgdk-pixbuf2.0-0 \ |
| 16 | libxss1 \ |
| 17 | libasound2 \ |
| 18 | libdrm-dev \ |
| 19 | libgbm-dev \ |
| 20 | libnss3-dev \ |
| 21 | libatk-bridge2.0-0 \ |
| 22 | libcups2 \ |
| 23 | libxrandr2 \ |
| 24 | libxcomposite1 \ |
| 25 | libxcursor1 \ |
| 26 | libxdamage1 \ |
| 27 | libxext6 \ |
| 28 | libxfixes3 \ |
| 29 | libxi6 \ |
| 30 | libxrender1 \ |
| 31 | libxtst6 \ |
| 32 | curl \ |
| 33 | gnupg \ |
| 34 | ca-certificates \ |
| 35 | libgl1 \ |
| 36 | tesseract-ocr \ |
| 37 | tesseract-ocr-eng \ |
| 38 | && rm -rf /var/lib/apt/lists/* # 清理 apt 快取,讓映像檔更小 |
| 39 | |
| 40 | # 設定應用程式在容器內的工作目錄 |
| 41 | WORKDIR /app |
| 42 | |
| 43 | # 複製你的 Python 依賴清單到工作目錄 |
| 44 | COPY requirements.txt ./ |
| 45 | |
| 46 | # 安裝 Python 依賴套件 |
| 47 | # --no-cache-dir 會避免快取 pip 下載的套件,再次減少映像檔大小 |
| 48 | RUN pip install --no-cache-dir -r requirements.txt |
| 49 | |
| 50 | # 如果你的應用程式需要用到 Playwright,這裡會安裝其瀏覽器驅動。 |
| 51 | # 如果不需要,你可以把這行註解掉或移除。 |
| 52 | # 這會把瀏覽器安裝到 /root/.cache/ms-playwright |
| 53 | RUN python -m playwright install chromium firefox webkit |
| 54 | |
| 55 | # 如果使用 Playwright,設定環境變數,讓 Playwright 知道瀏覽器在哪裡 |
| 56 | # 如果不需要 Playwright,這行也可以移除。 |
| 57 | ENV PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright |
| 58 | |
| 59 | # 複製你的應用程式碼到 Image 中 |
| 60 | # 通常會把整個專案複製進去 |
| 61 | # 請記得更新這裡的來源和目的地,例如 `COPY . .` 如果你的 Dockerfile 在專案根目錄 |
| 62 | # COPY . . |
| 63 | |
| 64 | # 定義容器啟動時執行的命令 |
| 65 | # 將 'your_main_app_file.py' 替換成你的主要執行檔名稱 |
| 66 | CMD ["python", "your_main_app_file.py"] |
| 67 |