Search code examples
dockerpipalpine-linux

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


I'm trying to install crypotography using the below dockerfile for alpine3.7 python base. However, I'm getting error like this:

#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

As per cryptography document I'm installing all necessary packages but still it's throwing me the same error. 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

Can you anyone point me what is i'm doing wrong?


Solution

  • -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
    

    So, for you, in apk add --no-cache --virtual gcc, the gcc was wrongly treat as virtual package name, which means you in fact did not install gcc.

    To fix this, try to add a virtual package name before all packages as next:

    / # 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)
    

    Or directly as document said, remove --virtual:

    apk add gcc