You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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缓存,避免镜像体积不必要增大

验证步骤

  1. 重新构建镜像:docker build -t flask-dl-app .
  2. 启动容器:docker run -p 5000:5000 flask-dl-app
  3. 访问网页确认错误消失

内容的提问来源于stack exchange,提问作者Hamza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 23:22:03