Docker 镜像上的 pip 找不到 Rust - 即使安装了 Rust

lte*_*e__ 2 pip rust docker docker-build huggingface-tokenizers

我正在尝试安装一些 Python 包,即tokenizers来自 Huggingface 的包transformers,它显然需要 Rust。所以我在我的 Docker 版本上安装 Rust:

\n
FROM nikolaik/python-nodejs\nUSER pn\nWORKDIR /home/pn/app\n\nCOPY . /home/pn/app/\nRUN ls -la /home/pn/app/*\nRUN curl https://sh.rustup.rs -sSf | sh -s -- -y\n\nENV PATH ~/.cargo/bin:$PATH\n\n# Install Python dependencies.\nRUN pip install --upgrade pip\nRUN pip install -r requirements.txt\nRUN python load_model.py\n\n# to be equal to the cores available.\nCMD exec gunicorn --bind :$PORT --workers 4 --threads 4 app:app\n
Run Code Online (Sandbox Code Playgroud)\n

但是,安装分词器时似乎pip找不到:Rust

\n
Building wheels for collected packages: tokenizers\n#11 103.2   Building wheel for tokenizers (pyproject.toml): started\n#11 104.6   Building wheel for tokenizers (pyproject.toml): finished with status 'error'\n#11 104.6   error: subprocess-exited-with-error\n#11 104.6\n#11 104.6   \xc3\x97 Building wheel for tokenizers (pyproject.toml) did not run successfully.\n#11 104.6   \xe2\x94\x82 exit code: 1\n#11 104.6   \xe2\x95\xb0\xe2\x94\x80> [51 lines of output]\n...\n  error: can't find Rust compiler\n#11 104.6\n#11 104.6       If you are using an outdated pip version, it is possible a prebuilt wheel is available for this package but pip is not able to install from it. Installing from the wheel would avoid the need for a Rust compiler.\n
Run Code Online (Sandbox Code Playgroud)\n

为什么会出现这种情况?我如何确保 Rust 可用?

\n

Han*_*ian 5

安装程序似乎在您的.bashrc文件中添加了一行来设置路径。.bashrc 仅当您处于交互式 shell 中时才会运行,而当您运行构建脚本时则不会。因此,您的路径未设置为包含 Rust 编译器的目录。

据我所知,编译器安装在$HOME/.cargo/bin. 在你的情况下,那就是/home/pn/.cargo/bin。要将其添加到路径中,您可以将 ENV 行添加到 Dockerfile 中,如下所示

FROM nikolaik/python-nodejs
USER pn
WORKDIR /home/pn/app

COPY . /home/pn/app/
RUN ls -la /home/pn/app/*
RUN curl --proto '=https' --tlsv1.2 -sSf -y https://sh.rustup.rs | sh
ENV PATH /home/pn/.cargo/bin:$PATH

# Install Python dependencies.
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN python load_model.py

# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 4 --threads 4 app:app
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请尝试使用以下命令在映像中运行 shell

docker run --rm -it <image name> bash
Run Code Online (Sandbox Code Playgroud)

然后你可以四处寻找并尝试找到 rust 编译器的安装目录。

  • 我认为它会是“/root/.cargo/bin”。这就是我尝试安装它时的情况。 (2认同)