使用docker-exporter遇BadRequest错误,容器指标异常求助
Docker Exporter指标采集异常排查与解决
环境配置
- Docker版本:Docker version 23.0.1, build a5ee5b1
- 运行系统:Ubuntu 20.04.5 LTS
- docker-exporter版本:prometheusnet/docker_exporter:2020031403160189d598
问题现象
docker_containers指标值始终为0:
# HELP docker_containers Number of containers that exist. # TYPE docker_containers gauge docker_containers 0
- docker-exporter日志持续抛出400错误:
jbsv@server1:/var/www/service/docker-exporter$ docker logs docker-exporter -f Info 2023-02-23 11:03:45Z [] DockerExporter v1.0.0-200225074229-54a46f7 Info 2023-02-23 11:03:45Z [] Configured to probe Docker on unix:///var/run/docker.sock Error 2023-02-23 11:03:49Z [] Docker API responded with status code=BadRequest, response=400 Bad Request (DockerApiException) Error 2023-02-23 11:03:54Z [] Docker API responded with status code=BadRequest, response=400 Bad Request (DockerApiException)
- 直接通过curl访问
/var/run/docker.sock正常返回Docker信息,确认Docker API服务正常:
jbsv@servers1:/var/www/service/docker-exporter$ curl --unix-socket /var/run/docker.sock -v http://localhost/info * Trying /var/run/docker.sock:0... * Connected to localhost (/run/docker.sock) port 80 (#0) > GET /info HTTP/1.1 > Host: localhost > User-Agent: curl/7.68.0 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Api-Version: 1.42 < Content-Type: application/json < Docker-Experimental: true < Ostype: linux < Server: Docker/23.0.1 (linux) < Date: Thu, 23 Feb 2023 16:14:23 GMT < Transfer-Encoding: chunked < {"ID":"4e3a8c8f-4780-4599-baf0-fca23bdf5c8e","Containers":26,"ContainersRunning":24,"ContainersPaused":0,"ContainersStopped":2,"Images":36,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","extfs"],["Supports d_type","true"],["Using metacopy","false"],["Native Overlay Diff","true"],["userxattr","false"]],"Plugins":{"Volume":["local"],"Network":["bridge","host","ipvlan","macvlan","null","overlay"],"Authorization":null,"Log":["awslogs","fluentd","gcplogs","gelf","journald","json-file","local","logentries","splunk","syslog"]},"MemoryLimit":true,"SwapLimit":false,"KernelMemoryTCP":true,"CpuCfsPeriod":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"PidsLimit":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIp6tables":true,"Debug":false,"NFd":147,"OomKillDisable":true,"NGoroutines":135,"SystemTime":"2023-02-23T23:14:23.53426525+07:00","LoggingDriver":"json-file","CgroupDriver":"cgroupfs","CgroupVersion":"1","NEventsListener":0,"KernelVersion":"5.4.0-139-generic","OperatingSystem":"Ubuntu 20.04.5 LTS","OSVersion":"20.04","OSType":"linux","Architecture":"x86_64","IndexServerAddress":"https://index.docker.io/v1/","RegistryConfig":{"AllowNondistributableArtifactsCIDRs":null,"AllowNondistributableArtifactsHostnames":null,"InsecureRegistryCIDRs":["127.0.0.0/8"],"IndexConfigs":{"docker.io":{"Name":"docker.io","Mirrors":[],"Secure":true,"Official":true}},"Mirrors":null},"NCPU":64,"MemTotal":134606831616,"GenericResources":null,"DockerRootDir":"/var/lib/docker","HttpProxy":"","HttpsProxy":"","NoProxy":"","Name":"trading1","Labels":[],"ExperimentalBuild":true,"ServerVersion":"23.0.1","Runtimes":{"io.containerd.runc.v2":{"path":"runc"},"runc":{"path":"runc"}},"DefaultRuntime":"runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"docker-init","ContainerdCommit":{"ID":"31aa4358a36870b21a992d3ad2bef29e1d693bec","Expected":"31aa4358a36870b21a992d3ad2bef29e1d693bec"},"RuncCommit":{"ID":"v1.1.4-0-g5fd4c4d","Expected":"v1.1.4-0-g5fd4c4d"},"InitCommit":{"ID":"de40ad0","Expected":"de40ad0"},"Securit
解决步骤
1. 修复版本兼容性问题
旧版prometheusnet/docker_exporter(2020年版本)与Docker 23.0.1的API版本不兼容,导致400错误。有两种处理方式:
- 替换为维护中的工具:改用
prom/node-exporter(自带Docker指标采集)或google/cadvisor(专注容器监控),这两个工具持续更新,完全兼容新版本Docker。 - 强制API版本适配:如果必须保留原exporter,启动容器时添加环境变量指定旧版Docker API:
docker run -d --name docker-exporter \ -v /var/run/docker.sock:/var/run/docker.sock \ -e DOCKER_API_VERSION=1.40 \ prometheusnet/docker_exporter:2020031403160189d598
2. 验证容器权限
确保docker-exporter容器对/var/run/docker.sock有足够权限,上述启动命令已包含正确的挂载映射,无需额外调整。
3. 确认指标恢复
重启docker-exporter后,查看日志是否不再出现400错误,然后访问指标端点(默认http://<容器IP>:9123/metrics),检查docker_containers、docker_containers_running等容器运行、重启相关指标是否正常返回。
内容的提问来源于stack exchange,提问作者MiH
相关产品推荐
相关产品推荐

