我正在使用 setuptools scm 动态提供 Python 包的版本号,并在 pyproject.toml 中包含这些行:
...
dynamic = ["dependencies", "version", "readme"]
[tool.setuptools]
packages = ["my_package"]
[tool.setuptools_scm]
...
当我尝试在 Docker 中安装软件包时,该过程失败,因为我已将 .git 目录包含在 .dockerignore 中。
这是我的 Dockerfile:
FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends -y \
build-essential curl wget git sox ffmpeg libsndfile1 zip unzip mandoc groff
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
unzip awscliv2.zip && \
./aws/install && \
rm awscliv2.zip
WORKDIR /workspace
COPY requirements.txt .
RUN pip install --root-user-action=ignore --no-cache-dir --no-deps -r requirements.txt
COPY . .
RUN ./scripts/install_custom_requirements.sh
ARG VERSION
ENV VERSION=$VERSION
RUN SETUPTOOLS_SCM_PRETEND_VERSION=$(python -m setuptools_scm) pip install --root-user-action=ignore --no-cache-dir --no-deps .
为了清楚起见,这是我在 .dockerignore 中的行:
**/.git
当我注释掉该行时,我的 Docker 构建工作正常,但当我在 Dockerfile 中执行 COPY . .
时,我不想复制
所有内容。如何将
**/.git
保留在 .dockerignore 中,同时让 setuptools scm 动态提供版本?文档(请参阅“使用 Docker / Podman”部分)为了避免 BuildKit 和完全安装 .git 文件夹,还可以将所需的版本作为构建参数传递。请注意,SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${NORMALIZED_DIST_NAME} 优于 SETUPTOOLS_SCM_PRETEND_VERSION。
但是当我尝试传递这个环境变量时,构建失败了。这是我尝试过的:
#!/usr/bin/env bash
set -e
source vars.env
VERSION=$(git describe --tags --dirty --always)
git submodule update --init --recursive --progress
docker build \
--progress=plain \
--build-arg "VERSION=${VERSION}" \
-t "${DOCKER_IMAGE_NAME}:${VERSION}" \
.
# In the Dockerfile, you are using
ARG VERSION
ENV VERSION=$VERSION
# When, from the docs you cited, it should have been
ARG VERSION
ENV SETUPTOOLS_SCM_PRETEND_VERSION=$VERSION
# or, even better
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_MY_PACKAGE=$VERSION
# Then you can change the RUN directive to
RUN pip install --root-user-action=ignore --no-cache-dir --no-deps .
您应该能够保留其余部分不变。