Docker容器部署Flask深度学习项目遇libGL.so.1缺失错误
解决Docker中Flask项目导入cv2时的libGL.so.1缺失错误
问题根源
官方Python镜像为精简体积,默认不包含OpenCV运行所需的系统级图形依赖库,libGL.so.1就是其中之一,导致pip安装的opencv-python无法正常加载。
修复后的Dockerfile
# Use an official Python runtime as a parent image FROM python:3.12.2 # Set the working directory in the container to /app WORKDIR /app # 安装OpenCV依赖的系统库 RUN apt-get update && apt-get install -y \ libgl1-mesa-glx \ libglib2.0-0 \ && rm -rf /var/lib/apt/lists/* # Install any needed packages specified in requirements.txt COPY requirements.txt . RUN pip install -r requirements.txt # Copy the current directory contents into the container at /app COPY . . # Make port 5000 available to the world outside this container EXPOSE 5000 # Run the command to start your application CMD cd process && flask run --host=0.0.0.0
关键改动说明
- 新增的
apt-get命令安装两个核心依赖:libgl1-mesa-glx:提供缺失的libGL.so.1库libglib2.0-0:OpenCV运行时依赖的常见系统库
rm -rf /var/lib/apt/lists/*:清理apt缓存,避免镜像体积不必要增大
验证步骤
- 重新构建镜像:
docker build -t flask-dl-app . - 启动容器:
docker run -p 5000:5000 flask-dl-app - 访问网页确认错误消失
内容的提问来源于stack exchange,提问作者Hamza
相关产品推荐
相关产品推荐

