Python 错误:在 Docker 中在 Alpine 上安装 psycopg2 时找不到 pg_config 可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46711990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:46:46  来源:igfitidea点击:

Error: pg_config executable not found when installing psycopg2 on Alpine in Docker

pythonpostgresqldockerpsycopg2

提问by hidace

I'm trying to build a Flask app using Postgres with Docker. I'd like to connect to an AWS RDS instance of Postgres, but use Docker for my Flask app. However, when trying to set up psycopg2it runs into an error because it can't find pg_config. Here's the error:

我正在尝试使用 Postgres 和 Docker 构建 Flask 应用程序。我想连接到 Postgres 的 AWS RDS 实例,但将 Docker 用于我的 Flask 应用程序。但是,当尝试设置psycopg2它时会遇到错误,因为它找不到pg_config. 这是错误:

Building api
Step 1/5 : FROM python:3.6.3-alpine3.6
 ---> 84c98ca3b5c5
Step 2/5 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 407c158f5ee4
Step 3/5 : COPY . .
 ---> 966df18d329e
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in 284cc97aeb63
Collecting aniso8601==1.3.0 (from -r requirements.txt (line 1))
  Downloading aniso8601-1.3.0.tar.gz (57kB)
Collecting click==6.7 (from -r requirements.txt (line 2))
  Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting Flask==0.12.2 (from -r requirements.txt (line 3))
  Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
Collecting Flask-RESTful==0.3.6 (from -r requirements.txt (line 4))
  Downloading Flask_RESTful-0.3.6-py2.py3-none-any.whl
Collecting Flask-SQLAlchemy==2.3.2 (from -r requirements.txt (line 5))
  Downloading Flask_SQLAlchemy-2.3.2-py2.py3-none-any.whl
Collecting itsdangerous==0.24 (from -r requirements.txt (line 6))
  Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Jinja2==2.9.6 (from -r requirements.txt (line 7))
  Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB)
Collecting MarkupSafe==1.0 (from -r requirements.txt (line 8))
  Downloading MarkupSafe-1.0.tar.gz
Collecting psycopg2==2.7.3.1 (from -r requirements.txt (line 9))
  Downloading psycopg2-2.7.3.1.tar.gz (425kB)
    Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
    Error: pg_config executable not found.

    Please add the directory containing pg_config to the PATH
    or specify the full executable path with the option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-01lf5grh/psycopg2/
ERROR: Service 'api' failed to build: The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

Here's my Dockerfile:

这是我的Dockerfile

FROM python:3.6.3-alpine3.6

WORKDIR /usr/src/app

COPY . .

RUN pip install -r requirements.txt

CMD ["python", "app.py"]

Many others seem to have a similar issue locally, but none of them involve using Docker. This seems like a Docker issue because I can set up a local virtual environment and the setup works just fine since I have Postgres installed locally and it's able to find my local pg_config.

许多其他人似乎在本地也有类似的问题,但没有一个涉及使用 Docker。这似乎是一个 Docker 问题,因为我可以设置一个本地虚拟环境并且设置工作得很好,因为我在本地安装了 Postgres 并且它能够找到我本地的pg_config.

It appears that during the container build/setup, Docker is trying to find pg_configwithin the container. Is there a way to install a pg_configin the container, even though I won't be using a containerized instance of Postgres, but rather the instance on RDS?

似乎在容器构建/设置期间,Docker 试图pg_config在容器内查找。有没有办法pg_config在容器中安装 a ,即使我不会使用 Postgres 的容器化实例,而是使用 RDS 上的实例?

Any and all suggestions on how to get around this are welcomed. Thanks!

欢迎任何和所有有关如何解决此问题的建议。谢谢!

回答by Norrius

Tested with Python 3.4.8, 3.5.5, 3.6.5 and 2.7.14 (just replace 3 with 2):

使用 Python 3.4.8、3.5.5、3.6.5 和 2.7.14 测试(只需将 3 替换为 2):

# You can use a specific version too, like python:3.6.5-alpine3.7
FROM python:3-alpine

WORKDIR /usr/src/app

COPY requirements.txt .

RUN \
 apk add --no-cache postgresql-libs && \
 apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev && \
 python3 -m pip install -r requirements.txt --no-cache-dir && \
 apk --purge del .build-deps

COPY . .

CMD ["python3", "app.py"]

Explanation: to build Psycopg you need the packages gcc musl-dev postgresql-dev. Then you also need that pg_config executable: while simply installing postgresql-devwill work, postgresql-libsdoes fine too and takes up some 12 MB less space.

说明:要构建 Psycopg,您需要包gcc musl-dev postgresql-dev。然后你还需要 pg_config 可执行文件:虽然简单地安装postgresql-dev可以工作,postgresql-libs但也很好,并且占用的空间减少了 12 MB。



Here's the original version of the answer (based on this Dockerfile) where I manually install Python onto a pure Alpine image because at that time Python did not provide the Docker image with Python 3.6 and Alpine 3.7. If you want to install Python 2.7 like that, also do apk add py2-pip(called py-pipin older Alpine repos).

这是答案的原始版本(基于此 Dockerfile),其中我手动将 Python 安装到纯 Alpine 映像上,因为当时 Python 未提供带有 Python 3.6 和 Alpine 3.7 的 Docker 映像。如果您想像这样安装 Python 2.7,也可以这样做apk add py2-pippy-pip在较旧的 Alpine 存储库中调用)。

FROM alpine:3.7

WORKDIR /usr/src/app

COPY requirements.txt .

RUN \
 apk add --no-cache python3 postgresql-libs && \
 apk add --no-cache --virtual .build-deps gcc python3-dev musl-dev postgresql-dev && \
 python3 -m pip install -r requirements.txt --no-cache-dir && \
 apk --purge del .build-deps

COPY . .

CMD ["python3", "app.py"]

回答by Vivin Veerali

You could try:

你可以试试:

pip install psycopg2-binary

回答by Tasawar Hussain

Simply add these commands (psycopg2 dependencies) before installing dependencies from requirements.txtin Dockerfile

在从requirements.txtDockerfile安装依赖项之前,只需添加这些命令(psycopg2 依赖项)

# install psycopg2 dependencies
RUN apk update
RUN apk add postgresql-dev gcc python3-dev musl-dev

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

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