使用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