【发布时间】:2021-03-13 12:40:42
【问题描述】:
我正在使用 djangorestframework 和 postgresql 数据库开发应用程序。首先,我使用了 alpine:python3.8 图像,一切正常,包括 psycopg2。然后,我需要使用机器学习包。我无法使用 alpine:python3.8 图像安装 scipy、matplotlib、catsim 等。
所以我将图像从 alpine:python 图像更改为 alpine:3.7。除了安装 psycopg2 之外,我已经修复了许多错误。使用 pip 安装 psycopg2 时出现错误:
In file included from psycopg/psycopgmodule.c:28:0:
./psycopg/psycopg.h:35:20: fatal error: Python.h: No such file or directory
#include <Python.h>
^
compilation terminated.
It appears you are missing some prerequisite to build the package from source.
You may install a binary package by installing 'psycopg2-binary' from PyPI.
If you want to install psycopg2 from source, please install the packages
required for the build and try again.
For further information please check the 'doc/src/install.rst' file (also at
<https://www.psycopg.org/docs/install.html>).
我已经尝试了很多东西,阅读了与这个问题相关的所有问题。但它们都对我不起作用。
Dockerfiles的内容是
FROM alpine:3.7
MAINTAINER Test
ENV PYTHONUNBUFFERED 1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
ENV PACKAGES="\
dumb-init \
musl \
libc6-compat \
linux-headers \
build-base \
bash \
git \
ca-certificates \
freetype \
libgfortran \
libgcc \
libstdc++ \
openblas \
tcl \
tk \
libssl1.0 \
psycopg2-binary \
"
ENV PYTHON_PACKAGES="\
numpy \
matplotlib \
scipy \
scikit-learn \
pandas \
nltk \
"
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev
RUN apk add --update --no-cache postgresql-client
RUN apk add --virtual build-dependencies python3 --update py-pip \
RUN apk add --virtual build-runtime build-base build-essential postgresql-devel libpq-dev python3.8-dev libpython3.8-dev openblas-dev freetype-dev pkgconfig gfortran \
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h \
RUN pip3 install --upgrade pip \
RUN pip3 install $PYTHON_PACKAGES \
RUN apk del build-runtime \
RUN apk add --no-cache --virtual build-dependencies $PACKAGES \
RUN rm -rf /var/cache/apk/*
RUN pip3 install -r /requirements.txt
RUN apk del .tmp-build-deps
RUN mkdir /app
WORKDIR /app
COPY ./app /app
RUN adduser -D user
USER user
requirements.txt文件的内容是
Django>=3.1.5,<3.2.5
djangorestframework>=3.12.2,<3.13.0
psycopg2>=2.8.5,<2.9.5
djangorestframework_simplejwt
django-cors-headers
requests
djangorestframework-datatables
django-filter
flake8<3.8.4,<3.9.0
【问题讨论】:
-
只使用python slim image,alpine 是个不好的选择(对于python,一般来说不是)
-
非常感谢,我已将图像更改为 python-slim。现在所有机器学习包、Django 和数据库都可以正常工作。 :) 而且它比你建议的高山图像运行得更快。
标签: python docker dockerfile psycopg2