psycopg2-binary 无法安装在 docker 容器中

时间:2020-12-30 00:35:00

标签: python django postgresql docker docker-compose

对于新手问题深表歉意。我已经尝试了这里其他问题的所有答案,但也没有回报。

使用 Postgres 对我的 python/django 应用程序进行 Dockerizing 证明......令人生畏。我收到错误“错误:找不到 pg_config 可执行文件。”当它开始通过我的 requirements.txt 工作时始终如一

这是 Dockerfile:

FROM python:3.8.3-slim-buster
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD ./ /code/

...还有我的需求.txt...

asgiref==3.3.1
Django==3.1.4
psycopg2-binary==2.7.4
pytz==2020.5
sqlparse==0.4.1
django-compressor>=2.2
django-libsass>=0.7

和 docker-compose.yml

v ersion: "3.9"
   
services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes: 
      - pgdata:/var/lib/postgresql/data
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

volumes: 
  pgdata:

当我运行 docker-compose up --build 时,我一遍又一遍地收到此错误:

Step 6/7 : RUN pip install -r requirements.txt
 ---> Running in e0fd67d2d935
Collecting asgiref==3.3.1
  Downloading asgiref-3.3.1-py3-none-any.whl (19 kB)
Collecting Django==3.1.4
  Downloading Django-3.1.4-py3-none-any.whl (7.8 MB)
Collecting psycopg2-binary==2.7.4
  Downloading psycopg2-binary-2.7.4.tar.gz (426 kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-fha1c65p/psycopg2-binary/setup.py'"'"'; __file__='"'"'/tmp/pip-install-fha1c65p/psycopg2-binary/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-ewxlxmh6
         cwd: /tmp/pip-install-fha1c65p/psycopg2-binary/
    Complete output (23 lines):
    running egg_info
    creating /tmp/pip-pip-egg-info-ewxlxmh6/psycopg2_binary.egg-info
    writing /tmp/pip-pip-egg-info-ewxlxmh6/psycopg2_binary.egg-info/PKG-INFO
    writing dependency_links to /tmp/pip-pip-egg-info-ewxlxmh6/psycopg2_binary.egg-info/dependency_links.txt
    writing top-level names to /tmp/pip-pip-egg-info-ewxlxmh6/psycopg2_binary.egg-info/top_level.txt
    writing manifest file '/tmp/pip-pip-egg-info-ewxlxmh6/psycopg2_binary.egg-info/SOURCES.txt'
    
    Error: pg_config executable not found.

1 个答案:

答案 0 :(得分:0)

最终,答案似乎是在我的 Dockerfile 中的一些内容中找到的,但是......最终......从 Python 3.8 降级到 3.7 解锁了一切。

FROM python:3.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/

RUN apt-get update
RUN apt-get install -y postgresql 
RUN apt-get install libpq-dev gcc
RUN export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
RUN apt-get install -y python3-dev
RUN apt-get install -y python3-psycopg2 

RUN pip3 install -r requirements.txt
ADD ./ /code/