Docker部署Go极限优化

使用upx对二进制文件进行压缩后发布。效果更佳!

  • 创建目录 mkdir helloworld
  • 初始化项目 cd helloworld && go mod init helloworld && vi main.go
  • main.go
package main

import (
    "fmt"
    "net/http"
    "time"
)

func greet(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello World! %s", time.Now())
}

func main() {
    http.HandleFunc("/", greet)
    http.ListenAndServe(":8080", nil)
}
  • Dockerfile 参考内容 社区云-社区文章详情 (devpress.cn)
# 基础镜像,基于golang的alpine镜像构建--编译阶段
FROM golang:alpine AS builder
# 作者
MAINTAINER korykim
# 全局工作目录
WORKDIR /go/myProject
# 把运行Dockerfile文件的当前目录所有文件复制到目标目录
COPY . /go/myProject
# 设置proxy环境变量,国内用户必选
ENV GOPROXY https://goproxy.cn,direct
# 安装UPX压缩壳
RUN echo "https://mirrors.aliyun.com/alpine/v3.8/main/" > /etc/apk/repositories \
    && echo "https://mirrors.aliyun.com/alpine/v3.8/community/" >> /etc/apk/repositories \
    && apk add --no-cache upx

# 编译
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" main.go

#对main二进制文件进行upx压缩后,卸载upx压缩壳程序
RUN upx -9 /go/myProject/main && apk del upx


# 使用alpine这个轻量级镜像为基础镜像--运行阶段
FROM alpine AS runner
# 全局工作目录
WORKDIR /go/myProject
# 复制编译阶段编译出来的运行文件到目标目录
COPY --from=builder /go/myProject/main .
# 将时区设置为东八区
RUN echo "https://mirrors.aliyun.com/alpine/v3.8/main/" > /etc/apk/repositories \
    && echo "https://mirrors.aliyun.com/alpine/v3.8/community/" >> /etc/apk/repositories \
    && apk add --no-cache tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo Asia/Shanghai > /etc/timezone \
    && apk del tzdata

# 需暴露的端口
EXPOSE 8080
# docker run命令触发的真实命令(相当于直接运行编译后的可运行文件)
ENTRYPOINT ["./main"]
  • 编译
docker build -t go/web:0.1 .
  • 镜像构建log
Sending build context to Docker daemon  5.632kB
Step 1/13 : FROM golang:alpine AS builder
 ---> d8bf44a3f6b4
Step 2/13 : MAINTAINER korykim
 ---> Running in 493bc8444c0a
Removing intermediate container 493bc8444c0a
 ---> ccb350a9243f
Step 3/13 : WORKDIR /go/myProject
 ---> Running in 928af1816920
Removing intermediate container 928af1816920
 ---> e6c7a88675f6
Step 4/13 : COPY . /go/myProject
 ---> 8b6782a41456
Step 5/13 : ENV GOPROXY https://goproxy.cn,direct
 ---> Running in eca25e073f24
Removing intermediate container eca25e073f24
 ---> 3f7f62cdfdde
Step 6/13 : RUN echo "https://mirrors.aliyun.com/alpine/v3.8/main/" > /etc/apk/repositories     && echo "https://mirrors.aliyun.com/alpine/v3.8/community/" >> /etc/apk/repositories     && apk add --no-cache upx
 ---> Running in f3b1c719e49a
fetch https://mirrors.aliyun.com/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/4) Installing libgcc (6.4.0-r9)
(2/4) Installing libstdc++ (6.4.0-r9)
(3/4) Installing ucl (1.03-r1)
(4/4) Installing upx (3.94-r0)
Executing busybox-1.34.1-r3.trigger
OK: 9 MiB in 19 packages
Removing intermediate container f3b1c719e49a
 ---> b4c5c85c17d4
Step 7/13 : RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" main.go && upx -9 /go/myProject/main && apk del upx
 ---> Running in 0045bcf8f6b8
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2017
UPX 3.94        Markus Oberhumer, Laszlo Molnar & John Reiser   May 12th 2017

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
   4280320 ->   1738612   40.62%   linux/amd64   main

Packed 1 file.
WARNING: Ignoring https://mirrors.aliyun.com/alpine/v3.8/main/: No such file or directory
WARNING: Ignoring https://mirrors.aliyun.com/alpine/v3.8/community/: No such file or directory
(1/4) Purging upx (3.94-r0)
(2/4) Purging libstdc++ (6.4.0-r9)
(3/4) Purging libgcc (6.4.0-r9)
(4/4) Purging ucl (1.03-r1)
Executing busybox-1.34.1-r3.trigger
OK: 6 MiB in 15 packages
Removing intermediate container 0045bcf8f6b8
 ---> 4e62746b89ab
Step 8/13 : FROM alpine AS runner
 ---> c059bfaa849c
Step 9/13 : WORKDIR /go/myProject
 ---> Running in e4085b8ac8c7
Removing intermediate container e4085b8ac8c7
 ---> eecc8098e168
Step 10/13 : COPY --from=builder /go/myProject/main .
 ---> ef6dcfc80ea4
Step 11/13 : RUN echo "https://mirrors.aliyun.com/alpine/v3.8/main/" > /etc/apk/repositories     && echo "https://mirrors.aliyun.com/alpine/v3.8/community/" >> /etc/apk/repositories     && apk add --no-cache tzdata     && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime     && echo Asia/Shanghai > /etc/timezone     && apk del tzdata
 ---> Running in faa8903f9281
fetch https://mirrors.aliyun.com/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2020a-r0)
Executing busybox-1.34.1-r3.trigger
OK: 9 MiB in 15 packages
WARNING: Ignoring https://mirrors.aliyun.com/alpine/v3.8/main/: No such file or directory
WARNING: Ignoring https://mirrors.aliyun.com/alpine/v3.8/community/: No such file or directory
(1/1) Purging tzdata (2020a-r0)
Executing busybox-1.34.1-r3.trigger
OK: 6 MiB in 14 packages
Removing intermediate container faa8903f9281
 ---> 15783988de8c
Step 12/13 : EXPOSE 8080
 ---> Running in 9efc520600c7
Removing intermediate container 9efc520600c7
 ---> 7167bf8453d3
Step 13/13 : ENTRYPOINT ["./main"]
 ---> Running in 2bbb9addf564
Removing intermediate container 2bbb9addf564
 ---> 215fdafdf48d
Successfully built 215fdafdf48d
Successfully tagged go/web:0.1
 
  • 最终docker镜像大小为 7.35MB

    docker image size

  • 运行

docker run --name go-web -it -d -p 8080:8080 go/web:0.1

你可能感兴趣的:(Docker部署Go极限优化)