46 lines
1.7 KiB
Docker
46 lines
1.7 KiB
Docker
# syntax=docker/dockerfile:1
|
||
# 国内拉基础镜像慢时:docker compose build --build-arg PY_BASE=docker.m.daocloud.io/library/python:3.11-slim
|
||
ARG PY_BASE=python:3.11-slim
|
||
FROM ${PY_BASE}
|
||
|
||
WORKDIR /app
|
||
|
||
# 默认清华 PyPI;海外可:docker compose build --build-arg PIP_INDEX_URL=https://pypi.org/simple
|
||
ARG PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
|
||
ENV PIP_INDEX_URL=${PIP_INDEX_URL} \
|
||
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
||
PIP_DEFAULT_TIMEOUT=120
|
||
|
||
COPY requirements.txt ./
|
||
# BuildKit 缓存加速重复构建;需 Docker 20.10+(compose 默认开 BuildKit)
|
||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||
pip install -r requirements.txt
|
||
|
||
COPY . .
|
||
|
||
# 可选:构建时自动拉取远端仓库最新代码覆盖工作目录(默认关闭)。
|
||
# 放在 COPY 之后,避免影响依赖层缓存;仅在你显式开启时才会触发网络更新。
|
||
# 用法示例:
|
||
# docker compose build \
|
||
# --build-arg GIT_AUTO_UPDATE=1 \
|
||
# --build-arg GIT_REMOTE_URL=https://github.com/you/repo.git \
|
||
# --build-arg GIT_REF=main
|
||
ARG GIT_AUTO_UPDATE=0
|
||
ARG GIT_REMOTE_URL=
|
||
ARG GIT_REF=main
|
||
RUN if [ "$GIT_AUTO_UPDATE" = "1" ] && [ -n "$GIT_REMOTE_URL" ]; then \
|
||
set -eux; \
|
||
apt-get update; \
|
||
apt-get install -y --no-install-recommends git ca-certificates; \
|
||
rm -rf /var/lib/apt/lists/*; \
|
||
tmpdir="$(mktemp -d)"; \
|
||
git clone --depth=1 --branch "$GIT_REF" "$GIT_REMOTE_URL" "$tmpdir/repo"; \
|
||
cp -a "$tmpdir/repo/." /app/; \
|
||
rm -rf "$tmpdir"; \
|
||
else \
|
||
echo "Skip remote code update (GIT_AUTO_UPDATE=0 or GIT_REMOTE_URL empty)"; \
|
||
fi
|
||
|
||
EXPOSE 8000
|
||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|