Docker已启动容器安装Pillow失效,Django项目报错求助
Docker部署Django应用时ImageField依赖Pillow的问题解决
问题背景
用Docker构建Django应用,容器已创建完成后给模型添加了ImageField,执行docker-compose run web python3 manage.py makemigrations时触发报错,提示需安装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".
已尝试的无效操作
- 执行
docker-compose run web python3 -m pip install Pillow,安装日志显示成功,但重启容器后报错依旧:
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
- 通过
docker exec -it <容器ID/名称> bash进入容器安装Pillow,pip list显示已安装,但容器重启后问题重复出现。 - 尝试通过Dockerfile重新构建镜像,但未解决问题。
当前使用的配置文件
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/
docker-compose.yml
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
解决方案
问题根源
- 直接在容器内安装Pillow是临时操作,容器重启后所有修改会丢失——容器基于镜像运行,镜像未更新的话,每次重启都是全新实例。
- Dockerfile构建无效的原因是未将Pillow添加到requirements.txt,导致构建镜像时不会自动安装该依赖。
- Dockerfile中使用了
apk命令(Alpine系统包管理器),但基础镜像python:3基于Debian,Debian使用apt-get而非apk,这会导致依赖安装失败。
修复步骤
- 在本地项目的
requirements.txt中添加一行:Pillow(或指定版本如Pillow==10.1.0) - 修改Dockerfile中的依赖安装命令,适配Debian系统:
FROM python:3 ENV PYTHONUNBUFFERED=1 WORKDIR /code COPY requirements.txt /code/ RUN pip install --upgrade pip # 替换apk为apt-get,安装Pillow编译依赖 RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ libjpeg-dev \ zlib1g-dev \ && rm -rf /var/lib/apt/lists/* RUN pip install --no-cache-dir -r requirements.txt COPY . /code/
- 重新构建镜像:
docker-compose build web - 重启容器:
docker-compose up
内容的提问来源于stack exchange,提问作者General Suslik
相关产品推荐
相关产品推荐

