“这个端口被谁占了?” 每个开发者都问过这个问题,然后对着 lsof 的输出一脸懵。
问题背景 本地开发时,端口冲突是家常便饭:
1 2 3 4 5 6 7 lsof -iTCP:3000 -sTCP:LISTEN netstat -tlnp | grep 3000
痛点很明显:
场景
传统做法
问题
端口被占用
lsof + grep
输出冗长,看不出是 Docker 还是本地进程
查看 Docker 端口
docker ps + 手动映射
容器端口和宿主机端口对不上
批量管理服务
逐个 kill
低效,容易误杀
等待服务启动
轮询 curl
写脚本麻烦,逻辑不统一
Sonar 就是为了解决这些问题而生的。
Sonar 是什么 Sonar 是一个用 Go 写的 CLI 工具,专门用来查看和管理 localhost 上监听的端口。
关键数字
GitHub Stars: 1,000+
语言: Go (88.9%) + Swift (7.5%)
许可证: MIT
支持平台: macOS、Linux、Windows
安装方式: Homebrew / 脚本 / Go
核心定位 1 2 3 4 5 Sonar 让你一眼看清本地端口在跑什么。 - 列出所有监听端口 - 显示 Docker 容器名、镜像、映射端口 - 支持 kill 进程、查看日志、shell 进入容器 - 等待端口就绪(脚本友好)
核心特性 1. 端口一目了然 1 2 3 4 5 6 7 8 9 $ sonar list PORT PROCESS CONTAINER IMAGE CPORT URL 1780 proxy (traefik:3.0) my-app-proxy-1 traefik:3.0 80 http://localhost:1780 3000 next-server (v16.1.6) http://localhost:3000 5432 db (postgres:17) my-app-db-1 postgres:17 5432 http://localhost:5432 6873 frontend (frontend:latest) my-app-frontend-1 frontend:latest 5173 http://localhost:6873 9700 backend (backend:latest) my-app-backend-1 backend:latest 8000 http://localhost:9700 5 ports (4 docker, 1 user)
不用再猜了:哪个端口、什么进程、是不是 Docker、映射到哪——全都有。
2. Docker 深度集成 Sonar 不是简单地调用 docker ps,它会:
显示容器名、镜像名、Compose 项目
区分 Docker 端口和用户进程
支持 docker stop 优雅停止容器
查看容器日志(docker logs -f)
3. 资源监控
输出 CPU、内存、运行时间、状态等信息,类似 docker stats 但更全面。
4. 脚本友好的等待机制 1 2 3 4 5 6 7 8 9 sonar wait 5432 3000 --timeout 60s sonar wait 3000 --http=/health docker compose up -d sonar wait 5432 3000 --timeout 60s && npm run migrate && npm run test
这在 CI/CD 或本地脚本里特别有用。
快速开始 安装 1 2 3 4 5 6 7 8 9 10 11 brew install raskrebs/sonar/sonar curl -sfL https://raw.githubusercontent.com/raskrebs/sonar/main/scripts/install.sh | bash irm https://raw.githubusercontent.com/raskrebs/sonar/main/scripts/install.ps1 | iex go install github.com/raskrebs/sonar@latest
Shell 补全 1 2 3 4 5 6 7 8 sonar completion zsh > "${fpath[1]} /_sonar" sonar completion bash > /etc/bash_completion.d/sonar sonar completion fish | source
常用命令 查看端口 1 2 3 4 5 6 sonar list sonar list --stats sonar list --filter docker sonar list --sort name sonar list --json sonar list -a
查看端口详情
显示完整信息:命令行、用户、绑定地址、资源使用、Docker 详情等。
杀进程 1 2 3 4 sonar kill 3000 sonar kill 3000 -f sonar kill-all --filter docker sonar kill-all --project my-app
Docker 容器会用 docker stop 优雅停止,不是直接杀进程。
查看日志
Docker 容器:调用 docker logs -f
本地进程:通过 lsof 找到日志文件并 tail
macOS 回退:log stream
Linux 回退:/proc/<pid>/fd
进入容器 1 2 sonar attach 3000 sonar attach 3000 --shell bash
实时监控 1 2 3 4 sonar watch sonar watch --stats sonar watch -i 500ms sonar watch --notify
端口映射
把 6873 端口的流量代理到 3002,方便调试。
找空闲端口 1 2 3 4 sonar next sonar next 8000 sonar next 3000-3100 sonar next -n 3
依赖关系图
显示哪些服务在互相通信(比如 backend 连接 postgres)。
实用场景 场景 1: 快速定位端口冲突
场景 2: Docker Compose 项目管理 1 2 3 4 5 6 7 8 sonar list --filter docker sonar kill-all --project my-app sonar logs 3000
场景 3: CI/CD 等待服务就绪 1 2 3 4 5 6 7 8 9 #!/bin/bash docker compose up -d sonar wait 5432 3000 --timeout 60s --http=/health npm run migrate npm run test
场景 4: 开发环境快速切换 1 2 3 4 5 6 7 8 sonar profile create my-app sonar up my-app sonar down my-app
配置文件 Sonar 支持配置文件,位置在 ~/.config/sonar/config.yaml:
1 2 3 4 5 6 7 8 sonar config init sonar config path sonar config edit
示例配置:
1 2 3 4 5 6 7 8 9 list: columns: [port , process , container , image , containerport , url ] sort: port filter: "" all: false color: true services: 9000: php-fpm 5050: my-dashboard
macOS 菜单栏应用 Sonar 还有一个 macOS 菜单栏应用,实时显示端口状态:
通过 Homebrew 安装时会自动包含 sonar-tray。
对比矩阵
工具
Docker 支持
资源监控
脚本集成
易用性
sonar
✅
✅
✅
⭐⭐⭐⭐⭐
lsof
❌
❌
⚠️
⭐⭐
netstat
❌
❌
⚠️
⭐⭐
ss
❌
❌
⚠️
⭐⭐⭐
docker ps
✅
✅
⚠️
⭐⭐⭐
总结 Sonar 解决的核心问题:让本地端口管理变得直观 。
维度
价值
可视化
一眼看清所有端口,不用拼凑命令
Docker 集成
容器名、镜像、端口映射一目了然
脚本友好
wait 命令替代手写轮询逻辑
操作便捷
kill、logs、attach 一个命令搞定
推荐使用 :
1 2 3 4 本地开发 → sonar list(快速查看端口) Docker 项目 → sonar list --filter docker(容器管理) CI/CD → sonar wait(等待服务就绪) 日常监控 → sonar watch(实时观察)
延伸阅读
下次端口被占了,别再对着 lsof 发呆了。装个 sonar,一眼看清。
Comments