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

时间:2017-10-12 14:17:54

标签: python postgresql docker psycopg2

我正在尝试使用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

这是我的Dockerfile

FROM python:3.6.3-alpine3.6

WORKDIR /usr/src/app

COPY . .

RUN pip install -r requirements.txt

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

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

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

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

3 个答案:

答案 0 :(得分:43)

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

# 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"]

说明:要构建Psycopg,您需要包gcc musl-dev postgresql-dev。然后你还需要pg_config可执行文件:只需安装postgresql-dev即可,postgresql-libs也可以正常工作,并占用大约12 MB的空间。

这是答案的原始版本(基于this Dockerfile),我将Python手动安装到纯Alpine图像上,因为当时Python没有为Python 3.6和Alpine 3.7提供Docker镜像。如果你想安装这样的Python 2.7,也可以apk add py2-pip(在旧的Alpine回购中称为py-pip)。

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"]

答案 1 :(得分:13)

在Dockerfile中从var result2Tables = _customerRepository.Table.Join(_sourcedefinitionepository.Table, c => c.SouceCode , s => s.SourceCode, (c, s) => new { Customer = c, SourceName = s })).Where(p => p.Customer.SirketKod == 1 && p.SourceName .UretimKanali == "E") var result = result2Tables.Join(_branchdefinitionepository.Table, p => p.Customer.BranchCode, b => b.BranchCode, (r, b) => new { Customer = r, SourceName = r.SourceName.LongName, b.BranchName }) 安装依赖项之前,只需添加这些命令(psycopg2依赖项)

requirements.txt

来源:enter image description here

答案 2 :(得分:1)

您可以尝试:

pip install psycopg2-binary