fix: agent install.sh 404 + 三阶段构建编译agent二进制
问题: 构建上下文 ./backend 无法访问 ../agent, 导致容器内
/app/agent/deploy/install.sh 不存在, 返回 404
修复:
- Dockerfile 改为三阶段构建 (backend go1.21 + agent go1.24 交叉编译 + alpine runtime)
- docker-compose backend build context 改为项目根
- routes.go /agent/arch 安装命令改为动态获取 URL
- 新增 .dockerignore
- agent_install.go: SSH 远程安装 handler
- AgentWizard.tsx: 复制按钮 fallback + 动态 server URL
- ServerTableRow.tsx: agent 状态判断
验证: curl|bash 安装成功, agent 上报数据到 MySQL ✅
This commit is contained in:
1 parent
d0344c2f52
commit
d0b94d39c0
9 files changed
+281
-38
No files matched your search
+51
-29
@@ -1,60 +1,82 @@
|
||||
# 构建阶段
|
||||
# ============================================================
|
||||
# AIOps Backend + Agent 多阶段 Dockerfile
|
||||
# Stage 1: 编译后端 (Go 1.21)
|
||||
# Stage 2: 交叉编译 Agent (Go 1.24, linux-amd64/arm64 + darwin-arm64)
|
||||
# Stage 3: 运行时 (Alpine)
|
||||
# 署名 Logan.Li
|
||||
# ============================================================
|
||||
|
||||
# ==================== Stage 1: Backend Builder ====================
|
||||
FROM golang:1.21-alpine AS builder
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 安装构建依赖
|
||||
RUN apk add --no-cache git ca-certificates tzdata
|
||||
|
||||
# 设置时区
|
||||
ENV TZ=Asia/Shanghai
|
||||
|
||||
# 设置 Go 模块代理(国内加速)
|
||||
ENV GOPROXY=https://goproxy.cn,direct
|
||||
ENV GOSUMDB=off
|
||||
|
||||
# 复制 go.mod 和 go.sum
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# 下载依赖
|
||||
# 复制后端 go.mod(利用 Docker 层缓存)
|
||||
COPY backend/go.mod backend/go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# 复制源代码
|
||||
COPY . .
|
||||
# 复制后端源码
|
||||
COPY backend/ ./
|
||||
|
||||
# 构建应用
|
||||
# 构建后端二进制
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
|
||||
-ldflags="-w -s \
|
||||
-X main.Version=$(git describe --tags --always --dirty 2>/dev/null || echo 'dev') \
|
||||
-X main.BuildTime=$(date +%Y-%m-%d_%H:%M:%S) \
|
||||
-X main.GitCommit=$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')" \
|
||||
-ldflags="-w -s" \
|
||||
-o ezops ./cmd/
|
||||
|
||||
# 运行阶段
|
||||
# ==================== Stage 2: Agent Builder ====================
|
||||
FROM golang:1.24-alpine AS agent-builder
|
||||
|
||||
WORKDIR /agent
|
||||
|
||||
ENV GOPROXY=https://goproxy.cn,direct
|
||||
ENV GOSUMDB=off
|
||||
|
||||
# 复制 agent go.mod
|
||||
COPY agent/go.mod agent/go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# 复制 agent 源码
|
||||
COPY agent/ ./
|
||||
|
||||
# 交叉编译三个平台的 agent 二进制
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/aiops-agent-linux-amd64 . && \
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o bin/aiops-agent-linux-arm64 . && \
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o bin/aiops-agent-darwin-arm64 .
|
||||
|
||||
# ==================== Stage 3: Runtime ====================
|
||||
FROM alpine:latest
|
||||
|
||||
# 安装运行时依赖
|
||||
RUN apk --no-cache add ca-certificates tzdata curl
|
||||
# 安装运行时依赖(curl=健康检查,openssh-client=SSH远程安装agent)
|
||||
RUN apk --no-cache add ca-certificates tzdata curl openssh-client
|
||||
|
||||
# 设置时区
|
||||
ENV TZ=Asia/Shanghai
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制构建产物
|
||||
# 复制后端二进制
|
||||
COPY --from=builder /app/ezops .
|
||||
|
||||
# 创建日志目录
|
||||
RUN mkdir -p /app/logs /app/uploads
|
||||
# 创建运行时目录
|
||||
RUN mkdir -p /app/logs /app/uploads /app/agent/bin /app/agent/deploy
|
||||
|
||||
# 复制 agent 二进制(三个平台)
|
||||
COPY --from=agent-builder /agent/bin/aiops-agent-linux-amd64 /app/agent/bin/
|
||||
COPY --from=agent-builder /agent/bin/aiops-agent-linux-arm64 /app/agent/bin/
|
||||
COPY --from=agent-builder /agent/bin/aiops-agent-darwin-arm64 /app/agent/bin/
|
||||
|
||||
# 复制 agent 安装脚本和 systemd service 模板
|
||||
COPY agent/deploy/install.sh /app/agent/deploy/
|
||||
COPY agent/deploy/aiops-agent.service /app/agent/deploy/
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 8080
|
||||
|
||||
# 健康检查
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
|
||||
CMD curl -f http://localhost:8080/health || exit 1
|
||||
|
||||
# 启动应用
|
||||
CMD ["./ezops"]
|
||||
Reference in new issue
Block a user