Search code examples
python-3.xdockerdockerfilealpine-linux

Failed to install paramiko (Failed to build cffi) on Python-3.8-alpine docker container


I have the following Dockerfile I am trying to use to build an image :

FROM python:3.8-alpine

USER root

WORKDIR /dump_generator_api

COPY requirements.txt ./

RUN python3 -m pip install --upgrade pip
RUN pip3 install -r requirements.txt

RUN apk add postgresql 
ADD . /dump_generator_api
EXPOSE 5000
CMD ["python", "/dump_generator_api/app.py"]

Here are the requirements.txt:

Flask==2.0.1
paramiko==3.0.0

When I try to build the image I have the following error. I think this from paramiko installation.

#0 43.49 Failed to build cffi
#0 43.69 Installing collected packages: pycparser, MarkupSafe, itsdangerous, colorama, click, bcrypt, Werkzeug, Jinja2, cffi, PyNaCl, Flask, cryptography, paramiko, flask-healthz, pysftp
#0 44.54   Running setup.py install for cffi: started
#0 44.93   Running setup.py install for cffi: finished with status 'error'
#0 44.94   error: subprocess-exited-with-error
#0 44.94
#0 44.94   × Running setup.py install for cffi did not run successfully.
#0 44.94   │ exit code: 1
#0 44.94   ╰─> [48 lines of output]
#0 44.94       unable to execute 'gcc': No such file or directory
#0 44.94       unable to execute 'gcc': No such file or directory
#0 44.94
#0 44.94           No working compiler found, or bogus compiler options passed to
#0 44.94           the compiler from Python's standard "distutils" module.  See
#0 44.94           the error messages above.  Likely, the problem is not related
#0 44.94           to CFFI but generic to the setup.py of any Python package that
#0 44.94           tries to compile C code.  (Hints: on OS/X 10.8, for errors about
#0 44.94           -mno-fused-madd see http://stackoverflow.com/questions/22313407/
#0 44.94           Otherwise, see https://wiki.python.org/moin/CompLangPython or
#0 44.94           the IRC channel #python on irc.libera.chat.)
#0 44.94
#0 44.94           Trying to continue anyway.  If you are trying to install CFFI from
#0 44.94           a build done in a different context, you can ignore this warning.
#0 44.94
#0 44.94       running install
#0 44.94       running build
#0 44.94       running build_py
#0 44.94       creating build
#0 44.94       creating build/lib.linux-x86_64-3.8
#0 44.94       creating build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/verifier.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/lock.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/pkgconfig.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/error.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/api.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/model.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/cparser.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/recompiler.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/__init__.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/commontypes.py -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/_cffi_include.h -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/parse_c_type.h -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/_embedding.h -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-3.8/cffi
#0 44.94       running build_ext
#0 44.94       building '_cffi_backend' extension
#0 44.94       creating build/temp.linux-x86_64-3.8
#0 44.94       creating build/temp.linux-x86_64-3.8/c
#0 44.94       gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DFFI_BUILDING=1 -I/usr/include/ffi -I/usr/include/libffi -I/usr/local/include/python3.8 -c c/_cffi_backend.c -o build/temp.linux-x86_64-3.8/c/_cffi_backend.o
#0 44.94       unable to execute 'gcc': No such file or directory
#0 44.94       error: command 'gcc' failed with exit status 1
#0 44.94       [end of output]
#0 44.94
#0 44.94   note: This error originates from a subprocess, and is likely not a problem with pip.
#0 44.95 error: legacy-install-failure
#0 44.95
#0 44.95 × Encountered error while trying to install package.
#0 44.95 ╰─> cffi
#0 44.95
#0 44.95 note: This is an issue with the package mentioned above, not pip.
#0 44.95 hint: See above for output from the failure.

I guess I have to include something else in my Dockerfile but I don't find. How to modify my Dockerfile to succeed in building my image ? I think the problem is from gcc.


Solution

  • You could either:

    1. Install gcc using apk (taken from this answer):
    # Dockerfile
    # ...
    
    RUN apk add build-base
    
    # Use pip as desired
    
    1. Force pip to install the precompiled cffi package:
    # Dockerfile
    # ...
    
    RUN pip3 install -r requirements.txt --only-binary=:all:
    

    (You can also try using --prefer-binary instead.)