枕头套件的Docker Django安装错误

时间:2019-10-23 10:18:06

标签: django docker

我正在对我的django应用进行docker化,如果您使用django image字段,您都知道,您需要使用Pillow软件包,但是当前我的docker安装了所有软件包,并在尝试安装pillow时显示错误

我的Dockerfile

# pull official base image
FROM python:3.7-alpine

# set work directory
WORKDIR /app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0

# install psycopg2
RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add postgresql-dev \
    && pip install psycopg2 \
    && apk del build-deps

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

# copy project
COPY . .

# collect static files
RUN python manage.py collectstatic --noinput

# add and run as non-root user
RUN adduser -D myuser
USER myuser

# run gunicorn
CMD gunicorn projectile.wsgi:application --bind 0.0.0.0:$PORT

这是requirements.txt文件

Django==2.2.2
Pillow==5.0.0
dj-database-url==0.5.0
gunicorn==19.9.0
whitenoise==4.1.2
psycopg2==2.8.4

我没有发现问题,为什么pilow没有安装,它抛出错误,如下所示:

The headers or library files could not be found for zlib,
remote:     a required dependency when compiling Pillow from source.
remote:     
remote:     Please see the install instructions at:
remote:        https://pillow.readthedocs.io/en/latest/installation.html
remote:     
remote:     
remote:     ----------------------------------------
remote: ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-k4_gcdwn/Pillow/setup.py'"'"'; __file__='"'"'/tmp/pip-install-k4_gcdwn/Pillow/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-qgvai9fm/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.

有人可以帮助我解决此错误吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以在apk添加部分中添加zlib-dev并在那里安装枕头。例如(注释部分中有解释):

RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev zlib-dev postgresql-dev jpeg-dev \  # will be removed after dependent python packages are installed
    && apk add postgresql zlib jpeg \  # these packages won't be deleted from docker image
    && pip install psycopg2 Pillow==5.0.0 \  # Here I am installing these python packages which have dependencies on the libraries installed in build-deps, because later build-deps will be deleted
    && apk del build-deps  # for reducing the size of the Docker Image, we are removing the build-deps folder

# install dependencies
COPY ./requirements.txt .

RUN pip install -r requirements.txt
# Rest of the code