Docker构建Flask应用时psutil编译失败问题求助
Solutions for Docker Build Failure When Installing Python Requirements (psutil, cffi, etc.)
1. Install System-Level Build Dependencies in Your Dockerfile
The packages failing to build (psutil, cffi, etc.) require compilers and system libraries to build from source. Add these dependencies before running pip install:
For Debian/Ubuntu-based base images (e.g., python:3.x-slim)
RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ python3-dev \ libffi-dev \ libssl-dev \ && rm -rf /var/lib/apt/lists/* RUN pip install -r requirements.txt
For Alpine-based base images
RUN apk add --no-cache gcc musl-dev python3-dev libffi-dev openssl-dev RUN pip install -r requirements.txt
2. Switch to the Full Python Base Image
The default python:3.x image includes pre-installed build tools, which can eliminate the need for manual dependency installation. Update your base image line:
FROM python:3.11 # Replace with your project's Python version
3. Fix Environment Variable Setup on Windows
Windows uses different syntax for setting environment variables:
- Command Prompt:
set DOCKER_BUILDKIT=0 set COMPOSE_DOCKER_CLI_BUILD=0
- PowerShell:
$env:DOCKER_BUILDKIT = "0" $env:COMPOSE_DOCKER_CLI_BUILD = "0"
Run these commands before executing docker build.
4. Pin Package Versions or Use Pre-Built Wheels
Newer package versions may have build compatibility issues. Try pinning to stable, known-working versions in requirements.txt:
psutil==5.9.5 cffi==1.15.1 Pillow==10.0.1 kiwisolver==1.4.5
Alternatively, force pip to use pre-built wheels instead of building from source:
RUN pip install --only-binary :all: -r requirements.txt
内容的提问来源于stack exchange,提问作者Helenk
相关产品推荐
相关产品推荐

