Browse Source

Merge pull request 'refactor: 🛠️ 优化 Dockerfile,重组构建阶段,移除冗余安装步骤' (#33) from cjl-dev into master

Reviewed-on: http://120.26.116.243:3000/root/model-lab/pulls/33
master
chenjiale 1 month ago
parent
commit
922b6aaab7
  1. 30
      Dockerfile

30
Dockerfile

@ -1,26 +1,36 @@
FROM docker.io/python:3.10.16-slim
# ======================
# Build Stage(含 gcc)
# ======================
FROM python:3.10.16-slim AS builder
RUN sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources \
&& sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
gcc g++ unixodbc unixodbc-dev default-libmysqlclient-dev \
|| (sleep 3 && apt-get update && apt-get install -y --no-install-recommends \
gcc g++ unixodbc unixodbc-dev default-libmysqlclient-dev) \
gcc g++ unixodbc-dev default-libmysqlclient-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /build
COPY requirements.txt .
# 安装依赖到 /python_install
RUN pip install --no-cache-dir --prefix=/python_install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# ======================
# Runtime Stage(无 gcc)
# ======================
FROM python:3.10.16-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# 只复制依赖(已编译好)
COPY --from=builder /python_install /usr/local
# 🔥 只复制除 config.py 外的全部代码
# 复制代码
COPY . .
# 🔥 再显式复制 config.py(此时一定是文件)
COPY config.py /app/config.py
RUN mkdir -p /opt/alert/model-lab \
&& cp /app/config.py /opt/alert/model-lab/config.py

Loading…
Cancel
Save