Python docker 由于 gcc 错误,图像构建失败

Python docker image build fails due to gcc error

我正在学习Nornir并想将我的应用程序容器化,这是Dockerfile内容:

FROM alpine

RUN apk update && apk upgrade 
RUN apk add gcc && apk add py3-pip && apk add libevent-dev
RUN apk add python3-dev && pip3 install nornir 

当我使用命令构建容器时:

docker build --network=host .

我收到以下错误:

somaiia@main-srv:~/core/nornir$ docker build --no-cache --network=host .
Sending build context to Docker daemon  10.24kB
Step 1/4 : FROM alpine
...
Step 4/4 : RUN apk add python3-dev && pip3 install nornir
 ---> Running in 025aee6693e4
(1/1) Installing python3-dev (3.9.7-r4)
Executing busybox-1.34.1-r5.trigger
OK: 229 MiB in 66 packages
Collecting nornir
  Downloading nornir-3.3.0-py3-none-any.whl (30 kB)
Collecting typing_extensions<5.0,>=4.1
  Downloading typing_extensions-4.2.0-py3-none-any.whl (24 kB)
Collecting importlib-metadata<5,>=4
  Downloading importlib_metadata-4.11.4-py3-none-any.whl (18 kB)
Collecting ruamel.yaml>=0.17
  Downloading ruamel.yaml-0.17.21-py3-none-any.whl (109 kB)
Collecting mypy_extensions<0.5.0,>=0.4.1
  Downloading mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB)
Collecting zipp>=0.5
  Downloading zipp-3.8.0-py3-none-any.whl (5.4 kB)
Collecting ruamel.yaml.clib>=0.2.6
  Downloading ruamel.yaml.clib-0.2.6.tar.gz (180 kB)
    **ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vs5bwupe/ruamel-yaml-clib_e6dc2477a7f349c6866be26f31b40c2e/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vs5bwupe/ruamel-yaml-clib_e6dc2477a7f349c6866be26f31b40c2e/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-jh_alwhp
         cwd: /tmp/pip-install-vs5bwupe/ruamel-yaml-clib_e6dc2477a7f349c6866be26f31b40c2e/
    Complete output (7 lines):
    /usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find Scrt1.o: No such file or directory
    /usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
    /usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lssp_nonshared
    collect2: error: ld returned 1 exit status
    sys.argv ['/tmp/pip-install-vs5bwupe/ruamel-yaml-clib_e6dc2477a7f349c6866be26f31b40c2e/setup.py', 'egg_info', '--egg-base', '/tmp/pip-pip-egg-info-jh_alwhp']
    test compiling /tmp/tmp_ruamel_zq9s1x2j/test_ruamel_yaml.c -> test_ruamel_yaml link error /tmp/tmp_ruamel_zq9s1x2j/test_ruamel_yaml.c
    Exception: command '/usr/bin/gcc' failed with exit code 1
    ----------------------------------------
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.
Collecting ruamel.yaml>=0.17
  Downloading ruamel.yaml-0.17.20-py3-none-any.whl (109 kB)
  Downloading ruamel.yaml-0.17.19-py3-none-any.whl (109 kB)
  Downloading ruamel.yaml-0.17.18-py3-none-any.whl (109 kB)
  Downloading ruamel.yaml-0.17.17-py3-none-any.whl (109 kB)
Collecting ruamel.yaml.clib>=0.1.2
  Downloading ruamel.yaml.clib-0.2.2.tar.gz (179 kB)
Using legacy 'setup.py install' for ruamel.yaml.clib, since package 'wheel' is not installed.
Installing collected packages: zipp, ruamel.yaml.clib, typing-extensions, ruamel.yaml, mypy-extensions, importlib-metadata, nornir
    Running setup.py install for ruamel.yaml.clib: started
    Running setup.py install for ruamel.yaml.clib: finished with status 'done'
Successfully installed importlib-metadata-4.11.4 mypy-extensions-0.4.3 nornir-3.3.0 ruamel.yaml-0.17.17 ruamel.yaml.clib-0.2.2 typing-extensions-4.2.0 zipp-3.8.0**

我无法构建 Nornir,因此无法继续下一步,我该怎么办?

在日志中,错误源于此

Collecting ruamel.yaml.clib>=0.2.6
  Downloading ruamel.yaml.clib-0.2.6.tar.gz (180 kB)
    **ERROR: Command errored out with exit status 1:

但是根据日志我们这个

Collecting ruamel.yaml.clib>=0.1.2
   Downloading ruamel.yaml.clib-0.2.2.tar.gz (179 kB)

基本上第一个依赖要求要求 ruamel.yaml.clib 版本大于或等于 0.2.6,但它失败了。

在接下来的尝试中,它尝试了一个大于或等于 0.1.2 的版本。

我们在最终日志中看到

Successfully installed ... nornir-3.3.0 ruamel.yaml.clib-0.2.2 ...**

意味着它成功安装了旧版本的 ruamel.yaml.clib 库,最后也安装了 nornir