使用 docker 安装 ruamel.yaml.clib

Installing ruamel.yaml.clib with docker

我在 django rest 框架中有一个小项目,我想 docker 对其进行处理。在我的 requirements.txt 文件中有一个名为 ruamel.yaml.clib==0.2.6 的包。虽然下载所有其他要求成功,但在尝试下载此包时出现问题。

#11 208.5 Collecting ruamel.yaml.clib==0.2.6
#11 208.7   Downloading ruamel.yaml.clib-0.2.6.tar.gz (180 kB)
#11 217.8     ERROR: Command errored out with exit status 1:
#11 217.8      command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/setup.py'"'"'; __file__='"'"'/tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-n2gr5j35
#11 217.8          cwd: /tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/
#11 217.8     Complete output (3 lines):
#11 217.8     sys.argv ['/tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/setup.py', 'egg_info', '--egg-base', '/tmp/pip-pip-egg-info-n2gr5j35']
#11 217.8     test compiling /tmp/tmp_ruamel_erx3efla/test_ruamel_yaml.c -> test_ruamel_yaml compile error: /tmp/tmp_ruamel_erx3efla/test_ruamel_yaml.c
#11 217.8     Exception: command 'gcc' failed: No such file or directory
#11 217.8     ----------------------------------------
#11 217.8 WARNING: Discarding https://files.pythonhosted.org/packages/8b/25/08e5ad2431a028d0723ca5540b3af6a32f58f25e83c6dda4d0fcef7288a3/ruamel.yaml.clib-0.2.6.tar.gz#sha256=4ff604ce439abb20794f05613c374759ce10e3595d1867764dd1ae675b85acbd (from https://pypi.org/simple/ruamel-yaml-clib/) (requires-python:>=3.5). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#11 217.8 ERROR: Could not find a version that satisfies the requirement ruamel.yaml.clib==0.2.6 (from versions: 0.1.0, 0.1.2, 0.2.0, 0.2.2, 0.2.3, 0.2.4, 0.2.6)
#11 217.8 ERROR: No matching distribution found for ruamel.yaml.clib==0.2.6

但是,我在没有docker的情况下下载这个包是没有问题的。有什么建议吗?

这是 Dockerfile:

    FROM python:3-alpine

    # set work directory
    WORKDIR /usr/src/app

    # set environment variables
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1

    # install dependencies

    RUN pip install -U pip setuptools wheel ruamel.yaml ruamel.yaml.clib==0.2.6
    COPY ./requirements.txt .
    RUN pip install --default-timeout=100 -r requirements.txt

    # copy project
    COPY . .

这是合成文件

    version: '3.8'
  
    services: 
      web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/usr/src/app
        ports: 
          - 8000:8000
        env_file: 
          - ./.env.dev

编辑

正如@Anthon 在评论中提到的,问题与高山有关。我在 Dockerfile 中使用 python:3.9-slim-buster 代替,问题解决了!

我认为问题在于您的 Dockerfile 尝试安装 ruamel.yaml.clib 的方式。它应该使用 pip 安装(就像 ruamel.yaml 中的文档一样)。

我建议你把它从 requirements.txt 中取出并明确地做一个

 pip install -U pip setuptools wheel ruamel.yaml.clib==0.2.6

在您的 Dockerfile 中。这应该只是让你得到预编译的轮子,而不是试图从源代码编译 ruamel.yaml.clib,如果你没有安装 C 编译器,这将不起作用(这实际上是 docker 抱怨的)

我在多个 Docker 容器中成功地 ruamel.yaml.clib 运行(但我从未使用过 requirements.txt

我看到 OP 已经从 Alpine 转移到解决这个问题,但是无论出于什么原因 need/want/wish 继续使用 Alpine,您可以安装 gccmusl-devpython3-dev:

⋮
RUN apk add --no-cache gcc musl-dev python3-dev
⋮
RUN pip install ruamel.yaml.clib …

我认为你应该添加 pip install --upgrade pip 在安装文件之前 requirements.txt