Docker Hub自动构建失败:[Errno 28] 设备空间不足
问题描述
正在构建基于YOLOv7的目标检测Docker镜像,本地构建正常,但Docker Hub自动构建失败,日志显示磁盘空间不足错误:
ERROR: Could not install packages due to an EnvironmentError: [Errno 28] No space left on device: '/tmp/pip-unpacked-wheel-r3egz0yp/torch/lib/libtorch_cuda_cpp.so' -> '/usr/local/lib/python3.8/dist-packages/torch/lib/libtorch_cuda_cpp.so'
完整构建日志如下:
2022-12-14T08:38:58Z #14 [ 8/10] RUN pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113 2022-12-14T08:38:59Z #14 1.429 Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu113 2022-12-14T08:38:59Z #14 1.701 Collecting torch==1.11.0+cu113 2022-12-14T08:38:59Z #14 1.707 Downloading https://download.pytorch.org/whl/cu113/torch-1.11.0%2Bcu113-cp38-cp38-linux_x86_64.whl (1637.0 MB) 2022-12-14T08:40:08Z #14 70.63 Collecting torchvision==0.12.0+cu113 2022-12-14T08:40:08Z #14 70.65 Downloading https://download.pytorch.org/whl/cu113/torchvision-0.12.0%2Bcu113-cp38-cp38-linux_x86_64.whl (22.3 MB) 2022-12-14T08:40:10Z #14 72.18 Collecting torchaudio==0.11.0 2022-12-14T08:40:10Z #14 72.19 Downloading https://download.pytorch.org/whl/cu113/torchaudio-0.11.0%2Bcu113-cp38-cp38-linux_x86_64.whl (2.9 MB) 2022-12-14T08:40:10Z #14 72.38 Requirement already satisfied: typing-extensions in /usr/local/lib/python3.8/dist-packages (from torch==1.11.0+cu113) (3.10.0.2) 2022-12-14T08:40:10Z #14 72.39 Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from torchvision==0.12.0+cu113) (1.21.2) 2022-12-14T08:40:10Z #14 72.40 Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (from torchvision==0.12.0+cu113) (2.26.0) 2022-12-14T08:40:10Z #14 72.42 Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.8/dist-packages (from torchvision==0.12.0+cu113) (9.2.0) 2022-12-14T08:40:10Z #14 72.44 Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.8/dist-packages (from requests->torchvision==0.12.0+cu113) (1.26.7) 2022-12-14T08:40:10Z #14 72.46 Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/dist-packages (from requests->torchvision==0.12.0+cu113) (2021.10.8) 2022-12-14T08:40:10Z #14 72.46 Requirement already satisfied: charset-normalizer~=2.0.0; python_version >= "3" in /usr/local/lib/python3.8/dist-packages (from requests->torchvision==0.12.0+cu113) (2.0.7) 2022-12-14T08:40:10Z #14 72.47 Requirement already satisfied: idna<4,>=2.5; python_version >= "3" in /usr/local/lib/python3.8/dist-packages (from requests->torchvision==0.12.0+cu113) (3.3) 2022-12-14T08:40:12Z #14 73.91 Installing collected packages: torch, torchvision, torchaudio 2022-12-14T08:40:12Z #14 73.91 Attempting uninstall: torch 2022-12-14T08:40:12Z #14 73.91 Found existing installation: torch 1.13.0 2022-12-14T08:40:18Z #14 79.92 Uninstalling torch-1.13.0: 2022-12-14T08:40:50Z #14 112.3 Successfully uninstalled torch-1.13.0 2022-12-14T08:41:35Z #14 157.6 Rolling back uninstall of torch 2022-12-14T08:41:35Z #14 157.6 Moving to /usr/local/bin/convert-caffe2-to-onnx 2022-12-14T08:41:35Z #14 157.6 from /tmp/pip-uninstall-21vj9ta9/convert-caffe2-to-onnx 2022-12-14T08:41:35Z #14 157.6 Moving to /usr/local/bin/convert-onnx-to-caffe2 2022-12-14T08:41:35Z #14 157.6 from /tmp/pip-uninstall-21vj9ta9/convert-onnx-to-caffe2 2022-12-14T08:41:35Z #14 157.6 Moving to /usr/local/bin/torchrun 2022-12-14T08:41:35Z #14 157.6 from /tmp/pip-uninstall-21vj9ta9/torchrun 2022-12-14T08:41:35Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/functorch/ 2022-12-14T08:41:35Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~unctorch 2022-12-14T08:41:35Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/torch-1.13.0.dist-info/ 2022-12-14T08:41:35Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~orch-1.13.0.dist-info 2022-12-14T08:41:35Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/torch/ 2022-12-14T08:41:35Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~orch 2022-12-14T08:41:35Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/torchgen/ 2022-12-14T08:41:35Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~orchgen 2022-12-14T08:41:36Z #14 158.1 ERROR: Could not install packages due to an EnvironmentError: [Errno 28] No space left on device: '/tmp/pip-unpacked-wheel-bkn_wblz/torch/lib/libtorch_cuda_cpp.so' -> '/usr/local/lib/python3.8/dist-packages/torch/lib/libtorch_cuda_cpp.so' 2022-12-14T08:41:36Z #14 158.1 2022-12-14T08:41:36Z #14 ERROR: executor failed running [/bin/sh -c pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113]: exit code: 1 2022-12-14T08:41:36Z ------ 2022-12-14T08:41:36Z > [ 8/10] RUN pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113: 2022-12-14T08:41:36Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/functorch/ 2022-12-14T08:41:36Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~unctorch 2022-12-14T08:41:36Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/torch-1.13.0.dist-info/ 2022-12-14T08:41:36Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~orch-1.13.0.dist-info 2022-12-14T08:41:36Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/torch/ 2022-12-14T08:41:36Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~orch 2022-12-14T08:41:36Z #14 157.6 Moving to /usr/local/lib/python3.8/dist-packages/torchgen/ 2022-12-14T08:41:36Z #14 157.6 from /usr/local/lib/python3.8/dist-packages/~orchgen 2022-12-14T08:41:36Z #14 158.1 ERROR: Could not install packages due to an EnvironmentError: [Errno 28] No space left on device: '/tmp/pip-unpacked-wheel-bkn_wblz/torch/lib/libtorch_cuda_cpp.so' -> '/usr/local/lib/python3.8/dist-packages/torch/lib/libtorch_cuda_cpp.so' 2022-12-14T08:41:36Z #14 158.1 2022-12-14T08:41:36Z ------ 2022-12-14T08:41:36Z ERROR: failed to solve: executor failed running [/bin/sh -c pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113]: exit code: 1 2022-12-14T08:41:36Z Build failed using Buildkit (1)
解决建议
1. 替换基础镜像,避免重复安装大体积依赖
日志显示基础镜像自带torch 1.13.0,你又要安装torch 1.11.0,导致先卸载旧版本再安装新版本,两次占用磁盘空间(旧版本+下载的新版本wheel包)。直接选择不带预安装torch的轻量基础镜像,比如python:3.8-slim,或者指定对应版本的PyTorch官方基础镜像,减少磁盘占用。
2. 调整pip安装参数,减少临时文件占用
- 安装时添加
--no-cache-dir参数,禁止pip缓存wheel包,节省/tmp目录空间:pip install --no-cache-dir torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113 - 若容器内有其他空间更大的目录,可手动指定pip临时目录:
TMPDIR=/var/tmp pip install ...
3. 清理构建过程中的冗余文件
在Dockerfile的每个RUN命令后及时清理无用文件:
- 若使用apt安装依赖,清理apt缓存:
rm -rf /var/lib/apt/lists/* - 合并多个RUN命令,减少镜像层数的同时,避免中间层残留文件占用空间。
4. 使用多阶段构建
将依赖安装和代码复制分离,第一阶段安装所有依赖,第二阶段只复制必要文件到轻量镜像,大幅降低镜像体积,缓解构建空间压力。示例结构:
# 第一阶段:安装依赖 FROM python:3.8 as builder RUN pip install --no-cache-dir torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113 # 第二阶段:构建最终镜像 FROM python:3.8-slim COPY --from=builder /usr/local/lib/python3.8/dist-packages /usr/local/lib/python3.8/dist-packages COPY --from=builder /usr/local/bin /usr/local/bin # 复制YOLOv7代码等文件 COPY . /app WORKDIR /app
5. 选择更轻量化的PyTorch版本
如果不需要CUDA加速,安装CPU版本的PyTorch,体积会大幅减小:
pip install --no-cache-dir torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0
若必须使用CUDA,可选择更小体积的CUDA版本,或使用NVIDIA提供的轻量CUDA基础镜像。
内容的提问来源于stack exchange,提问作者GreenGameCodi
相关产品推荐
相关产品推荐

