Проблема после сборки контейнера Docker на Alpine

Как избавится от данной ошибки. Буду благодарен за любую помощь!

Waiting for postgres...

PostgreSQL started

[2021-05-30 14:44:07 +0000] [1] [INFO] Starting gunicorn 20.1.0

[2021-05-30 14:44:07 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)

[2021-05-30 14:44:07 +0000] [1] [INFO] Using worker: sync

[2021-05-30 14:44:07 +0000] [10] [INFO] Booting worker with pid: 10

[2021-05-30 14:44:07 +0000] [10] [ERROR] Exception in worker process

Traceback (most recent call last):

File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 25, in <module>

import psycopg2 as Database

ModuleNotFoundError: No module named 'psycopg2'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

File "/usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker

worker.init_process()

File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process

self.load_wsgi()

File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi

self.wsgi = self.app.wsgi()

File "/usr/local/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi

self.callable = self.load()

File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load

return self.load_wsgiapp()

File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp

return util.import_app(self.app_uri)

File "/usr/local/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app

mod = importlib.import_module(module)

File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 1014, in _gcd_import

File "<frozen importlib._bootstrap>", line 991, in _find_and_load

File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked

File "<frozen importlib._bootstrap>", line 671, in _load_unlocked

File "<frozen importlib._bootstrap_external>", line 783, in exec_module

File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

File "/home/commers/web/shop/wsgi.py", line 16, in <module>

application = get_wsgi_application()

File "/usr/local/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application

django.setup(set_prefix=False)

File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup

apps.populate(settings.INSTALLED_APPS)

File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate

app_config.import_models()

File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 301, in import_models

self.models_module = import_module(models_module_name)

File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 1014, in _gcd_import

File "<frozen importlib._bootstrap>", line 991, in _find_and_load

File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked

File "<frozen importlib._bootstrap>", line 671, in _load_unlocked

File "<frozen importlib._bootstrap_external>", line 783, in exec_module

File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed

File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/models.py", line 3, in <module>

from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager

File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 48, in <module>

class AbstractBaseUser(models.Model):

File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 122, in __new__

new_class.add_to_class('_meta', Options(meta, app_label))

File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 326, in add_to_class

value.contribute_to_class(cls, name)

File "/usr/local/lib/python3.8/site-packages/django/db/models/options.py", line 207, in contribute_to_class

self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())

File "/usr/local/lib/python3.8/site-packages/django/utils/connection.py", line 15, in __getattr__

return getattr(self._connections[self._alias], item)

File "/usr/local/lib/python3.8/site-packages/django/utils/connection.py", line 62, in __getitem__

conn = self.create_connection(alias)

File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 204, in create_connection

backend = load_backend(db['ENGINE'])

File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 111, in load_backend

return import_module('%s.base' % backend_name)

File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 29, in <module>

raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

[2021-05-30 14:44:07 +0000] [10] [INFO] Worker exiting (pid: 10)

[2021-05-30 14:44:07 +0000] [1] [INFO] Shutting down: Master

[2021-05-30 14:44:07 +0000] [1] [INFO] Reason: Worker failed to boot.

Search...
Stick to bottom

Сама конфигурация была испробована в разных вариантах!

FROM python:3.8.3-alpine as builder

WORKDIR /usr/src/commers

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV export LD_LIBRARY_PATH=/usr/local/lib

RUN apk update \
    && pip install --upgrade pip \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add libpq \
    && apk add postgresql \
    && apk add postgresql-dev \
    && pip install psycopg2-binary \
    && apk add jpeg-dev zlib-dev libjpeg \
    && pip install Pillow \
    && apk del build-deps

RUN pip install --upgrade pip
COPY ./requirements.txt .
COPY . .

COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/commers/wheels -r requirements.txt

При добавлении psycopq2 в requirements.txt получается следующая ошибка

 > [builder 6/8] RUN pip install -r requirements.txt:                                      
#16 2.199 Collecting Django==3.2.3                                                         
#16 3.136   Downloading Django-3.2.3-py3-none-any.whl (7.9 MB)                             
#16 20.15 Collecting psycopg2                                                              
#16 20.27   Downloading psycopg2-2.8.6.tar.gz (383 kB)                                     
#16 22.49 Collecting asgiref==3.3.4
#16 22.61   Downloading asgiref-3.3.4-py3-none-any.whl (22 kB)
#16 22.89 Collecting django-crispy-forms==1.11.2
#16 23.03   Downloading django_crispy_forms-1.11.2-py3-none-any.whl (112 kB)
#16 23.44 Collecting django-debug-toolbar==3.2.1
#16 23.58   Downloading django_debug_toolbar-3.2.1-py3-none-any.whl (199 kB)
#16 24.16 Collecting gunicorn==20.1.0
#16 24.29   Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB)
#16 24.99 Collecting phonenumbers==8.12.23
#16 25.18   Downloading phonenumbers-8.12.23-py2.py3-none-any.whl (2.6 MB)
#16 29.84 Collecting pytils==0.3
#16 29.98   Downloading pytils-0.3.tar.gz (89 kB)
#16 31.20 Collecting pytz==2020.1
#16 31.33   Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
#16 32.67 Collecting sqlparse==0.4.1
#16 32.86   Downloading sqlparse-0.4.1-py3-none-any.whl (42 kB)
#16 33.05 Requirement already satisfied: setuptools>=3.0 in /usr/local/lib/python3.8/site-packages (from gunicorn==20.1.0->-r requirements.txt (line 6)) (47.1.1)
#16 33.08 Building wheels for collected packages: pytils, psycopg2
#16 33.08   Building wheel for pytils (setup.py): started
#16 33.75   Building wheel for pytils (setup.py): finished with status 'done'
#16 33.75   Created wheel for pytils: filename=pytils-0.3-py3-none-any.whl size=40354 sha256=23d1ffa10e5ff4ce1b7079d8f0d4c2106dc35a66c515da3b8b5e1ed78643591b
#16 33.75   Stored in directory: /root/.cache/pip/wheels/66/79/1f/bd77399f1515db6e172673c3346e1a7df2a1235c90c10ae8ba
#16 33.75   Building wheel for psycopg2 (setup.py): started
#16 34.28   Building wheel for psycopg2 (setup.py): finished with status 'error'
#16 34.28   ERROR: Command errored out with exit status 1:
#16 34.28    command: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/setup.py'"'"'; __file__='"'"'/tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/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'"'"'))' bdist_wheel -d /tmp/pip-wheel-ejnn0fni
#16 34.28        cwd: /tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/
#16 34.28   Complete output (38 lines):
#16 34.28   running bdist_wheel
#16 34.28   running build
#16 34.28   running build_py
#16 34.28   creating build
#16 34.28   creating build/lib.linux-x86_64-3.8
#16 34.28   creating build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/_lru_cache.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/compat.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/errorcodes.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/_json.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/tz.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/errors.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/extensions.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/_range.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/pool.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/sql.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   copying lib/extras.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 34.28   warning: build_py: byte-compiling is disabled, skipping.
#16 34.28   
#16 34.28   running build_ext
#16 34.28   building 'psycopg2._psycopg' extension
#16 34.28   creating build/temp.linux-x86_64-3.8
#16 34.28   creating build/temp.linux-x86_64-3.8/psycopg
#16 34.28   gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSYCOPG_VERSION=2.8.6 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120007 -DHAVE_LO64=1 -I/usr/local/include/python3.8 -I. -I/usr/include -I/usr/include/postgresql/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.8/psycopg/psycopgmodule.o -Wdeclaration-after-statement
#16 34.28   unable to execute 'gcc': No such file or directory
#16 34.28   
#16 34.28   It appears you are missing some prerequisite to build the package from source.
#16 34.28   
#16 34.28   You may install a binary package by installing 'psycopg2-binary' from PyPI.
#16 34.28   If you want to install psycopg2 from source, please install the packages
#16 34.28   required for the build and try again.
#16 34.28   
#16 34.28   For further information please check the 'doc/src/install.rst' file (also at
#16 34.28   <https://www.psycopg.org/docs/install.html>).
#16 34.28   
#16 34.28   error: command 'gcc' failed with exit status 1
#16 34.28   ----------------------------------------
#16 34.28   ERROR: Failed building wheel for psycopg2
#16 34.28   Running setup.py clean for psycopg2
#16 34.72 Successfully built pytils
#16 34.72 Failed to build psycopg2
#16 34.78 Installing collected packages: sqlparse, pytz, asgiref, Django, pytils, psycopg2, phonenumbers, gunicorn, django-debug-toolbar, django-crispy-forms
#16 36.47     Running setup.py install for psycopg2: started
#16 36.97     Running setup.py install for psycopg2: finished with status 'error'
#16 36.97     ERROR: Command errored out with exit status 1:
#16 36.97      command: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/setup.py'"'"'; __file__='"'"'/tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/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-julo4fg1/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/psycopg2
#16 36.97          cwd: /tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/
#16 36.97     Complete output (38 lines):
#16 36.97     running install
#16 36.97     running build
#16 36.97     running build_py
#16 36.97     creating build
#16 36.97     creating build/lib.linux-x86_64-3.8
#16 36.97     creating build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/_lru_cache.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/compat.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/errorcodes.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/_json.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/tz.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/errors.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/extensions.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/_range.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/pool.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/sql.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     copying lib/extras.py -> build/lib.linux-x86_64-3.8/psycopg2
#16 36.97     warning: build_py: byte-compiling is disabled, skipping.
#16 36.97     
#16 36.97     running build_ext
#16 36.97     building 'psycopg2._psycopg' extension
#16 36.97     creating build/temp.linux-x86_64-3.8
#16 36.97     creating build/temp.linux-x86_64-3.8/psycopg
#16 36.97     gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -DPSYCOPG_VERSION=2.8.6 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=120007 -DHAVE_LO64=1 -I/usr/local/include/python3.8 -I. -I/usr/include -I/usr/include/postgresql/server -c psycopg/psycopgmodule.c -o build/temp.linux-x86_64-3.8/psycopg/psycopgmodule.o -Wdeclaration-after-statement
#16 36.97     unable to execute 'gcc': No such file or directory
#16 36.97     
#16 36.97     It appears you are missing some prerequisite to build the package from source.
#16 36.97     
#16 36.97     You may install a binary package by installing 'psycopg2-binary' from PyPI.
#16 36.97     If you want to install psycopg2 from source, please install the packages
#16 36.97     required for the build and try again.
#16 36.97     
#16 36.97     For further information please check the 'doc/src/install.rst' file (also at
#16 36.97     <https://www.psycopg.org/docs/install.html>).
#16 36.97     
#16 36.97     error: command 'gcc' failed with exit status 1
#16 36.97     ----------------------------------------
#16 36.97 ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/setup.py'"'"'; __file__='"'"'/tmp/pip-install-m56o8wut/psycopg2_d323af2fcab5442aae12fecd272f4f01/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-julo4fg1/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/psycopg2 Check the logs for full command output.
------
executor failed running [/bin/sh -c pip install -r requirements.txt]: exit code: 1
ERROR: Service 'web' failed to build : Build failed

Полностью весь Dockerfile выглядит так

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

# set work directory
WORKDIR /usr/src/commers

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1


# install psycopg2 dependencies
RUN apk update \
    && pip install --upgrade pip \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add libpq \
    && apk add postgresql \
    && apk add postgresql-dev \
    && pip install psycopg2-binary \
    && apk add jpeg-dev zlib-dev libjpeg \
    && pip install Pillow \
    && apk del build-deps
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/commers/wheels -r requirements.txt
COPY . .

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

# pull official base image
FROM python:3.8.3-alpine

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

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

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

# install dependencies
RUN apk update && apk add libpq
#RUN pip install psycopg2-binary
COPY --from=builder /usr/src/commers/wheels /wheels
COPY --from=builder /usr/src/commers/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 commers:commers $APP_HOME

# change to the app user
USER commers

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

Ответы (1 шт):

Автор решения: Sergey Mitrofanov

На 6 шаге у вас нет gcc.

Потому-что его нет.

  && apk add --virtual build-deps gcc python3-dev musl-dev
  ...
  && apk del build-deps

Если gcc нужен. Этот фокус нужно повторить во втором контейнере. И удалять виртуальный пакет, только после сборки зависимостей, или просто поставить gcc и библиотеки без ключа apk del build-deps

→ Ссылка