|NO.Z.00114|——————————|^^ 製作 ^^|——|KuberNetes&Docker操作.V12|----------------------------------------------------|docker.v02|優化小映象|
阿新 • • 發佈:2022-03-29
[CloudNative:KuberNetes&二進位制升級.V12] [Applications.KuberNetes] [|DevOps|docker|映象製作|Alpine|busybox|scratch|Debian|Glibc/node:slim|python/slim net|]
一、怎麼去優化小映象
二、下載映象版本### --- 使用多階段構建,使用多個FROM引數 ~~~ 使用多階段構建:就是多個FROM ~~~ 編譯操作和生成最終映象的操作分開
### --- 使用go語言去做映象
[root@k8s-master01 dockerfiles]# docker pull golang:1.14.4-alpine
三、配置go引數
### --- 協議個go語言的引數
[root@k8s-master01 dockerfiles]# vim main.go
package main
import "fmt"
func main() {
fmt.Println("hello.golang")
}
四、單階段構建—基於alpine:建立dockerfile
五、單階段構建—基於alpine:構建映象### --- 配置dockerfile [root@k8s-master01 dockerfiles]# vim Dockerfile FROM golang:1.14.4-alpine WORKDIR /opt COPY main.go /opt RUN go build /opt/main.go CMD "./main"
六、單階段構建—基於alpine:執行構建的映象### --- 構建映象 [root@k8s-master01 dockerfiles]# docker build -t hello:single_build . Sending build context to Docker daemon 6.656kB Step 1/5 : FROM golang:1.14.4-alpine ---> 3289bf11c284 Step 2/5 : WORKDIR /opt ---> Running in 2e54278dd489 Removing intermediate container 2e54278dd489 ---> 14621984bf34 Step 3/5 : COPY main.go /opt ---> 72a6ef2fd24e Step 4/5 : RUN go build /opt/main.go ---> Running in 27f1de553f68 Removing intermediate container 27f1de553f68 ---> 7764459737f9 Step 5/5 : CMD "./hello" ---> Running in b02772a2178c Removing intermediate container b02772a2178c ---> f0bac85c72b5 Successfully built f0bac85c72b5 Successfully tagged hello:single_build
### --- 進入容器執行映象
[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:single_build sh
/opt # ls
main main.go // 可以檢視到當前目錄下已經生成了main檔案
[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:single_build
hello.golang // 可以檢視到它是可以執行main裡面的命令的
七、單階段構建—基於alpine:檢視製作的映象大小
### --- 檢視它的映象包大小
~~~ 可以看到生成的這個包是372M,包是非常大的。
[root@k8s-master01 dockerfiles]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello single_build ba14d9831a6a About a minute ago 372MB
### --- 檢視它的二進位制包
~~~ 在生產環境下是不可以這樣的。二進位制包只有2M,而映象包有372M,是不可以這樣部署的。
[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:single_build sh
/opt # ls -lh
total 2M
-rwxr-xr-x 1 root root 2.0M Apr 14 07:14 main // 而它的二進位制包只有2M
八、多階段構建—基於alpine:優化小映象:在生產環境下,優化映象大小
### --- 在生產環境下,我們只需要main的二進位制包就可以執行我們go的這個環境,設定以下方案
[root@k8s-master01 dockerfiles]# vim Dockerfile
#build step
FROM golang:1.14.4-alpine
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
#create real app image
FROM alpine:3.8
COPY --from=0 /opt/main /
CMD "./opt/main"
九、多階段構建—基於alpine:製作小映象:構建映象
### --- 執行構建映象
[root@k8s-master01 dockerfiles]# docker build -t hello:alpine .
Step 6/8 : FROM alpine:3.8
---> c8bccc0af957
Step 7/8 : COPY --from=0 /opt/main /
---> c49e54a6dbb2
Step 8/8 : CMD "./opt/main"
---> Running in c07bd08cbcb0
Removing intermediate container c07bd08cbcb0
---> 2ec9b3e54af1
Successfully built 2ec9b3e54af1
Successfully tagged hello:alpine
十、多階段構建—基於alpine:優化小映象:執行構建的映象### --- 執行容器
[root@k8s-master01 dockerfiles]# docker run -ti --rm hello:alpine sh
/ # ls
main // 可以看到main已經拷貝過來了
/ # ./main
hello.golang
### --- 檢視一下映象大小
~~~ 實現了同樣的結果,單階段和分階段構建檔案大小相差200多MB,
~~~ 所以建議採用分階段構建比較合理;特別是go語言或者編譯操作
[root@k8s-master01 dockerfiles]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello alpine 3b4a7c8ca51c 55 seconds ago 6.48MB // 分階段構建
hello single_build ba14d9831a6a 9 minutes ago 372MB // 單階段構建
十一、多階段構建—基於alpine:優化小映象:編輯dockerfile配置引數
### --- 修改dockerfile配置引數
[root@k8s-master01 dockerfiles]# vim Dockerfile
#build step
FROM golang:1.14.4-alpine as builder # 把這個結果做成一個映象,拷貝到下面的/根目錄下
WORKDIR /opt
COPY main.go /opt
RUN go build /opt/main.go
CMD "./main"
#create real app image
FROM alpine:3.8
COPY --from=builder /opt/main / # 將上面製作的映象拷貝到/目錄下
CMD "./opt/main"
十二、多階段構建—基於alpine:製作小映象:執行容器
### --- 執行容器
[root@k8s-master01 dockerfiles]# docker build -t hello:alpine .
===============================END===============================
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart ——W.S.Landor
來自為知筆記(Wiz)