构建Python项目Dockerfile时psutil编译失败问题求助
解决Python项目Docker镜像构建时的依赖编译失败问题
问题场景
基于python:3.11-slim-buster镜像构建Python项目Docker镜像时,安装DataProfiler等依赖包出现编译错误,具体表现为psutil、python-snappy的wheel构建失败,提示gcc: No such file or directory。
原Dockerfile
FROM python:3.11-slim-buster # Update and install system packages RUN apt-get update -y && \ apt-get install --no-install-recommends -y -q \ git libpq-dev python-dev && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # Copy the requirements.txt file COPY requirements.txt . # Install Python dependencies using pip RUN python3.11 -m pip install --no-cache-dir --upgrade pip \ && python3.11 -m pip install --no-cache-dir -r requirements.txt EXPOSE 9700 WORKDIR /my_app
requirements.txt
snowflake-connector-python==3.0.2 DataProflier==0.8.8
关键错误信息
#0 10.85 error: command 'gcc' failed: No such file or directory #0 10.85 [end of output] #0 10.85 #0 10.85 note: This error originates from a subprocess, and is likely not a problem with pip. #0 10.85 ERROR: Failed building wheel for python-snappy #0 10.85 Running setup.py clean for python-snappy #0 11.06 Failed to build psutil python-snappy #0 11.06 ERROR: Could not build wheels for psutil, which is required to install pyproject.toml-based projects
原因
python:slim-buster是精简版镜像,默认未安装gcc等C语言编译工具,也缺少python-snappy依赖的系统库libsnappy。而psutil、python-snappy这类包需要编译C扩展才能安装,因此会出现编译失败的问题。
解决方法
修改Dockerfile,在系统依赖安装步骤中添加编译工具和必要的系统库:
修改后的Dockerfile
FROM python:3.11-slim-buster # Update and install system packages including build tools and libraries RUN apt-get update -y && \ apt-get install --no-install-recommends -y -q \ git libpq-dev python-dev build-essential libsnappy-dev && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # Copy the requirements.txt file COPY requirements.txt . # Install Python dependencies using pip RUN python3.11 -m pip install --no-cache-dir --upgrade pip \ && python3.11 -m pip install --no-cache-dir -r requirements.txt EXPOSE 9700 WORKDIR /my_app
关键修改说明
- 添加
build-essential:包含gcc、make等基础编译工具,解决找不到gcc的问题; - 添加
libsnappy-dev:提供python-snappy编译所需的系统库文件; - 保留
--no-install-recommends和清理步骤,避免镜像体积过度膨胀。
修改后重新执行docker build命令,即可完成依赖安装和镜像构建。
内容的提问来源于stack exchange,提问作者R0bert
相关产品推荐
相关产品推荐

