修复 Docker 构建 + 端口可配置

- Dockerfile 设置 GOPROXY=https://goproxy.cn,direct 解决国内网络问题
- 新增 addr 配置项,支持自定义端口,默认 :8080
- docker-compose 端口改为 ${OPS_MCP_PORT:-9090}
- 移除 EXPOSE 指令(端口通过 compose 管理)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
yangzhaohan
2026-07-06 10:47:31 +08:00
parent 093511bbdd
commit 843024d7da
6 changed files with 13 additions and 8 deletions
+1 -1
View File
@@ -1 +1 @@
OPS_MCP_TRANSPORT=sse
OPS_MCP_PORT=9090
+2 -1
View File
@@ -1,5 +1,7 @@
FROM golang:1.24-alpine AS builder
ENV GOPROXY=https://goproxy.cn,direct
WORKDIR /app
COPY go.mod go.sum ./
@@ -15,5 +17,4 @@ RUN apk add --no-cache docker-cli ca-certificates
COPY --from=builder /server /server
COPY config/ /config/
EXPOSE 8080
ENTRYPOINT ["/server"]
+2 -2
View File
@@ -17,10 +17,10 @@ func main() {
cfg, err := config.Load()
if err != nil {
slog.Warn("config load failed, using defaults", "err", err)
cfg = &config.Config{Transport: "stdio"}
cfg = &config.Config{Transport: "stdio", Addr: ":8080"}
}
if err := server.Run(cfg.Transport, tool.NewDockerTool()); err != nil {
if err := server.Run(cfg.Transport, cfg.Addr, tool.NewDockerTool()); err != nil {
slog.Error("fatal", "err", err)
os.Exit(1)
}
+2 -1
View File
@@ -8,6 +8,7 @@ import (
type Config struct {
Transport string `mapstructure:"transport"` // stdio | sse
Addr string `mapstructure:"addr"` // 监听地址,默认 :8080
}
func Load() (*Config, error) {
@@ -20,8 +21,8 @@ func Load() (*Config, error) {
v.SetEnvPrefix("OPS_MCP")
v.AutomaticEnv()
v.SetDefault("transport", "stdio")
v.SetDefault("addr", ":8080")
// 配置文件不存在不算错误,直接用默认值 + 环境变量
if err := v.ReadInConfig(); err != nil {
slog.Warn("no config file found, using defaults and env vars", "err", err)
}
+3
View File
@@ -6,7 +6,10 @@ services:
image: ops-mcp:latest
container_name: ops-mcp
restart: unless-stopped
ports:
- "${OPS_MCP_PORT:-9090}:${OPS_MCP_PORT:-9090}"
environment:
- OPS_MCP_TRANSPORT=sse
- OPS_MCP_ADDR=:${OPS_MCP_PORT:-9090}
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
+3 -3
View File
@@ -16,7 +16,7 @@ import (
const version = "0.1.0"
// Run 启动 MCP Server。
func Run(transport string, tools ...tool.Tool) error {
func Run(transport, addr string, tools ...tool.Tool) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -60,8 +60,8 @@ func Run(transport string, tools ...tool.Tool) error {
slog.Info("starting MCP server", "transport", "stdio")
return server.ServeStdio(mcpServer)
case "sse":
slog.Info("starting MCP server", "transport", "sse", "addr", ":8080")
return server.NewSSEServer(mcpServer).Start(":8080")
slog.Info("starting MCP server", "transport", "sse", "addr", addr)
return server.NewSSEServer(mcpServer).Start(addr)
default:
return fmt.Errorf("unknown transport: %s", transport)
}