成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Windows Containers 大冒險: 優(yōu)化計劃(Dockerfile)

Enlightenment / 2939人閱讀

摘要:最終疊加在一起成為新的鏡像。優(yōu)化的方法就是使用和換行符將多條語句合并成一條執(zhí)行。第一點,所有指令在中需要大寫,從而和具體的操作命令區(qū)分開來。第二點,對于過長的指令請合理使用換行符,從而增加的可閱讀性。

前言

有一定旅行經(jīng)驗的朋友都知道,即使在出發(fā)前制定了詳細(xì)的出行計劃,也會在路途中因為各式各樣的狀況而不得不重新修改計劃。這個狀況在我們編寫Dockerfile時一樣存在?;?0分鐘編寫的Dockerfile在構(gòu)建鏡像的時候也會出現(xiàn)各式各樣的狀況。那么編寫Dockerfile時有哪些可以優(yōu)化的點呢?

在了解可優(yōu)化點之前,先來了解下容器的分層文件系統(tǒng)。

分層文件系統(tǒng)

分層文件系統(tǒng)是容器技術(shù)中的重要概念,當(dāng)通過Dockerfile構(gòu)建鏡像時,每一個可執(zhí)行指令會被容器引擎一一執(zhí)行,并形成臨時的容器。最終疊加在一起成為新的鏡像。這里有個名詞,叫可執(zhí)行指令,最常用的就是RUN指令,因為RUN指令會在構(gòu)建時會形成新的鏡像層。其它創(chuàng)建文件層的指令還有ADD和COPY。

對于以下的Dockerfile,可以通過 docker history 來查看分層文件系統(tǒng)的結(jié)構(gòu)。各位需要注意的是docker history的輸出結(jié)果和Dockerfile中指令的先后順序。

#escape=`

FROM microsoft/windowsservercore:1803

SHELL [ "powershell", "-command" ]

RUN $ErrorActionPreference = "Stop"; `
    $ProgressPreference = "SilentlyContinue"; `
    $null = New-Item -Path c:apps -Type Directory

RUN Invoke-WebRequest -Uri https://download.sysinternals.com/files/SysinternalsSuite.zip `
                      -UseBasicParsing -OutFile c:appsSysinternalsSuite.zip `
                      -Proxy http://192.168.0.124:1080; `
    Expand-Archive -Path C:appsSysinternalsSuite.zip -DestinationPath C:appssysinternals -Force; `
    Remove-Item -Path c:appsSysinternalsSuite.zip

ENTRYPOINT [ "powershell" ]
$ docker history greggu/sysinternals:20170516
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
64b20b828374        2 weeks ago         powershell -command #(nop)  ENTRYPOINT ["pow…   41kB
5b6b75a8ed6c        2 weeks ago         powershell -command Invoke-WebRequest -Uri h…   94.2MB
8cdde7fb6229        2 weeks ago         powershell -command $ErrorActionPreference =…   39MB
214857b207fb        2 weeks ago         powershell -command #(nop)  SHELL [powershel…   41kB
ad6116672030        6 weeks ago         Install update 10.0.14393.2189                  2.79GB
           17 months ago       Apply image 10.0.14393.0                        7.68GB
優(yōu)化Dockerfile 對相關(guān)指令采取分組合并

首先針對以下Dockerfile構(gòu)建鏡像,請在構(gòu)建過程中觀察輸出,也可以使用docker history查看。

FROM microsoft/windowsservercore:1803

SHELL [ "powershell", "-command" ]

RUN $ErrorActionPreference = "Stop"
RUN $ProgressPreference = "SilentlyContinue"
RUN $null = New-Item -Path c:apps -Type Directory
$ docker history greggu/test:0.0.1
IMAGE               CREATED              CREATED BY                                      SIZE                COMMENT
158b934a3b80        About a minute ago   powershell -command $null = New-Item -Path c…   32.7MB
e3b45ff79cc9        2 minutes ago        powershell -command $ProgressPreference = "S…   32.7MB
a61529eb0225        2 minutes ago        powershell -command $ErrorActionPreference =…   39.1MB
214857b207fb        2 weeks ago          powershell -command #(nop)  SHELL [powershel…   41kB
ad6116672030        6 weeks ago          Install update 10.0.14393.2189                  2.79GB
           17 months ago        Apply image 10.0.14393.0                        7.68GB

可以看到每一句RUN指令都新生成了一層鏡像層,導(dǎo)致鏡像大小變大。優(yōu)化的方法就是使用;和`(換行符)將多條PowerShell語句合并成一條執(zhí)行。也就是在本文最開始使用的例子。

移除不再需要的文件

還是本文最開始使用的例子,在這個例子中,首先從網(wǎng)絡(luò)下載SysinternalsSuite.zip壓縮包,隨后執(zhí)行了解壓操作。最后執(zhí)行Remove-Item操作把壓縮包刪除。而這個刪除操作減小了鏡像體積。這點大家很容易理解,不再需要的文件刪除即可。當(dāng)然這里還有一個小點,為了讓Dockerfile能在任意位置被使用,一般推薦通過網(wǎng)絡(luò)下載所依賴的文件。所以Invoke-WebRequest的常用參數(shù)也是需要掌握的。

合理使用分層文件系統(tǒng)

在之前的例子中,各位可以看到每一行RUN指令都生成了新的一層鏡像,從而增加最終鏡像的大小。那么是不是意味這把所有指令都寫在一行內(nèi)就完成了最佳優(yōu)化實踐呢?這個理解是不對的。為了在構(gòu)建鏡像時使用到Docker的緩存功能,需要對構(gòu)建指令進(jìn)行分組。請各位準(zhǔn)備以下兩個Dockerfile并執(zhí)行構(gòu)建操作,并特別留意構(gòu)建第二個鏡像時第3步的輸出。

# escape=`

FROM microsoft/windowsservercore:1803

SHELL [ "powershell", "-command" ]

RUN $ErrorActionPreference = "Stop"; `
    $ProgressPreference = "SilentlyContinue"; `
    $null = New-Item -Path c:demos -Type Directory

RUN $null = New-Item -Path c:demosdemo01 -Type Directory
# escape=`

FROM microsoft/windowsservercore:1803

SHELL [ "powershell", "-command" ]

RUN $ErrorActionPreference = "Stop"; `
    $ProgressPreference = "SilentlyContinue"; `
    $null = New-Item -Path c:demos -Type Directory

RUN $null = New-Item -Path c:demosdemo02 -Type Directory

因為使用了兩個Dockerfile,需要在docker build時指定具體的Dockerfile,具體構(gòu)建命令可以放在一個批處理文件中

docker build -f Dockerfile.1 --rm --tag greggu/demo01:0.0.1 .
docker build -f Dockerfile.2 --rm --tag greggu/demo02:0.0.1 .

構(gòu)建日志

PS C:Usersgreggu
eposdow-playgroundwindowsdemo02> .uild.ps1
Sending build context to Docker daemon  4.096kB
Step 1/4 : FROM microsoft/windowsservercore:1803
 ---> 7e2287b03e2e
Step 2/4 : SHELL [ "powershell", "-command" ]
 ---> Running in cb5a6e633a04
Removing intermediate container cb5a6e633a04
 ---> 8fc2f8b81b5b
Step 3/4 : RUN $ErrorActionPreference = "Stop";     $ProgressPreference = "SilentlyContinue";     $null = New-Item -Path c:demos -Type Directory
 ---> Running in dce5d9150f26
Removing intermediate container dce5d9150f26
 ---> 922a8037b585
Step 4/4 : RUN $null = New-Item -Path c:demosdemo01 -Type Directory
 ---> Running in 69d0d0277786
Removing intermediate container 69d0d0277786
 ---> 57bde82f4a6f
Successfully built 57bde82f4a6f
Successfully tagged greggu/demo01:0.0.1
Sending build context to Docker daemon  4.096kB
Step 1/4 : FROM microsoft/windowsservercore:1803
 ---> 7e2287b03e2e
Step 2/4 : SHELL [ "powershell", "-command" ]
 ---> Using cache
 ---> 8fc2f8b81b5b
Step 3/4 : RUN $ErrorActionPreference = "Stop";     $ProgressPreference = "SilentlyContinue";     $null = New-Item -Path c:demos -Type Directory
 ---> Using cache
 ---> 922a8037b585
Step 4/4 : RUN $null = New-Item -Path c:demosdemo02 -Type Directory
 ---> Running in 295a54558ab5
Removing intermediate container 295a54558ab5
 ---> 386e4f6cd28d
Successfully built 386e4f6cd28d
Successfully tagged greggu/demo02:0.0.1
對指令進(jìn)行排序

由于Docker在處理Dockerfile時是自頂向下處理的,每行指令將和緩存的文件層進(jìn)行對比。如果緩存中沒有所需的文件層,Docker將按照Dockerfile的指令進(jìn)行構(gòu)建。為了合理利用緩存從而加快鏡像構(gòu)建速度,一般推薦將在多個鏡像中共享的文件層的構(gòu)建指令放在Dockerfile前部,而改變的部分放在Dockerfile尾部,具體的Dockerfile可以參考上一個優(yōu)化建議。

Dockerfile編寫風(fēng)格

在編寫Dockerfile時,一般遵循以下兩點。第一點,所有Docker指令在Dockerfile中需要大寫,從而和具體的操作命令區(qū)分開來。第二點,對于過長的指令請合理使用換行符,從而增加Dockerfile的可閱讀性。

總結(jié)

本文總結(jié)了在構(gòu)建Windows容器時,如何對Dockerfile進(jìn)行優(yōu)化,提高構(gòu)建效率,在各位編寫多個Dockerfile之后,相信會增加對本文的理解。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/27307.html

相關(guān)文章

  • Windows Containers 冒險: 出行計劃(Dockerfile)

    摘要:其中指令用于指定在構(gòu)建新鏡像時將使用的基礎(chǔ)鏡像,通用用途的容器一般可以選擇則或者。這張表格里混合了好幾種情況,可以按照縱向列的方式來閱讀。因此命令之間是通過分號進(jìn)行分隔的??偨Y(jié)文章標(biāo)題中使用了出行計劃來形容之于容器制作的作用。 前言 在有了Docker相關(guān)的基礎(chǔ)知識后,就可以開始指定出行計劃了(Dockerfile),計劃里將記錄我們的出發(fā)點(FROM),需要購買的物品(COPY/AD...

    tomener 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<