skip to Main Content

I am building a django app with docker. After i`ve already built my container, I’ve added an ImageField to one of my models. When I ran docker-compose run web(my service name) python3 manage.py makemigrations, it gave me an error, that I have to install Pillow:

web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
web_1  | Exception in thread django-main-thread:
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.12/threading.py", line 1052, in _bootstrap_inner
web_1  |     self.run()
web_1  |   File "/usr/local/lib/python3.12/threading.py", line 989, in run
web_1  |     self._target(*self._args, **self._kwargs)
web_1  |   File "/usr/local/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 133, in inner_run
web_1  |     self.check(display_num_errors=True)
web_1  |   File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 556, in check
web_1  |     raise SystemCheckError(msg)
web_1  | django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
web_1  | 
web_1  | ERRORS:
web_1  | system.Task.image: (fields.E210) Cannot use ImageField because Pillow is not installed.
web_1  |    HINT: Get Pillow at https://pypi.org/project/Pillow/ or run command "python -m pip install Pillow".

After that I tried to install with docker-compose run web python3 -m pip install Pillow, it seemed, like it installed it:

Starting db ... done
Creating app_web_run ... done
Collecting Pillow
  Obtaining dependency information for Pillow from https://files.pythonhosted.org/packages/44/ed/a6f7dcd6631ec55b8d26c6a8bca762b04b7025daa3aa67e860a886abed89/Pillow-10.1.0-cp312-cp312-manylinux_2_28_x86_64.whl.metadata
  Downloading Pillow-10.1.0-cp312-cp312-manylinux_2_28_x86_64.whl.metadata (9.5 kB)
Downloading Pillow-10.1.0-cp312-cp312-manylinux_2_28_x86_64.whl (3.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.6/3.6 MB 287.0 kB/s eta 0:00:00
Installing collected packages: Pillow
Successfully installed Pillow-10.1.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

But when I ran docker-compose up, it was still giving me the same error.

After some googling, I found that i can go to docker console or smth like docker exec -it <container_id/name/etc> bash so I did that and installed Pillow, and after running pip list in it, it showed me, that Pillow is installed, but when i came back to docker-compose up, I was still getting the same error. I tried to add Dockerfile to build section of docker-compose but it didn’t work either. So I’m stack here((

My Dockerfile:

FROM python:3

ENV PYTHONUNBUFFERED=1

WORKDIR /code
COPY requirements.txt /code/

RUN pip install --upgrade pip
RUN apk --update add 
    build-base 
    jpeg-dev 
    zlib-dev
RUN pip install --no-cache-dir -r requirements.txt
COPY . /code/

My docker-compose.yml file:

version: "3.11"

services:
  db:
    container_name: db
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"

    depends_on:
      - db

2

Answers


  1. To deal with images django relies on pillow. Without pillow you run into an error.

    At the same place where you define that you want to place django inside of your environment you have to state that you want to place pillow alongside of it.

    I assume this is in requirements.txt.

    requirements.txt

    django==<versionnumber>
    moduleX
    moduleY
    Pillow
    

    The problem you are facing when you add it to a running container is, that it is then fixed for that specific container. If you use docker-compose up again the container gets rebuild without pillow! That’s why you would want to put it into your Dockerfile or requirements.txt

    Login or Signup to reply.
  2. Wouldn’t it be possible for you to add Pillow to your dependencies since you need it anyways ?

    The problem occurs because you’re giving a context in your docker-compose.yml, therefore docker-compose will always run a container that matches the context. And since you’re not installing Pillow in the Dockerfile of the given context, it will never work when running docker-compose up.

    But since you need Pillow, just add it to your requirements.txt and it will fix the issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search