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

如何让Docker容器内的Prometheus访问Mac本地Spring Boot应用?

解决Docker中Prometheus访问Mac宿主机Spring Boot应用的问题

方法1:使用Mac Docker专属宿主机地址

Docker for Mac提供了固定内部地址host.docker.internal,可直接替代配置里的localhost:

  1. 修改Prometheus的prometheus.yml配置文件:
scrape_configs:
  - job_name: 'spring-boot-app'
    static_configs:
      - targets: ['host.docker.internal:8085']
  1. 重启Prometheus容器(命令启动的先停容器再重新运行;用docker-compose则执行docker-compose restart prometheus)

方法2:使用宿主机实际IP地址

先找Mac的本地私有IP(终端执行ifconfig | grep inet,取类似192.168.x.x的地址),再替换到Prometheus配置中:

scrape_configs:
  - job_name: 'spring-boot-app'
    static_configs:
      - targets: ['192.168.1.100:8085'] # 替换为你的Mac实际IP

注意:若Mac IP为动态分配,需定期更新该配置。

方法3:使用Docker host网络模式(Docker for Mac 18.03+可用)

启动Prometheus容器时添加--net=host参数,容器会直接复用宿主机网络栈,此时容器内的localhost等价于宿主机的localhost:

docker run -d --net=host prom/prometheus

此方式无需单独映射9090端口,Prometheus会直接监听宿主机的9090端口。

验证配置有效性

访问宿主机http://localhost:9090/targets,查看spring-boot-app任务状态是否为UP,是则配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 10:12:06