Docker : 执行 "RUN pip install --no-cache /wheels/*" 命令时出错

Docker : error while performing "RUN pip install --no-cache /wheels/*" command

我正在尝试通过

上的教程将 Django 与 Postgres、Gunicorn 和 Nginx 进行 Dockerizing

https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/

我在 docker 执行第 7 步时遇到错误,即

Step 7/23 : RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt

我能够更新 Dockerfile.prod 来克服这个错误,但是我在第 21 步中遇到另一个错误

Step 21/26 : RUN pip install --no-cache /wheels/*

这是我更新的 Dockerfile.prod

###########
# BUILDER #
###########

# pull official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev openssl-dev

# lint
RUN pip install --upgrade pip
# RUN pip install flake8
COPY . .
# RUN flake8 --ignore=E501,F401 .

# install dependencies
COPY ./requirements.txt .
RUN apk --update add \
    build-base \
    jpeg-dev \
    zlib-dev
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.8.3-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

以下是我得到的错误

Step 21/26 : RUN pip install --no-cache /wheels/*
 ---> Running in 2edb399fffff
Processing /wheels/DateTimeRange-1.0.0-py3-none-any.whl
Processing /wheels/Django-3.1-py3-none-any.whl
Processing /wheels/Jinja2-2.11.2-py2.py3-none-any.whl
Processing /wheels/Markdown-3.2.2-py3-none-any.whl
Processing /wheels/MarkupSafe-1.1.1-cp38-cp38-linux_x86_64.whl
Processing /wheels/Pillow-7.2.0-cp38-cp38-linux_x86_64.whl
Processing /wheels/asgiref-3.2.10-py3-none-any.whl
Processing /wheels/certifi-2020.6.20-py2.py3-none-any.whl
Processing /wheels/chardet-3.0.4-py2.py3-none-any.whl
Processing /wheels/coreapi-2.3.3-py2.py3-none-any.whl
Processing /wheels/coreschema-0.0.4-py3-none-any.whl
Processing /wheels/dj_database_url-0.5.0-py2.py3-none-any.whl
Processing /wheels/django_cors_headers-3.6.0-py3-none-any.whl
Processing /wheels/django_filter-2.3.0-py3-none-any.whl
Processing /wheels/django_import_export-2.3.0-py3-none-any.whl
Processing /wheels/django_microsoft_auth-2.4.0-py2.py3-none-any.whl
Processing /wheels/django_rest_swagger-2.1.0-py2.py3-none-any.whl
Processing /wheels/djangorestframework-3.11.1-py3-none-any.whl
Processing /wheels/djangorestframework_gis-0.14-py2.py3-none-any.whl
Processing /wheels/djangorestframework_jwt-1.11.0-py2.py3-none-any.whl
Processing /wheels/djangorestframework_simplejwt-4.3.0-py3-none-any.whl
Processing /wheels/drf_msal_jwt-0.4.0-py2.py3-none-any.whl
Processing /wheels/gunicorn-20.0.4-py2.py3-none-any.whl
Processing /wheels/idna-2.10-py2.py3-none-any.whl
Processing /wheels/importlib_metadata-1.7.0-py2.py3-none-any.whl
Processing /wheels/itypes-1.2.0-py2.py3-none-any.whl
Processing /wheels/openapi_codec-1.3.2-py3-none-any.whl
Processing /wheels/pandas-1.1.2-cp38-cp38-linux_x86_64.whl
Processing /wheels/psycopg2_binary-2.8.5-cp38-cp38-linux_x86_64.whl
Processing /wheels/pytz-2020.1-py2.py3-none-any.whl
Processing /wheels/requests-2.24.0-py2.py3-none-any.whl
Processing /wheels/simplejson-3.17.2-cp38-cp38-linux_x86_64.whl
Processing /wheels/smartsheet_python_sdk-2.105.1-py2.py3-none-any.whl
Processing /wheels/sqlparse-0.3.1-py2.py3-none-any.whl
Processing /wheels/uritemplate-3.0.1-py2.py3-none-any.whl
Processing /wheels/urllib3-1.25.10-py2.py3-none-any.whl
Processing /wheels/zipp-3.1.0-py3-none-any.whl
Collecting typepy[datetime]<2,>=1.0.0
  Downloading typepy-1.1.2-py3-none-any.whl (30 kB)
Collecting python-dateutil<3,>=2.4.2
  Downloading python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
Requirement already satisfied: setuptools>=38.3.0 in /usr/local/lib/python3.8/site-packages (from DateTimeRange==1.0.0) (47.1.1)
Collecting diff-match-patch
  Downloading diff_match_patch-20200713-py3-none-any.whl (61 kB)
Collecting tablib[html,ods,xls,xlsx,yaml]>=0.14.0
  Downloading tablib-3.0.0-py3-none-any.whl (47 kB)
Collecting cryptography
  Downloading cryptography-3.3.1.tar.gz (539 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.8/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-mytrl8h6/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.12; platform_python_implementation != '"'"'PyPy'"'"''
       cwd: None
  Complete output (127 lines):
  WARNING: The directory '/home/app/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  Collecting setuptools>=40.6.0
    Downloading setuptools-52.0.0-py3-none-any.whl (784 kB)
  Collecting wheel
    Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
  Collecting cffi>=1.12
    Downloading cffi-1.14.4.tar.gz (471 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-6fztua11/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-6fztua11/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-qyffv65y
         cwd: /tmp/pip-install-6fztua11/cffi/
    Complete output (48 lines):
    unable to execute 'gcc': No such file or directory
    unable to execute 'gcc': No such file or directory
  
        No working compiler found, or bogus compiler options passed to
        the compiler from Python's standard "distutils" module.  See
        the error messages above.  Likely, the problem is not related
        to CFFI but generic to the setup.py of any Python package that
        tries to compile C code.  (Hints: on OS/X 10.8, for errors about
        -mno-fused-madd see 
        Otherwise, see https://wiki.python.org/moin/CompLangPython or
        the IRC channel #python on irc.freenode.net.)
  
        Trying to continue anyway.  If you are trying to install CFFI from
        a build done in a different context, you can ignore this warning.
  
    running bdist_wheel
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.8
    creating build/lib.linux-x86_64-3.8/cffi
    copying cffi/lock.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/pkgconfig.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/__init__.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/commontypes.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/error.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/verifier.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/api.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/cparser.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/model.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/recompiler.py -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/_cffi_include.h -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/parse_c_type.h -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/_embedding.h -> build/lib.linux-x86_64-3.8/cffi
    copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-3.8/cffi
    running build_ext
    building '_cffi_backend' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/c
    gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -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
    unable to execute 'gcc': No such file or directory
    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-6fztua11/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-6fztua11/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-zfw6b2gn/install-record.txt --single-version-externally-managed --prefix /tmp/pip-build-env-mytrl8h6/overlay --compile --install-headers /tmp/pip-build-env-mytrl8h6/overlay/include/python3.8/cffi
           cwd: /tmp/pip-install-6fztua11/cffi/
      Complete output (48 lines):
      unable to execute 'gcc': No such file or directory
      unable to execute 'gcc': No such file or directory
  
          No working compiler found, or bogus compiler options passed to
          the compiler from Python's standard "distutils" module.  See
          the error messages above.  Likely, the problem is not related
          to CFFI but generic to the setup.py of any Python package that
          tries to compile C code.  (Hints: on OS/X 10.8, for errors about
          -mno-fused-madd see 
          Otherwise, see https://wiki.python.org/moin/CompLangPython or
          the IRC channel #python on irc.freenode.net.)
  
          Trying to continue anyway.  If you are trying to install CFFI from
          a build done in a different context, you can ignore this warning.
  
      running install
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-3.8
      creating build/lib.linux-x86_64-3.8/cffi
      copying cffi/lock.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/pkgconfig.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/__init__.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/commontypes.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/error.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/cffi_opcode.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/verifier.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/vengine_gen.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/setuptools_ext.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/api.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/cparser.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/vengine_cpy.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/backend_ctypes.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/model.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/ffiplatform.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/recompiler.py -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/_cffi_include.h -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/parse_c_type.h -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/_embedding.h -> build/lib.linux-x86_64-3.8/cffi
      copying cffi/_cffi_errors.h -> build/lib.linux-x86_64-3.8/cffi
      running build_ext
      building '_cffi_backend' extension
      creating build/temp.linux-x86_64-3.8
      creating build/temp.linux-x86_64-3.8/c
      gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -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
      unable to execute 'gcc': No such file or directory
      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-6fztua11/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-6fztua11/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-zfw6b2gn/install-record.txt --single-version-externally-managed --prefix /tmp/pip-build-env-mytrl8h6/overlay --compile --install-headers /tmp/pip-build-env-mytrl8h6/overlay/include/python3.8/cffi Check the logs for full command output.
  WARNING: You are using pip version 20.1.1; however, version 21.0 is available.
  You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
  ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python /usr/local/lib/python3.8/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-mytrl8h6/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.12; platform_python_implementation != '"'"'PyPy'"'"'' Check the logs for full command output.
WARNING: You are using pip version 20.1.1; however, version 21.0 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.

接下来我该做什么?

感谢@DawidGacek 的建议,我已经为两个容器添加了依赖项,现在它工作正常。这是最后的工作 Dockerfile.prod [注意:我刚刚注释掉了 flake8 lint 检查器,如果您需要相同的,只需取消注释]

###########
# BUILDER #
###########

# pull official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev openssl-dev

# install other dependencies
RUN apk --update add \
    build-base \
    jpeg-dev \
    zlib-dev

# lint
# RUN pip install --upgrade pip
# RUN pip install flake8
# COPY . .
# RUN flake8 --ignore=E501,F401 .

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.8.3-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev openssl-dev

# install other dependencies
RUN apk update && apk add libpq
RUN apk --update add \
    build-base \
    jpeg-dev \
    zlib-dev

COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]