`pip install cyrptography ` 无法在 python3.7-alpine docker 上安装

`pip install cyrptography ` not able to install on python3.7-alpine docker

我正在尝试使用下面的 dockerfile 为 alpine3.7 python base 安装 crypotography。 但是,我收到这样的错误:

#7 17.27 Failed to build cryptography cffi
#7 17.37 Installing collected packages: pycparser, six, idna, cffi, asn1crypto, pymysql, cryptography
#7 17.68     Running setup.py install for cffi: started
#7 18.04     Running setup.py install for cffi: finished with status 'error'
#7 18.04     ERROR: Command errored out with exit status 1:
#7 18.04      command: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-twxf8fj_/cffi_1f087e9b9e8c4af8b4e6bf3deb3a5bf9/setup.py'"'"'; __file__='"'"'/tmp/pip-install-twxf8fj_/cffi_1f087e9b9e8c4af8b4e6bf3deb3a5bf9/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'"'"'))' install --record /tmp/pip-record-j8mkceyt/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.7m/cffi
#7 18.04          cwd: /tmp/pip-install-twxf8fj_/cffi_1f087e9b9e8c4af8b4e6bf3deb3a5bf9/
#7 18.04     Complete output (48 lines):
#7 18.04     unable to execute 'gcc': No such file or directory
#7 18.04     unable to execute 'gcc': No such file or directory

根据 cryptography 文档,我正在安装所有必要的软件包,但它仍然抛出同样的错误。 Dockerfile:

FROM python:3.7-alpine
WORKDIR  /project
RUN apk add --no-cache --virtual gcc musl-dev python3-dev libffi-dev openssl-dev cargo mariadb-dev
RUN pip install pymysql cryptography

谁能指出我做错了什么?

-t, --virtual NAME Instead of adding all the packages to 'world', create a new virtual package with the listed dependencies and add that to 'world'; the actions of the command are easily reverted by deleting the virtual package

What that means is when you install packages, those packages are not added to global packages. And this change can be easily reverted. So if I need gcc to compile a program, but once the program is compiled I no more need gcc.

I can install gcc, and other required packages in a virtual package and all of its dependencies and everything can be removed this virtual package name. Below is an example usage:

apk add --virtual .dep gcc
apk del .dep

所以,对于你来说,在apk add --no-cache --virtual gcc中,gcc被错误地当作虚拟包名,这意味着你实际上并没有安装gcc。

要解决此问题,请尝试在所有包之前添加一个虚拟包名称,如下所示:

/ # apk add --no-cache --virtual .dep gcc
fetch https://mirrors.ustc.edu.cn/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.ustc.edu.cn/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/12) Installing libgcc (10.3.1_git20210424-r2)
(2/12) Installing libstdc++ (10.3.1_git20210424-r2)
(3/12) Installing binutils (2.35.2-r2)
(4/12) Installing libgomp (10.3.1_git20210424-r2)
(5/12) Installing libatomic (10.3.1_git20210424-r2)
(6/12) Installing libgphobos (10.3.1_git20210424-r2)
(7/12) Installing gmp (6.2.1-r0)
(8/12) Installing isl22 (0.22-r0)
(9/12) Installing mpfr4 (4.1.0-r0)
(10/12) Installing mpc1 (1.2.1-r0)
(11/12) Installing gcc (10.3.1_git20210424-r2)
(12/12) Installing .dep (20210629.140945)
Executing busybox-1.33.1-r2.trigger
OK: 122 MiB in 47 packages
/ # gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-alpine-linux-musl/10.3.1/lto-wrapper
Target: x86_64-alpine-linux-musl
Configured with: /home/buildozer/aports/main/gcc/src/gcc-10.3.1_git20210424/configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --build=x86_64-alpine-linux-musl --host=x86_64-alpine-linux-musl --target=x86_64-alpine-linux-musl --with-pkgversion='Alpine 10.3.1_git20210424' --enable-checking=release --disable-fixed-point --disable-libstdcxx-pch --disable-multilib --disable-nls --disable-werror --disable-symvers --enable-__cxa_atexit --enable-default-pie --enable-default-ssp --enable-cloog-backend --enable-languages=c,c++,d,objc,go,fortran,ada --disable-libssp --disable-libmpx --disable-libmudflap --disable-libsanitizer --enable-shared --enable-threads --enable-tls --with-system-zlib --with-linker-hash-style=gnu
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.3.1 20210424 (Alpine 10.3.1_git20210424)

或者直接按照文档所说,删除 --virtual:

apk add gcc