Docker:无法使用 ImageField,因为未安装 Pillow

Dom*_*nik 5 django python-imaging-library docker

我的 Docker 有问题。在我的 Docker 开发版本中它可以工作,但是当我创建产品版本时它说 Pillow 未安装。从昨天起我一直在寻找解决方案,但没有一个对我有用。提前致谢。

当我运行 flake8 时,有一个关于二元运算符之前的一些换行符和 Pillow 中的空格的奇怪错误。

version: '3.7'

services:

  backend:
    build:
      context: ./backend/carrent
      dockerfile: Dockerfile.prod
    container_name: django-backend
    command: gunicorn carrent.wsgi:application --bind 0.0.0.0:8000
    ports:
      - 8000:8000
    env_file:
      - ./.env.prod
    depends_on:
      - db

  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.db.prod

volumes:
  postgres_data:
Run Code Online (Sandbox Code Playgroud)

Dockerfile.prod

###########
# BUILDER #
###########

# pull official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/backend

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install Pillow dependencies
RUN apk add libpng-dev tiff-dev libjpeg gcc libgcc musl-dev
RUN apk add jpeg-dev zlib-dev
RUN apk add --no-cache --virtual .build-deps build-base linux-headers

# install psycopg2 dependencies
RUN apk update \
    && apk add postgresql-dev gcc python3-dev musl-dev

# lint
RUN pip install --upgrade pip
COPY . .

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/backend/wheels -r requirements.txt

##############
# PRODUCTION #
##############

# pull official base image
FROM python:3.8.3-alpine

# create directory for the carrent user
RUN mkdir -p /home/carrent

# create the carrent user
RUN addgroup -S carrent && adduser -S carrent -G carrent

# create the appropriate directories
ENV HOME=/home/carrent
ENV APP_HOME=/home/carrent/backend
RUN mkdir $APP_HOME
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq
COPY --from=builder /usr/src/backend/wheels /wheels
COPY --from=builder /usr/src/backend/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint-prod.sh
COPY ./entrypoint.prod.sh $APP_HOME

# copy project
COPY . $APP_HOME

# chown all the files to the carrent user
RUN chown -R carrent:carrent $APP_HOME

# change to the carrent user
USER carrent

# run entrypoint.prod.sh
ENTRYPOINT ["/home/carrent/backend/entrypoint.prod.sh"]
Run Code Online (Sandbox Code Playgroud)

错误

ERRORS:
car.Car.main_image: (fields.E210) Cannot use ImageField because Pillow is not installed.
        HINT: Get Pillow at https://pypi.org/project/Pillow/ or run command "python -m pip install 
Pillow".
Run Code Online (Sandbox Code Playgroud)

roo*_*eki 3

您好,请将 Dockerfile 更改为:在第二台机器中添加此行

运行 apk 更新 && apk 添加 libpq jpeg-dev zlib-dev libjpeg

###########
# BUILDER #
###########

# pull official base image
FROM python:3.8.3-alpine as builder

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 and pillow dependencies
RUN apk update \
    && apk add postgresql-libs postgresql-dev libffi-dev \
       openldap-dev unixodbc-dev gcc musl-dev python3-dev \
       jpeg-dev zlib-dev libjpeg

# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt


#########
# FINAL #
#########

# pull official base image
FROM python:3.8.3-alpine

# create directory for the app user
RUN mkdir -p /home/app

# create the app user
RUN addgroup -S app && adduser -S app -G app

# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/damnplastic
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/static
RUN mkdir $APP_HOME/media
WORKDIR $APP_HOME

# install dependencies
RUN apk update && apk add libpq jpeg-dev zlib-dev libjpeg
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*

# copy entrypoint.sh
COPY ./entrypoint.sh $APP_HOME

# copy project
COPY . $APP_HOME

# chown all the files to the app user
RUN chown -R app:app $APP_HOME

# change to the app user
USER app

# run entrypoint.sh
ENTRYPOINT ["/home/app/damnplastic/entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)