31 lines
777 B
Docker
31 lines
777 B
Docker
# 使用官方Golang最新版本作为构建环境
|
|
FROM golang:latest AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# 复制项目文件
|
|
COPY . .
|
|
|
|
# 构建应用
|
|
RUN apt update && apt install -y xz-utils && CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main .
|
|
|
|
# upx压缩
|
|
RUN wget -c $(curl -s https://api.github.com/repos/upx/upx/releases/latest | grep browser_download_url | grep linux |grep amd64| cut -d'"' -f4) -O - |tar --strip-components 1 -xvJf - && ./upx ./main
|
|
|
|
# 使用轻量级的Alpine镜像作为运行时环境
|
|
FROM alpine:latest
|
|
|
|
# 安装必要的工具
|
|
RUN apk --no-cache add ca-certificates
|
|
|
|
# 设置工作目录
|
|
WORKDIR /root/
|
|
|
|
# 从构建阶段复制可执行文件
|
|
COPY --from=builder /app/main .
|
|
|
|
# 暴露端口
|
|
EXPOSE 80
|
|
|
|
# 设置容器启动命令
|
|
CMD ["./main"] |