This is a problem I encountered when deploying a Django app on Docker in a production environment. Django uses a library called Pillow when working with Image files, but this library is awkward and error-prone. Build using the following Dockerfile.
Dockerfile
###########
# BUILDER #
###########
# pull official base image
FROM python:3.7.3-alpine as builder
# set work directory
WORKDIR /usr/src/app
# set environment variables
#ENV PATH="/scripts:${PATH}"
ENV PYTHONUNBUFFERD 1
ENV PYTHONDONTWRITEBYTECODE 1
# install psycopg2 dependencies
RUN apk add --update --no-cache --virtual .tmp-build-deps postgresql-client jpeg-dev \
.tmp-build-deps gcc build-base python3-dev musl-dev linux-headers \
postgresql-dev
RUN apk add --update --no-cache --virtual .build-deps build-base python3-dev musl-dev \
libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev gettext \
postgresql-client gettext-dev libjpeg-turbo pcre
RUN apk add --update libxml2-dev libxslt-dev libffi-dev gcc libgcc openssl-dev curl
RUN apk add --update jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev libjpeg
RUN apk add --no-cache jpeg
# lint
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install flake8
RUN pip install pillow
COPY . .
RUN flake8 --ignore=E501,F401 .
# install dependencies
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
RUN apk del .tmp-build-deps
#########
# FINAL #
#########
# pull official base image
FROM python:3.7.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
COPY --from=builder /usr/local/lib/python3.7/site-packages /usr/local/lib/python3.7/site-packages
# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
#RUN mkdir $APP_HOME/staticfiles
#RUN mkdir $APP_HOME/mediafiles
WORKDIR $APP_HOME
# install dependencies
RUN apk update && apk add libpq
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --no-cache /wheels/*
#RUN pip install pillow
# copy entrypoint-prod.sh
COPY ./entrypoint.prod.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 chmod +x /home/app/web/entrypoint.prod.sh
# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]:
docker-compose.yml should look like this:
docker-compose.yml
version: '3.7'
services:
web:
build:
context: ./app
dockerfile: Dockerfile
command: gunicorn backend.wsgi:application --bind 0.0.0.0:8000
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=hello_django
- POSTGRES_PASSWORD=hello_django
- POSTGRES_DB=hello_django_dev
- POSTGRES_HOST_AUTH_METHOD=trust
nginx:
build: ./nginx
ports:
- 1337:80
depends_on:
- web
volumes:
postgres_data:
Write like this, docker-compose up -d build, docker-compose exec web python manage.py migrate and I get a "Cannot use ImageField because Pillow is not installed" error like the article title.
Follow the error statement and try docker-compose exec web python -m pip install Pillow.
Or rather, if you look at the library with docker-compose exec web python -m pip list, Pillow is installed successfully. Why......
When I check it, this error seems to occur frequently, such as pip install -m Pillow, put various dependencies such as jpeg-dev with apk in alpine, and change the version of python and pillow. I tried various things, but it seems that the alpine image cannot handle Pillow.
I changed the Docker image from python-x.x.x-alpine to python-x.x-buster and built it with various dependencies, and I was able to migrate successfully. The solution is simple. Pillow cannot be used with django on docker (alpine). that's all.