Dominik
Dominik

Reputation: 53

Docker: Cannot use ImageField because Pillow is not installed

I have a problem with my Docker. In my dev version of Docker it's working but when I've created prod version it says that Pillow is not installed. I've been searching for solution since yesterday, but none of given is working for me. Thanks in advance.

When I'm running flake8 there are a strange errors about some line breaks before binary operator and whitespaces in 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:

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"]

ERROR

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".

Upvotes: 5

Views: 940

Answers (2)

You just need to install jpeg-dev zlib-dev in production image

#install dependencies

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

Upvotes: 0

rmaleki
rmaleki

Reputation: 752

Hi please change Dockerfile to this: add this line in the second machine

RUN apk update && apk add 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"]

Upvotes: 3

Related Questions