DigitalOcean Droplet中Docker镜像安装detectron2失败求助
本地构建Docker镜像安装detectron2完全正常,但在2GB内存/2核Intel CPU、60GB磁盘的Ubuntu 22.10 x64 DigitalOcean Droplet上构建时,出现ERROR: Failed building wheel for detectron2错误,日志核心信息为:
gcc: fatal error: Killed signal terminated program cc1plus
compilation terminated.
error: command '/usr/bin/gcc' failed with exit code 1
补充情况:使用--no-cache参数重建镜像时detectron2未被安装,此前无此问题;第二个未指定detectron2版本的Dockerfile在本地也无法完成安装。
测试的两个Dockerfile
Dockerfile 1(指定v0.4版本)
FROM python:3.10-slim-buster # Update package lists RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 gcc g++ git build-essential libpoppler-cpp-dev pkg-config poppler-utils tesseract-ocr libtesseract-dev -y # Make working directories RUN mkdir -p /app WORKDIR /app # Copy the requirements.txt file to the container COPY requirements.txt . # Install dependencies RUN pip install --upgrade pip RUN pip install torch torchvision torchaudio RUN pip install -r requirements.txt RUN pip install 'git+https://github.com/facebookresearch/detectron2.git@v0.4#egg=detectron2' # Copy the .env file to the container COPY .env . # Copy every file in the source folder to the created working directory COPY . . # Expose the port that the application will run on EXPOSE 8080 # Start the application CMD ["python3.10", "uvicorn", "-m", "main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8080"]
Dockerfile 2(未指定版本)
FROM python:3.10-slim-buster # Update package lists RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 gcc g++ git build-essential libpoppler-cpp-dev pkg-config poppler-utils tesseract-ocr libtesseract-dev -y # Make working directories RUN mkdir -p /app WORKDIR /app # Copy the requirements.txt file to the container COPY requirements.txt . # Install dependencies RUN pip install --upgrade pip RUN pip install torch torchvision torchaudio RUN pip install -r requirements.txt RUN pip install 'git+https://github.com/facebookresearch/detectron2.git' # Copy the .env file to the container COPY .env . # Copy every file in the source folder to the created working directory COPY . . # Expose the port that the application will run on EXPOSE 8080 # Start the application CMD ["python3.10", "-m", "uvicorn", "main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8080"]
解决方案
1. 给Droplet添加Swap分区(核心解决内存不足)
编译detectron2需要大量内存,2GB内存无法支撑,添加Swap作为虚拟内存补充:
# 在Droplet上执行,创建4GB Swap文件 fallocate -l 4G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile # 设置开机自动挂载Swap echo '/swapfile none swap sw 0 0' >> /etc/fstab
构建完成后若不需要Swap,可执行以下命令删除:
swapoff /swapfile rm /swapfile sed -i '/swapfile/d' /etc/fstab
2. 使用预编译轮子跳过源码编译
直接安装对应环境的预编译detectron2包,避免在容器内编译:
修改Dockerfile中安装detectron2的命令,替换为匹配你torch版本的预编译包地址(以torch 1.13+、CUDA 11.7为例):
RUN pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu117/torch1.13/index.html
注意:需根据实际安装的torch、CUDA版本选择对应轮子地址,版本不匹配会导致安装失败
3. 限制编译核心数降低内存占用
编译时默认使用全部CPU核心,会加剧内存消耗,限制核心数减少压力:
在安装detectron2前设置环境变量:
ENV MAX_JOBS=1 RUN pip install 'git+https://github.com/facebookresearch/detectron2.git@v0.4#egg=detectron2'
4. 优化Dockerfile编译效率
在apt-get install命令中添加ninja-build,避免编译时fallback到慢的distutils后端,加快编译速度:
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 gcc g++ git build-essential ninja-build libpoppler-cpp-dev pkg-config poppler-utils tesseract-ocr libtesseract-dev -y
内容的提问来源于stack exchange,提问作者neil_ruaro

