postgresql python的psycopg2安装:Docker中的2.7-alpine
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41574928/
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
psycopg2 installation for python:2.7-alpine in Docker
提问by salehinejad
To use PostgreSql in python I need to
要在 python 中使用 PostgreSql,我需要
pip install psycopg2
However, it has dependency on libpq-dev and python-dev. I wonder how can I install the dependencies in alpine? Thanks.
但是,它依赖于 libpq-dev 和 python-dev。我想知道如何在 alpine 中安装依赖项?谢谢。
Here is a Dockerfile:
这是一个 Dockerfile:
FROM python:2.7-alpine
RUN apk add python-dev libpq-dev
RUN pip install psycopg2
and the output is:
输出是:
Step 3 : RUN apk add python-dev libpq-dev ---> Running in 3223b1bf7cde WARNING: Ignoring APKINDEX.167438ca.tar.gz: No such file or directory WARNING: Ignoring APKINDEX.a2e6dac0.tar.gz: No such file or directory ERROR: unsatisfiable constraints: libpq-dev (missing): required by: world[libpq-dev] python-dev (missing): required by: world[python-dev] ERROR: Service 'service' failed to build: The command '/bin/sh -c apk add python-dev libpq-dev' returned a non-zero code: 2
第 3 步:运行 apk add python-dev libpq-dev ---> 在 3223b1bf7cde 中运行警告:忽略 APKINDEX.167438ca.tar.gz:无此类文件或目录警告:忽略 APKINDEX.a2e6dac0.tar.gz:无此类文件或目录错误:无法满足的约束:libpq-dev(缺失):需要:world[libpq-dev] python-dev(缺失):需要:world[python-dev] 错误:服务“service”未能构建:命令'/bin/sh -c apk add python-dev libpq-dev' 返回非零代码:2
回答by Sant
If you only need to install psycopg2 for python 2.7 on Docker image based on python:2.7-alpinethen following code for Dockerfile will be nice for you:
如果您只需要在基于python:2.7-alpine 的Docker 映像上为 python 2.7 安装 psycopg2,那么 Dockerfile 的以下代码对您来说会很好:
FROM python:2.7-alpine
RUN apk update && \
apk add --virtual build-deps gcc python-dev musl-dev && \
apk add postgresql-dev
RUN pip install psycopg2
回答by Felipe Buccioni
An explanation before compile/install psycopg2
编译/安装psycopg2前的说明
libpq
is the client library for PostgreSQL https://www.postgresql.org/docs/9.5/libpq.html
libpq
是 PostgreSQL 的客户端库https://www.postgresql.org/docs/9.5/libpq.html
postgresql-dev
are the package with the headers to link libpq in a library/binary as is psycopg.
postgresql-dev
是带有头文件的包,用于在库/二进制文件中链接 libpq,就像 psycopg 一样。
I use the following configuration in alpine 3.7, I add some comments to explain it.
我在alpine 3.7中使用了以下配置,我添加了一些注释来解释它。
# Installing client libraries and any other package you need
RUN apk update && apk add libpq
# Installing build dependencies
# For python3 you need to add python3-dev *please upvote the comment
# of @its30 below if you use this*
RUN apk add --virtual .build-deps gcc python-dev musl-dev postgresql-dev
# Installing and build python module
RUN pip install psycopg2
# Delete build dependencies
RUN apk del .build-deps
回答by pbatey
I couldn't get it to install from python:2.7.13-alpine
. Ended up with this:
我无法从python:2.7.13-alpine
. 结束了这个:
FROM gliderlabs/alpine:3.3
RUN apk add --no-cache --update \
python \
python-dev \
py-pip \
build-base
RUN apk add --virtual build-deps gcc python-dev musl-dev && \
apk add --no-cache --update postgresql-dev && \
pip install psycopg2==2.7.1
回答by Maximilian Kindshofer
Seems like the package you need is libpq not lobpq-dev:
看起来你需要的包是 libpq 而不是 lobpq-dev:
https://pkgs.alpinelinux.org/package/edge/main/x86/py2-psycopg2
https://pkgs.alpinelinux.org/package/edge/main/x86/py2-psycopg2
Have a look at the dependencies at the right
查看右侧的依赖项
回答by Aleksandr
Had problems with running Python 3.7 and PostgreSQL under Alpine Linux in Docker. This article helped https://www.rockyourcode.com/install-psycopg2-binary-with-docker/
在 Docker 中在 Alpine Linux 下运行 Python 3.7 和 PostgreSQL 时遇到问题。这篇文章帮助https://www.rockyourcode.com/install-psycopg2-binary-with-docker/
The main thing is to reference psypcopg2-binary
in your requirements file and install the following packages (in Dockerfile):
主要是psypcopg2-binary
在您的需求文件中引用并安装以下软件包(在 Dockerfile 中):
RUN apk update && \
apk add --no-cache --virtual build-deps gcc python3-dev musl-dev && \
apk add postgresql-dev
回答by Prakash Takkalaki
add it in dockerfile
将它添加到 dockerfile
RUN apk update && apk add --no-cache --virtual .build-deps\
postgresql-dev gcc libpq python3-dev musl-dev linux-headers\
&& pip install --no-cache-dir -r requirements.txt\
&& apk del .build-deps\
&& rm -rf /var/cache/apk/*