23

I am trying to set up a Python docker container, when I run the command "docker-compose up" to build the Dockerfile it throws me this error message below.

The docker file was successfully built two weeks ago, I thought to rebuild the docker container due to a newly added library in the requirements.txt.

Trying to build the image produces the following error:

Collecting cryptography
  Downloading cryptography-2.8.tar.gz (504 kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'error'
  ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python /usr/local/lib/python3.7/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-6ilf25i_/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.8,!=1.11.3; platform_python_implementation != '"'"'PyPy'"'"''
       cwd: None
  Complete output (140 lines):
  Collecting setuptools>=40.6.0
    Downloading setuptools-46.0.0-py3-none-any.whl (582 kB)
  Collecting wheel
    Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
  Collecting cffi!=1.11.3,>=1.8
    Downloading cffi-1.14.0.tar.gz (463 kB)
  Collecting pycparser
    Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB)
  Building wheels for collected packages: cffi
    Building wheel for cffi (setup.py): started
    Building wheel for cffi (setup.py): finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-mzaa6f6w
         cwd: /tmp/pip-install-vm8xra6q/cffi/
    Complete output (56 lines):
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    Package 'libffi', required by 'virtual:world', not found
    running bdist_wheel
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.7
    creating build/lib.linux-x86_64-3.7/cffi
    copying cffi/pkgconfig.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/error.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/model.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/lock.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/recompiler.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/verifier.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/api.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/cparser.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/commontypes.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/__init__.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/_cffi_include.h -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/parse_c_type.h -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/_embedding.h -> build/lib.linux-x86_64-3.7/cffi
    copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-3.7/cffi
    running build_ext
    building '_cffi_backend' extension
    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/c
    gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/usr/include/ffi -I/usr/include/libffi -I/usr/local/include/python3.7m -c c/_cffi_backend.c -o build/temp.linux-x86_64-3.7/c/_cffi_backend.o
    c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
       15 | #include <ffi.h>
          |          ^~~~~~~
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
    ERROR: Failed building wheel for cffi
    Running setup.py clean for cffi
  Failed to build cffi
  Installing collected packages: setuptools, wheel, pycparser, cffi
      Running setup.py install for cffi: started
      Running setup.py install for cffi: finished with status 'error'
      ERROR: Command errored out with exit status 1:
       command: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-akcet9bz/install-record.txt --single-version-externally-managed --prefix /tmp/pip-build-env-6ilf25i_/overlay --compile --install-headers /tmp/pip-build-env-6ilf25i_/overlay/include/python3.7m/cffi
           cwd: /tmp/pip-install-vm8xra6q/cffi/
      Complete output (56 lines):
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      Package libffi was not found in the pkg-config search path.
      Perhaps you should add the directory containing `libffi.pc'
      to the PKG_CONFIG_PATH environment variable
      Package 'libffi', required by 'virtual:world', not found
      running install
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-3.7
      creating build/lib.linux-x86_64-3.7/cffi
      copying cffi/pkgconfig.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/error.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/model.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/lock.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/recompiler.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/verifier.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/api.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/cparser.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/commontypes.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/__init__.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/_cffi_include.h -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/parse_c_type.h -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/_embedding.h -> build/lib.linux-x86_64-3.7/cffi
      copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-3.7/cffi
      running build_ext
      building '_cffi_backend' extension
      creating build/temp.linux-x86_64-3.7
      creating build/temp.linux-x86_64-3.7/c
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/usr/include/ffi -I/usr/include/libffi -I/usr/local/include/python3.7m -c c/_cffi_backend.c -o build/temp.linux-x86_64-3.7/c/_cffi_backend.o
      c/_cffi_backend.c:15:10: fatal error: ffi.h: No such file or directory
         15 | #include <ffi.h>
            |          ^~~~~~~
      compilation terminated.
      error: command 'gcc' failed with exit status 1
      ----------------------------------------
  ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-vm8xra6q/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-akcet9bz/install-record.txt --single-version-externally-managed --prefix /tmp/pip-build-env-6ilf25i_/overlay --compile --install-headers /tmp/pip-build-env-6ilf25i_/overlay/include/python3.7m/cffi Check the logs for full command output.
  ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python /usr/local/lib/python3.7/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-6ilf25i_/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.8,!=1.11.3; platform_python_implementation != '"'"'PyPy'"'"'' Check the logs for full command output.

My requirements.txt file looks like:

Django>=2.1.3,<2.2.0
djangorestframework>3.9.0,<3.10.0
psycopg2>=2.7.5,<2.8.0
flake8>=3.6.0,<3.7.0
attrdict==2.0.1
certifi ==2019.3.9
chardet ==3.0.4
cytoolz == 0.9.0.1
eth-abi==1.3.0
eth-account == 0.3.0
eth-hash==0.2.0
eth-keyfile ==0.5.1
eth-keys ==0.2.1
eth-rlp==0.1.2
eth-typing==2.1.0
eth-utils ==1.4.1
hexbytes==0.1.0
idna==2.8
lru-dict==1.1.6
parsimonious==0.8.1
pycryptodome==3.8.0
requests==2.21.0
rlp==1.1.0
six==1.12.0
toolz==0.9.0
urllib3==1.24.1
web3==4.9.1
websockets==6.0
uwsgi
psycopg2-binary==2.8.3
Pillow
django-storages[azure]
django-countries
wsgi-basic-auth
celery
redis
django-celery-results
django-rest-knox

I'm using the following Dockerfile:

FROM python:3.7-alpine
MAINTAINER DB

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client jpeg-dev
RUN apk add --update --no-cache --virtual .tmp-build-deps \
      gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol/
RUN chmod -R 755 /vol/web
USER user
David Maze
  • 130,717
  • 29
  • 175
  • 215
David
  • 607
  • 1
  • 6
  • 19
  • what is the new library? – Z4-tier Mar 09 '20 at 06:49
  • @Z4-tier - Please see my requirement file above. The newly added libraries are django-rest-knox and web3.py. – David Mar 09 '20 at 07:51
  • May I know what could be the issue here? – David Mar 09 '20 at 07:53
  • @Z4-tier - I found out the root cause of failing the requirements.txt installation. The "bad apple" is the django-rest-knox library. Why is that? – David Mar 09 '20 at 12:21
  • That's a good question. I was actually just looking at that library too, but I don't have a good answer at this point. One thing you might want to try is to switch from alpine and use `python:3.7` instead. A lot of the time, the space you save by using alpine isn't worth the amount of effort that it takes to get it working in the first place. – Z4-tier Mar 09 '20 at 12:27

6 Answers6

27

For me installation of these packages within docker container helped:

RUN apk update && apk add python3-dev \
                        gcc \
                        libc-dev
Ihor Shylo
  • 572
  • 5
  • 12
  • 8
    I also needed `libffi-dev`, see https://github.com/jfloff/alpine-python/issues/44 – Justin Harris Mar 30 '21 at 02:31
  • 1
    I also needed `libffi-dev`, but not `libc-dev`. Perhaps it was a bit of another problem, I cannot say it for sure. – questionto42 Aug 30 '21 at 11:25
  • had this issue while running workflows on circleci `image: cimg/base:2022.06-18.04` for part of the config and all the above solved the problem. just fyi – nate Jul 04 '22 at 18:06
18

One of these three approaches may help you:

1 - Use python:<version> which is a Debian image or python:<version>-slim image instead of python:<version>-alpine:

This approach uses different base images for python which have some preinstalled utilities.

Alpine just have a few necessary utilities, so you may want to install more utilities by yourself.

2 - Installing required dependencies:

RUN apk update && apk add python3-dev \
                          gcc \
                          libc-dev \
                          libffi-dev

3 - downgrading python version:

I'm not sure about this solution and I don't recommend it, I just remember once it worked.


As an additional note, I recommend not to use alpine images unless you have a lot of storage concerns.

Arsham Arya
  • 1,461
  • 3
  • 17
  • 25
  • 1
    Using a different Linux flavor is not always an option. For example, anybody working on ARM32v5 hardware, such as the RPi Zero, will not be able to use the other Python images. Only Alpine is available for this hardware. – Vince Aug 30 '22 at 16:51
  • Using `python:3.9` as oppsed to `python:3.9-alpine` worked for me. – Sau001 Sep 21 '22 at 07:21
6

I was able to fix the same error by using the non-alpine image. Therefore, the top of my dockerfile is now:

    FROM python:3.8.0
Nick Weimer
  • 497
  • 5
  • 7
  • This is due to the Alpine image being much slimmer than other linux distributions. If you are using Alpine for a reason, then it is better to add the packages in your Dockerfile rather than swap to a full-fat image. – James Dec 03 '21 at 13:44
1

My solution to the mentioned error:

FROM python:3.8-alpine

RUN apk add --no-cache --virtual .build-deps gcc libffi-dev musl-dev && pip install cython \

   && pip install cffi && apk del .build-deps gcc libffi-dev musl-dev && pip install -r requirements.txt
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Maina
  • 11
  • 2
1

I just found out a solution. You have to add these commands in the docker file

RUN apk add --update alpine-sdk

0

I resolved the issue with the answer provided below's link.

Cannot "pip install cryptography" in Docker Alpine Linux 3.3 with OpenSSL 1.0.2g and Python 2.7

David
  • 607
  • 1
  • 6
  • 19
  • 1
    This link is helpful. The downvote that was alrady there was probably for the formal reason that just posting a link is not in the lines of "Stack Overflow rules". It should have been in a comment under the question, I guess. But then again, you often skip a comment or two. That is why this almost got my upvote. – questionto42 Aug 30 '21 at 13:54