在新的Docker容器上安装psycopg2-binary失败

35

我在尝试在新的Docker容器上运行我的django项目时遇到了问题。这是我第一次使用Docker,似乎找不到一个好的方法在其中运行django项目。尝试了多个教程后,我总是收到关于未安装psycopg2的错误。

requirements.txt:

-i https://pypi.org/simple
asgiref==3.2.7
django-cors-headers==3.3.0
django==3.0.7
djangorestframework==3.11.0
gunicorn==20.0.4
psycopg2-binary==2.8.5
pytz==2020.1
sqlparse==0.3.1

Dockerfile:

# pull official base image
FROM python:3.8.3-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip

COPY ./requirements.txt .
RUN pip install -r requirements.txt

# copy project
COPY . .

# set project environment variables
# grab these via Python's os.environ
# these are 100% optional here

ENV PORT=8000
ENV SECRET_KEY_TWITTER = "***"
在运行docker-compose build时,我遇到了以下错误:
Error: pg_config executable not found.

pg_config is required to build psycopg2 from source. 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'.

If you prefer to avoid building psycopg2 from source, please install the PyPI

'psycopg2-binary' package instead.

我很乐意回答任何可能导致解决方案的问题。另外,也许有人可以向我推荐一个关于将Django应用程序容器化的好教程?


我认为在pip安装包之前,您需要安装PostgreSQL客户端。编辑:实际上,当我使用Py3.8时,我遇到了同样的问题,然后我回退到Py3.7并且没有遇到任何问题就成功安装了它。 - Joshua Nixon
7个回答

68

我搞定了,这是代码:

FROM python:3.8.3-slim #Image python:3.9.5-slim also works # Image python:3.9.5-slim-buster also works

RUN apt-get update \
    && apt-get -y install libpq-dev gcc \
    && pip install psycopg2
    

这对于 psycopg2 不起作用。但是如果您更改为图像 3.9.5-slim3.9.5-slim-buster,它将会起作用。 - Sergio
1
Sergio,你是什么意思,它不会工作?现在它正在工作,我的Django项目正在使用这些设置。 - Zoltán Buzás
我无法让 psycog23.8.3 版本上工作。但是我成功在 3.9.5 版本上实现了它。 - Sergio
按照这种方式做对我起作用了。不起作用的是通过requirements.txt安装它。 - 5hizzle

24
在Alpine Linux上,即使在PyPI上有预编译的二进制包,您仍需要编译所有软件包。在标准的基于Linux的映像中,则不需要(参见https://pythonspeed.com/articles/alpine-docker-python/ - 那里还有其他我写过的文章,例如关于安全性的文章)。
因此,请将基础映像更改为python:3.8.3-slim-busterpython:3.8-slim-buster,然后应该可以正常工作。

它实际上解决了我的问题,即使只是部分地,谢谢!不幸的是,现在在运行容器时出现错误,django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known。我现在要尝试解决这个问题。 - kczan
如果您正在使用Compose,请确保您的数据库服务名称为“db”。 - Itamar Turner-Trauring
我实际上已经在这里发布了一个帖子:https://stackoverflow.com/questions/62729917/connecting-postgresql-db-to-django-project-using-docker - kczan
7
使用 python:3.8-slim-buster 时,不幸的是我得到了相同的错误。 - Phil Gyford
2
现在尝试切换到安装 psycopg-binary - Itamar Turner-Trauring

11
这份脚本可以在 MacBook Air M1 上运行。
Dockerfile。
FROM ubuntu:20.04
RUN apt-get update && apt-get -y install libpq-dev gcc && pip install psycopg2
COPY requirements.txt /cs_account/
RUN pip3 install -r requirements.txt

requirements.txt

psycopg2-binary~=2.8.6

基于Zoltán Buzás的回答更新


你尝试过运行 GOARCH=amd64 docker build .... 吗?如果指定了 GOARCH,你还需要安装 gcc 吗?我没有 M1 Mac,但我正在尝试帮助另一位使用 M1 Mac 的工程师。谢谢!注意:您需要安装 Rosetta https://docs.docker.com/desktop/mac/apple-silicon/ - Druhin Bala
1
是的,我仍然需要安装libpq-dev和gcc。 - Sadidul Islam

5

这对我有效。尝试slim-buster镜像。

在你的Dockerfile中。

FROM python:3.8.7-slim-buster

并且在你的requirements.txt文件中

psycopg2-binary~= <<version_number>>

2

我将此添加到顶部答案,因为我遇到了其他类似下面的错误:

gcc: error trying to exec 'cc1plus': execvp: No such file or directory
error: command 'gcc' failed with exit status 1

并且

src/pyodbc.h:56:10: fatal error: sql.h: No such file or directory
#include <sql.h>

这是我解决这个问题的方法,所以我不确定其他人是如何让它工作的,也许是因为我做了一些其他的事情?
当我在谷歌上搜索这两个错误时,从其他帖子中找到了我的解决方案。
FROM python:3.8.3-slim 

RUN apt-get update \
&& apt-get -y install g++ libpq-dev gcc unixodbc unixodbc-dev

修好了,g++ 似乎起了作用。 - Jakob

0
对于可能遇到这个问题的其他人(我知道楼主没有这个问题),可能是你试图安装psycopg2而不是psycopg2-binary,这就是我的问题。

值得注意的是,psycopg2和psycopg2-binary之间存在差异:链接 - 建议在生产环境中使用源代码分发。 - Jakob

0

我已经制作了一个自定义图像

FROM python:alpine
ADD requirements.txt /
RUN apk update --no-cache \
&& apk add build-base postgresql-dev libpq --no-cache --virtual .build-deps \
&& pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r /requirements.txt \
&& apk del .build-deps
RUN apk add postgresql-libs libpq --no-cache

和 requirements.txt

django
djangorestframework
psycopg2-binary

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接