【问题标题】:Failing to install psycopg2-binary on new docker container无法在新的 docker 容器上安装 psycopg2-binary
【发布时间】:2020-07-03 12:35:03
【问题描述】:

我在尝试在新的 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.

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

【问题讨论】:

  • 我认为您需要在 pip 安装软件包之前安装 PostgreSQL 客户端。编辑:我在使用 Py3.8 时实际上遇到了同样的问题,我恢复到 Py3.7 并安装它没有任何问题。

标签: python django docker psycopg2


【解决方案1】:

我成功了。这是代码:

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-slim或者3.9.5-slim-buster就可以了。
  • Sergio 你是什么意思它不起作用?它现在正在工作,我的 django 项目使用此设置。
  • 我没有使它工作 psycog2 与版本 3.8.3。但我设法用版本3.9.5做到了。
【解决方案2】:

在 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 。现在会尝试解决这个问题。
  • 如果您使用 Compose,请确保您的数据库服务名称为 db
  • 其实,我在这里发了一篇关于它的帖子:stackoverflow.com/questions/62729917/…
  • 不幸的是,我在使用 python:3.8-slim-buster 时遇到了同样的错误。
  • 现在尝试切换到安装psycopg-binary
【解决方案3】:

此脚本适用于 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 docs.docker.com/desktop/mac/apple-silicon
  • 是的,我仍然需要安装 libpq-dev 和 gcc。
【解决方案4】:

这对我有用。试试超薄图像。

在你的Dockerfile

FROM python:3.8.7-slim-buster

在您的requirements.txt 文件中

psycopg2-binary~= <<version_number>>

【讨论】:

    【解决方案5】:

    我用

    制作了一张自定义图片
    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
    

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 2021-11-06
      • 1970-01-01
      • 2017-07-14
      • 2022-01-04
      • 1970-01-01
      • 2021-07-21
      • 2020-10-17
      • 1970-01-01
      相关资源
      最近更新 更多