repair-agent c3a171163a
All checks were successful
Build and Deploy Backend / build-and-deploy (push) Successful in 50m38s
fix build bug
2026-03-03 14:13:02 +08:00

55 lines
1.6 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ============================================================
# hw-ws-service Dockerfile — 多阶段构建
# 构建阶段Go 编译(含 CGO for libopus
# 运行阶段Alpine + libopus + ffmpeg最终镜像 ~60-80MB
# ============================================================
# ---- 构建阶段 ----
FROM golang:1.23-alpine AS builder
# 安装 CGO 编译所需的 C 工具链和 libopus 开发头文件
RUN apk add --no-cache gcc musl-dev opus-dev opusfile-dev
WORKDIR /app
# 使用国内镜像加速,避免 proxy.golang.org 超时
ENV GOPROXY=https://goproxy.cn,direct
# 先拷贝 go.mod/go.sum利用 cache mount 持久化模块缓存
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
# 使用 cache mount 持久化模块缓存和编译缓存CI 重复构建大幅加速
# CGO_ENABLED=1 必须开启hraban/opus 是 CGO 库)
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=1 GOOS=linux \
go build \
-trimpath \
-ldflags="-s -w" \
-o hw-ws-service \
./cmd/main.go
# ---- 运行阶段 ----
FROM alpine:3.20
# 运行时依赖:
# opus — libopus 动态库hraban/opus CGO 绑定需要)
# ffmpeg — MP3/AAC 解码为 PCM
# ca-certificates — HTTPS 请求 OSS 需要根证书
RUN apk add --no-cache opus ffmpeg ca-certificates && \
# 创建非 root 运行用户(安全最佳实践)
addgroup -S hwws && adduser -S hwws -G hwws
COPY --from=builder /app/hw-ws-service /hw-ws-service
# 以非 root 用户运行
USER hwws
EXPOSE 8888
ENTRYPOINT ["/hw-ws-service"]