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

部署Google Cloud Run容器应用时pycocotools编译失败求助

部署Google Cloud Run时pycocotools构建失败的解决思路

问题详情

将Docker容器化的Python应用部署到Google Cloud Run时,构建pycocotools的wheel失败,核心报错为:error: command 'gcc' failed: No such file or directory。

完整错误日志

Building wheels for collected packages: Secret-Manager, pycocotools, termcolor, treelib, coverage, xhtml2pdf, antlr4-python3-runtime, stringcase, future, svglib
Step #0:   Building wheel for Secret-Manager (setup.py): started
Step #0:   Building wheel for Secret-Manager (setup.py): finished with status 'done'
Step #0:   Created wheel for Secret-Manager: filename=Secret_Manager-1.0.0-py3-none-any.whl size=3207 sha256=a8fc3edbfbf7bc6c63a8b4743c4cdfcf4860e964082d11bf0ec07863ba840d6d
Step #0:   Stored in directory: /root/.cache/pip/wheels/6a/b1/83/6bb7443da6d0eddeb09108a425f97ae623048e1d579a65da35
Step #0:   Building wheel for pycocotools (pyproject.toml): started
Step #0:   Building wheel for pycocotools (pyproject.toml): finished with status 'error'
Step #0:   error: subprocess-exited-with-error
Step #0:   
Step #0:   × Building wheel for pycocotools (pyproject.toml) did not run successfully.
Step #0:   │ exit code: 1
Step #0:   ╰─> [21 lines of output]
Step #0:       running bdist_wheel
Step #0:       running build
Step #0:       running build_py
Step #0:       creating build
Step #0:       creating build/lib.linux-x86_64-cpython-310
Step #0:       creating build/lib.linux-x86_64-cpython-310/pycocotools
Step #0:       copying pycocotools/coco.py -> build/lib.linux-x86_64-cpython-310/pycocotools
Step #0:       copying pycocotools/cocoeval.py -> build/lib.linux-x86_64-cpython-310/pycocotools
Step #0:       copying pycocotools/mask.py -> build/lib.linux-x86_64-cpython-310/pycocotools
Step #0:       copying pycocotools/__init__.py -> build/lib.linux-x86_64-cpython-310/pycocotools
Step #0:       running build_ext
Step #0:       /tmp/pip-build-env-orf1v9v9/overlay/lib/python3.10/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /tmp/pip-install-yap6d6s2/pycocotools_ccc10746b8464ffc97ac525b5fe81013/pycocotools/_mask.pyx
Step #0:         tree = Parsing.p_module(s, pxd, full_module_name)
Step #0:       Compiling pycocotools/_mask.pyx because it changed.
Step #0:       [1/1] Cythonizing pycocotools/_mask.pyx
Step #0:       building 'pycocotools._mask' extension
Step #0:       creating build/temp.linux-x86_64-cpython-310
Step #0:       creating build/temp.linux-x86_64-cpython-310/common
Step #0:       creating build/temp.linux-x86_64-cpython-310/pycocotools
Step #0:       gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/tmp/pip-build-env-orf1v9v9/overlay/lib/python3.10/site-packages/numpy/core/include -I./common -I/usr/local/include/python3.10 -c ./common/maskApi.c -o build/temp.linux-x86_64-cpython-310/./common/maskApi.o -Wno-cpp -Wno-unused-function -std=c99
Step #0:       error: command 'gcc' failed: No such file or directory
Step #0:       [end of output]
Step #0:   
Step #0:   note: This error originates from a subprocess, and is likely not a problem with pip.
Step #0:   ERROR: Failed building wheel for pycocotools

当前使用的Dockerfile

# This is an official Python runtime, used as the parent image
FROM python:3.8.10-slim

# set the working directory in the container to /app
WORKDIR /app

# add the current directory to the container as /app
ADD . /app

# execute everyone's favorite pip command, pip install -r
RUN pip3 install -r requirements.txt

# execute the Flask app
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

当前使用的requirements.txt

boto3==1.17.15
botocore==1.20.15
ultralytics==8.0.207
super-gradients==3.3.1
umpy==1.23.0
matplotlib==3.5.3
opencv-python==4.6.0.66
scipy==1.10.0
markupsafe==2.0.1
gunicorn==19.9.0
google-cloud-firestore==2.5.2
google-cloud-core==2.3.2
google-cloud-pubsub
mlflow==2.8.0
google.cloud.storage==1.41.1
requests==2.28.2

解决思路

1. 安装编译依赖

Python slim镜像为精简体积默认不含gcc等编译工具,而pycocotools需要编译C扩展才能安装。修改Dockerfile,在pip install前添加编译工具的安装步骤:

# 安装编译依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

添加后完整Dockerfile:

# This is an official Python runtime, used as the parent image
FROM python:3.8.10-slim

# set the working directory in the container to /app
WORKDIR /app

# 安装编译依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# add the current directory to the container as /app
ADD . /app

# execute everyone's favorite pip command, pip install -r
RUN pip3 install -r requirements.txt

# execute the Flask app
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app

2. 修正requirements.txt拼写错误

requirements.txt中的umpy==1.23.0是拼写错误,需改为numpy==1.23.0,否则会导致numpy安装失败,间接影响pycocotools的编译(pycocotools依赖numpy头文件)。

3. 可选:使用预编译包加速构建

若不想在镜像中安装编译工具,可尝试使用预编译的pycocotools包。不过由于ultralytics会自动依赖pycocotools,优先通过安装编译工具解决更稳定。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 01:57:44