dockerizing Django+ PostgreSQL项目失败,因为安装psycopg2时出现问题

wbgh16ku  于 2023-05-06  发布在  Docker
关注(0)|答案(2)|浏览(172)

我创建了一个使用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包拒绝安装在容器内。
这里是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==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 executable not found等页面上探讨了这个问题,但没有一个答案解决了我的问题。诅咒的是,没有一个人的迷恋像我一样犯了错误。

kqlmhetl

kqlmhetl1#

针对slimtiny等尺寸进行优化的图像通常会删除每个不必要的软件包,就像您的情况一样。
为了安装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

这样,你的形象就会像预期的那样。

a6b3iqyw

a6b3iqyw2#

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

相关问题