错误:未找到PG_CONFIG可执行文件-尝试停靠Django应用程序时

2022-04-14 00:00:00 python django docker psycopg2

问题描述

我正在尝试将Django应用程序与postgres对接:13.3-阿尔卑斯和3.8.11-超薄
以下是我的Dockerfile的内容:

FROM python:3.8.11-slim

ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install -r requirements.txt

docker-compose.yml:

version: "3"

services:
  web:
    build: .
    command: python backend/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres:13.3-alpine

您可以在下面找到我的错误:

#9 13.12     ERROR: Command errored out with exit status 1:
#9 13.12      command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-vftamw8l/psycopg2_a4b7534c24454475bc6e8699e7fd9f92/setup.py'"'"'; _file='"'"'/tmp/pip-install-vftamw8l/psycopg2_a4b7534c24454475bc6e8699e7fd9f92/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('"'"'
'"'"', '"'"'
'"'"');f.close();exec(compile(code, __file_, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-jnd2mp66
#9 13.12          cwd: /tmp/pip-install-vftamw8l/psycopg2_a4b7534c24454475bc6e8699e7fd9f92/
#9 13.12     Complete output (23 lines):
#9 13.12     running egg_info
#9 13.12     creating /tmp/pip-pip-egg-info-jnd2mp66/psycopg2.egg-info
#9 13.12     writing /tmp/pip-pip-egg-info-jnd2mp66/psycopg2.egg-info/PKG-INFO
#9 13.12     writing dependency_links to /tmp/pip-pip-egg-info-jnd2mp66/psycopg2.egg-info/dependency_links.txt
#9 13.12     writing top-level names to /tmp/pip-pip-egg-info-jnd2mp66/psycopg2.egg-info/top_level.txt
#9 13.12     writing manifest file '/tmp/pip-pip-egg-info-jnd2mp66/psycopg2.egg-info/SOURCES.txt'
#9 13.12
#9 13.12     Error: pg_config executable not found.
#9 13.12
#9 13.12     pg_config is required to build psycopg2 from source.  Please add the directory
#9 13.12     containing pg_config to the $PATH or specify the full executable path with the
#9 13.12     option:
#9 13.12
#9 13.12         python setup.py build_ext --pg-config /path/to/pg_config build ...
#9 13.12
#9 13.12     or with the pg_config option in 'setup.cfg'.
#9 13.12
#9 13.12     If you prefer to avoid building psycopg2 from source, please install the PyPI
#9 13.12     'psycopg2-binary' package instead.
#9 13.12
#9 13.12     For further information please check the 'doc/src/install.rst' file (also at
#9 13.12     <https://www.psycopg.org/docs/install.html>).
#9 13.12
#9 13.12     ----------------------------------------
#9 13.12 WARNING: Discarding https://files.pythonhosted.org/packages/aa/8a/7c80e7e44fb1b4277e89bd9ca509aefdd4dd1b2c547c6f293afe9f7ffd04/psycopg2-2.9.1.tar.gz#sha256=de5303a6f1d0a7a34b9d40e4d3bef684ccc44a49bbe3eb85e3c0bffb4a131b7c (from https://pypi.org/simple/psycopg2/) (requires-python:>=3.6). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#9 13.13 ERROR: Could not find a version that satisfies the requirement psycopg2==2.9.1 (from versions: 2.0.10, 2.0.11, 2.0.12, 2.0.13, 2.0.14, 2.2.0, 2.2.1, 2.2.2, 2.3.0, 2.3.1, 2.3.2, 2.4, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.4.5, 2.4.6, 2.5, 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.5.5, 2.6, 2.6.1, 2.6.2, 2.7, 2.7.1, 2.7.2, 2.7.3, 2.7.3.1, 2.7.3.2, 2.7.4, 2.7.5, 2.7.6, 2.7.6.1, 2.7.7, 2.8, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.8.5, 2.8.6, 2.9, 2.9.1)
#9 13.13 ERROR: No matching distribution found for psycopg2==2.9.1
------
executor failed running [/bin/sh -c pip install -r requirements.txt]: exit code: 1
ERROR: Service 'web' failed to build : Build failed

尝试了来自Stackoverflow的各种答案的许多修复程序,比如向下的那些,但似乎没有一项对我有效:
Error: pg_config executable not found when installing psycopg2 on Alpine in Docker
Failing to install psycopg2-binary on new docker container


解决方案

如果有人需要,我使用不同的docker镜像解决了这个问题。FROM python:3.9

相关文章