All checks were successful
Build and Deploy Backend / build-and-deploy (push) Successful in 50m38s
55 lines
1.6 KiB
Docker
55 lines
1.6 KiB
Docker
# ============================================================
|
||
# 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"]
|