File size: 896 Bytes
34fee23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 使用多阶段构建
FROM golang:1.18-alpine AS builder

# 设置工作目录
WORKDIR /app

# 复制go.mod和go.sum文件
COPY go.mod go.sum ./

# 下载依赖
RUN go mod download

# 复制源代码
COPY . .

# 编译应用程序
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o nextconnect .

# 使用轻量级基础镜像
FROM alpine:latest

# 安装必要的包
RUN apk --no-cache add ca-certificates

# 创建非root用户
RUN addgroup -g 1000 nextconnect && adduser -D -s /bin/sh -u 1000 -G nextconnect nextconnect

# 设置工作目录
WORKDIR /root/

# 从builder阶段复制二进制文件
COPY --from=builder /app/nextconnect .

# 复制配置文件
COPY --from=builder /app/config.ini .

# 更改文件权限
RUN chown nextconnect:nextconnect nextconnect

# 切换到非root用户
USER nextconnect

# 暴露端口
EXPOSE 7000

# 设置默认命令
CMD ["./nextconnect"]