由于安装 psycopg2 出现问题,dockerizing Django+ PostgreSQL 项目失败

问题描述 投票:0回答:2

我创建了一个使用 PostgreSQL 作为数据库的 Django 项目。我已经有了 PostgreSQL 容器,并在尝试创建项目的映像时遇到以下错误:

Preparing metadata (setup.py): started
#8 46.75   Preparing metadata (setup.py): finished with status 'error'
#8 46.75   error: subprocess-exited-with-error
#8 46.75
#8 46.75   × python setup.py egg_info did not run successfully.
#8 46.75   │ exit code: 1
#8 46.75   ╰─> [25 lines of output]
#8 46.75       /usr/local/lib/python3.12/site-packages/setuptools/config/setupcfg.py:508: SetuptoolsDeprecationWarning: The license_file parameter is deprecated, use license_files instead.
#8 46.75         warnings.warn(msg, warning_class)
#8 46.75       running egg_info
#8 46.75       creating /tmp/pip-pip-egg-info-h_1_kknf/psycopg2.egg-info
#8 46.75       writing /tmp/pip-pip-egg-info-h_1_kknf/psycopg2.egg-info/PKG-INFO
#8 46.75       writing dependency_links to /tmp/pip-pip-egg-info-h_1_kknf/psycopg2.egg-info/dependency_links.txt
#8 46.75       writing top-level names to /tmp/pip-pip-egg-info-h_1_kknf/psycopg2.egg-info/top_level.txt
#8 46.75       writing manifest file '/tmp/pip-pip-egg-info-h_1_kknf/psycopg2.egg-info/SOURCES.txt'
#8 46.75
#8 46.75       Error: pg_config executable not found.
#8 46.75
#8 46.75       pg_config is required to build psycopg2 from source.  Please add the directory
#8 46.75       containing pg_config to the $PATH or specify the full executable path with the
#8 46.75       option:
#8 46.75
#8 46.75           python setup.py build_ext --pg-config /path/to/pg_config build ...
#8 46.75
#8 46.75       or with the pg_config option in 'setup.cfg'.
#8 46.75
#8 46.75       If you prefer to avoid building psycopg2 from source, please install the PyPI
#8 46.75       'psycopg2-binary' package instead.
#8 46.75
#8 46.75       For further information please check the 'doc/src/install.rst' file (also at
#8 46.75       <https://www.psycopg.org/docs/install.html>).
#8 46.75
#8 46.75       [end of output]
#8 46.75
#8 46.75   note: This error originates from a subprocess, and is likely not a problem with pip.
#8 46.75 error: metadata-generation-failed
#8 46.75
#8 46.75 × Encountered error while generating package metadata.
#8 46.75 ╰─> See above for output.
#8 46.75
#8 46.75 note: This is an issue with the package mentioned above, not pip.
#8 46.75 hint: See above for details.
#8 48.72
#8 48.72 [notice] A new release of pip is available: 23.0.1 -> 23.1.2
#8 48.72 [notice] To update, run: pip install --upgrade pip
------
executor failed running [/bin/sh -c pip install -r requirements.txt]: exit code: 1

值得一提的是,我的项目在本地运行得很好。项目与数据库连接完美,没有任何错误。问题发生在尝试创建 docker 容器时。似乎 psycopg2 软件包拒绝安装在容器内。

这是需求.txt

asgiref==3.6.0
Django==4.2
django-cors-headers==3.14.0
django-environ==0.10.0
djangorestframework==3.14.0
psycopg2==2.9.6
pytz==2023.3
sqlparse==0.4.4
tzdata==2023.3

这是 Dockerfile

FROM python:3.12.0a7-slim-buster

WORKDIR /MyBackend

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python" , "manage.py" , "runserver" , "0.0.0.0:8000"]

我使用Windows机器,我在诸如pg_config可执行文件未找到之类的页面上探索了这个问题 但没有一个答案解决了我的问题。当然,这些迷恋都没有引发像我这样的错误。

django postgresql docker psycopg2
2个回答
2
投票

slim
tiny
这样针对尺寸进行优化的图像通常会删除所有非必要的包,就像您的情况一样。

为了安装

psycopg2
,您需要两个额外的库,因此您的 Dockerfile 如下所示:

FROM python:3.12.0a7-slim-buster

WORKDIR /MyBackend

COPY requirements.txt .

RUN apt-get update && apt-get install -y gcc libpq-dev
RUN pip install -r requirements.txt

COPY . .

CMD ["python" , "manage.py" , "runserver" , "0.0.0.0:8000"]

并且您的

requirements.txt
也需要进行一些小调整:

asgiref==3.6.0
Django==4.2
django-cors-headers==3.14.0
django-environ==0.10.0
djangorestframework==3.14.0
psycopg2-binary==2.9.6
pytz==2023.3
sqlparse==0.4.4
tzdata==2023.3

这样您的形象就会按预期构建。


2
投票

在使用

python:3.10-slim
图像构建的 Docker 容器内运行 FastAPI 应用程序时,我遇到了同样的问题。通过更改为
python:3.10
来解决,因为磁盘空间对我来说不是问题。

© www.soinside.com 2019 - 2024. All rights reserved.