在Docker中通过Pipfile.lock安装含Django Channels及Pillow时出错求助
Got it, let's break down what's causing your installation failures and fix this step by step. There are a few key issues here related to version compatibility and missing system dependencies:
1. Version Incompatibility with Python 3.7
First off, the latest versions of Django Channels (3.0+) require Python 3.8 or higher, but you're using Python 3.7-alpine. When you try to install channels="*", pip will pull a version that doesn't work with your Python setup, leading to errors.
Fix: Pin Channels & Channels-Redis to Compatible Versions
Update your Pipfile to specify Channels 2.x (which supports Python 3.7) and a matching channels-redis version:
[packages] django = "*" "psycopg2-binary" = "*" djangorestframework = "*" djangorestframework-jwt = "*" djoser = "*" graphene-django = "*" django-cors-headers = "*" django-graphql-social-auth = "*" django-money = "*" django-mptt = "*" pillow = "*" channels = "~=2.4" # Channels 2.x is compatible with Python 3.7 channels-redis = "~=2.4" # Matches Channels 2.x version
2. Missing System Dependencies for Pillow
Pillow requires specific system libraries to compile and run. Your current Dockerfile only installs jpeg-dev (a compile-time dependency) but doesn't include the runtime library needed to use Pillow after the build dependencies are deleted. You're also missing libraries for other image formats like PNG and FreeType.
3. Missing Compile Dependencies for Channels-Redis
Channels-redis relies on the hiredis library, which needs the hiredis-dev system package to compile correctly on Alpine.
Fix: Update Dockerfile to Include Required Dependencies
Modify your Dockerfile to add these missing dependencies. Here's the updated version with comments explaining the changes:
FROM python:3.7-alpine ENV PYTHONUNBUFFERED 1 ENV PYTHONDONTWRITEBYTECODE 1 WORKDIR /code # Keep your existing repo setup (note: Alpine 3.9 is quite old; consider upgrading to 3.12+ for better compatibility) RUN echo "https://mirror.csclub.uwaterloo.ca/alpine/v3.9/main" > /etc/apk/repositories RUN echo "https://mirror.csclub.uwaterloo.ca/alpine/v3.9/community" >>/etc/apk/repositories RUN apk update # Install runtime dependencies (stay after build deletions) RUN apk add --update --no-cache \ postgresql-client \ libjpeg-turbo # Runtime library for Pillow (replaces jpeg-dev after build) # Install temporary build dependencies (deleted after pip installs) RUN apk add --update --no-cache --virtual .tmp-build-deps \ gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev \ jpeg-dev libpng-dev freetype-dev # Extra dev libs for full Pillow support \ hiredis-dev # Required to compile channels-redis's hiredis dependency \ && pip3 install psycopg2-binary \ && apk del .tmp-build-deps RUN pip3 install --upgrade pip RUN pip3 install pipenv COPY Pipfile* /code/ RUN pipenv install --system --deploy --ignore-pipfile COPY ./entrypoint.sh /code/entrypoint.sh COPY . /code/ ENTRYPOINT ["/code/entrypoint.sh"]
Key Changes Explained:
- Runtime Pillow Dependency: Added
libjpeg-turboso Pillow can access the JPEG library after we delete the temporaryjpeg-devpackage. - Full Pillow Compile Support: Added
libpng-devandfreetype-devto ensure Pillow can compile support for PNG and font-related features. - Channels-Redis Compile Support: Added
hiredis-devto let pip compile the hiredis backend for channels-redis. - Optional: Upgrade Alpine Version: Alpine 3.9 is end-of-life, so consider switching to
v3.12(the last version with full Python 3.7 support) in your repo URLs. This will give you newer, more stable system packages and fewer compatibility headaches.
Verify the Fix
After updating the files, rebuild your Docker image and test it:
docker build -t your-project-image . docker run -it your-project-image python -c "import PIL; from channels import Channel; print('All dependencies installed successfully!')"
If the command prints the success message, you're good to go.
内容的提问来源于stack exchange,提问作者milan

