DEBUG.WIKI
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
晚安云;香港 CN2 ¥15/月
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS

解决 Bitbucket Pipelines 报错 Container 'build' exceeded memory limit 内存超限修复

排查 Atlassian Bitbucket 运行自动化构建流水线时突然报错 Terminated with exit code 137: Container 'build' exceeded memory limit,分析 默认 4096MB 内存限制与 size: 2x 双倍内存配置。

问题现象

在 Bitbucket Pipelines 中运行打包脚本接近尾声时,步骤被云平台强杀退出:

Container 'build' exceeded memory limit of 4096 MB.
Build step failed with exit code 137.

触发根因

  1. Bitbucket Pipelines 默认分配给单个 Step 的容器总内存硬限制为 4096MB(4GB),包括主应用与所有声明的边车服务;
  2. 同时运行的 Docker 边车、数据库与编译器瞬间消耗总和突破了 4GB 物理上限;
  3. 未声明 size: 2x 选项向平台申请扩展内存。

解决步骤

在 bitbucket-pipelines.yml 中为该步骤开启 size: 2x 申请 8GB 内存配额

修改步骤定义:

pipelines:
  default:
    - step:
        name: Build and Test
        size: 2x # 将可用内存翻倍为 8192 MB
        image: node:18
        script:
          - npm ci
          - npm run build

合理分配边车服务的内存配额避免挤占主步骤空间

在 definitions.services 中为各边车设置明确上限:

definitions:
  services:
    postgres:
      image: postgres:15
      memory: 1024 # 仅分配 1GB 给数据库

优化构建工具的堆内存参数匹配容器

避免盲目过度申请。

验证确认

重新推送提交测试:

构建步骤在 8GB 充裕空间内平顺运行至 100%,再无超限强杀发生,流水线稳定完成交付。