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

Ubuntu Server Snap版Docker配置Prometheus采集Docker指标失败求助

Ubuntu Server Snap版Docker配置Prometheus采集Docker指标失败求助

各位大佬好,我最近在折腾Ubuntu Server上用Snap安装的Docker,想让容器里的Prometheus采集Docker daemon的指标,但试了各种方法都连不上,实在没辙了,来求助大家。

我的环境信息

服务器系统

Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04
Codename:       jammy

Docker(Snap安装)运行状态

执行sudo snap services得到结果:

Service                          Startup  Current   Notes
docker.dockerd                   enabled  active    -
docker.nvidia-container-toolkit  enabled  inactive  -
lxd.activate                     enabled  inactive  -
lxd.daemon                       enabled  inactive  socket-activated
lxd.user-daemon                  enabled  inactive  socket-activated

Snap版本详情

snap    2.61.1
snapd   2.61.1
series  16
ubuntu  22.04
kernel  5.15.0-91-generic

Prometheus的docker-compose配置

version: '3.8'
volumes:
  prometheus_data: {}
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: unless-stopped
    volumes:
      - /home/lukas/data/config/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "9090:9090/tcp"

Docker网桥docker0的IP信息

执行ip addr show docker0输出:

8: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:18:5c:09:7f brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:18ff:fe5c:97f/64 scope link
valid_lft forever preferred_lft forever

问题经过与尝试的方法

我想让容器里的Prometheus采集Docker daemon的指标,按照配置指引,需要在Docker的配置文件里添加指标监听地址。一开始我以为是在/etc/docker/daemon.json里加:

{
  "metrics-addr": "127.0.0.1:9323"
}

后来发现连不上,又改成了172.17.0.1:9323,结果还是不行。

之后查资料才知道,Snap安装的Docker配置文件路径不是默认的,而是/var/snap/docker/current/etc/docker/daemon.json,于是我把配置文件放到了这个路径,然后执行sudo snap restart docker重启了Docker服务。

但重启之后,不管是在宿主机上执行:

curl http://localhost:9323/metrics
curl http://172.17.0.1:9323/metrics

都返回curl: (7) Failed to connect to xxx port 9323 after 0 ms: Connection refused,完全连不上这个端口。

我还尝试把Prometheus容器改成host网络模式,修改后的docker-compose.yml如下:

version: '3.8'
volumes:
  prometheus_data: {}
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: unless-stopped
    volumes:
      - /home/lukas/data/config/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    extra_hosts:
      - "host.docker.internal:host-gateway"
    network_mode: "host"
    ports:
      - "9090:9090/tcp"

但问题依旧,还是无法连接到9323端口获取Docker指标。

补充信息

Docker系统信息(执行docker system info):

Client:
Context:    default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.10.4)
compose: Docker Compose (Docker Inc., v2.17.2)
Server:
Containers: 20
Running: 20
Paused: 0
Stopped: 0
Images: 20
Server Version: 20.10.24
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 2806fc1057397dbaeefbea0e4e17bddfbd388f38
runc version:
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-91-generic
Operating System: Ubuntu Core 22
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 31.26GiB
Name: blackholeport
ID: AMKQ:XQSW:OPFY:ZLWE:VCVI:BKBB:NVO4:5WYJ:W6AZ:JB7M:7T4S:WXNC
Docker Root Dir: /var/snap/docker/common/var-lib-docker
Debug Mode: false
Username: hagbart
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

当前Docker网络列表(执行docker network ls):

NETWORK ID     NAME                    DRIVER    SCOPE
f124d5db3e0b   bridge                  bridge    local
862535ef9371   deluge_default          bridge    local
86bf6736570f   foundryvtt_default      bridge    local
909b8162cdbe   grafana_default         bridge    local
299ce7db89b5   heimdall_default        bridge    local
92a81645a176   host                    host      local
a5e580aa1810   microbin_default        bridge    local
da8d9cb1d3e7   nginx-pm_default        bridge    local
055308848eba   none                    null      local
93afb48bbba2   o-portainer             bridge    local
460a87dcb670   o-security              bridge    local
ea16af5d1414   pihole_default          bridge    local
250440a093d7   portainer_default       bridge    local
971e8873d462   prometheus_default      bridge    local
dcbfb120b9b2   prometheus_monitoring   bridge    local
a482e951879d   prowlarr_default        bridge    local
9ab6f6734815   radarr_default          bridge    local
f6abd6de38d3   sonarr_default          bridge    local
0e062c280f80   spigotmc_default        bridge    local
640505be1145   teamspeak_default       bridge    local

现在实在搞不清哪里出问题了,有没有大佬能帮忙分析一下,指点我怎么解决这个问题?

备注:内容来源于stack exchange,提问作者Hagbart Celine

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 10:24:35