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

如何在CircleCI中跟踪Docker执行器各容器的内存使用情况?

问题

我通过GitHub在CircleCI上进行代码构建与测试,配置了如下Docker执行器:

// in .circleci/config.yml
executors:
  test_executor:
    docker:
      - image: circleci-base/java:corretto
      - image: postgres image
      - image: other image
      - image: other image

...
jobs:
  test:
    executor: test_executor
    steps:
      - run: // for build
      - run: // for test

当前流程运行正常,但需要跟踪所用各容器的内存使用情况。CircleCI测试结果的「RESOURCES」面板仅提供整体CPU和内存指标,无法查看单个容器的使用数据。

我尝试在步骤中添加查看Docker指标的命令,但Docker命令无法被识别:

- setup_remote_docker // not work whether set this setting or not.
  - run:
      name: docker stats
      command: |
        while true; do
          sleep 5
          docker stats --no-stream
          echo "======"
        done
      background: true

报错信息:

/bin/bash: line 2: docker: command not found Exited with code exit status 127

请问是否有方法可以跟踪运行中容器的内存使用情况?

解决方法

方法1:在执行器中安装Docker CLI

你使用的circleci-base/java:corretto镜像默认未预装Docker CLI,可在执行器初始化步骤中添加安装命令:

executors:
  test_executor:
    docker:
      - image: circleci-base/java:corretto
        steps:
          - run: sudo apt-get update && sudo apt-get install -y docker.io
      - image: postgres image
      - image: other image
      - image: other image

之后执行docker stats命令时需加上sudo,确保权限足够。

方法2:利用容器内原生监控工具

无需依赖Docker CLI,直接在目标服务容器内添加内存监控脚本,通过日志输出数据。以Postgres容器为例:

executors:
  test_executor:
    docker:
      - image: circleci-base/java:corretto
      - image: postgres:latest
        command: ["sh", "-c", "postgres & while true; do sleep 5; free -h; echo '======'; done"]
      - image: other image
      - image: other image

其他容器可类似配置,使用top -b -n 1、htop等工具输出更详细的资源占用数据。

方法3:配置远程Docker环境并挂载本地Socket

通过setup_remote_docker启用远程Docker环境,同时挂载本地Docker Socket来访问运行中的容器:

jobs:
  test:
    executor: test_executor
    steps:
      - setup_remote_docker:
          version: 20.10.14
      - run:
          name: 后台监控容器内存
          command: |
            docker run -d -v /var/run/docker.sock:/var/run/docker.sock busybox sh -c "while true; do sleep 5; docker stats --no-stream; echo '======'; done"
          background: true
      - run: // for build
      - run: // for test

注意:部分CircleCI环境可能限制Docker Socket的访问权限,需确认当前执行环境支持该操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 22:32:39