成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Windows Containers 大冒險(xiǎn): 優(yōu)化計(jì)劃(Dockerfile)

Enlightenment / 2992人閱讀

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

前言

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

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

分層文件系統(tǒng)

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

對(duì)于以下的Dockerfile,可以通過(guò) docker history 來(lái)查看分層文件系統(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 對(duì)相關(guān)指令采取分組合并

首先針對(duì)以下Dockerfile構(gòu)建鏡像,請(qǐng)?jiān)跇?gòu)建過(guò)程中觀察輸出,也可以使用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語(yǔ)句合并成一條執(zhí)行。也就是在本文最開(kāi)始使用的例子。

移除不再需要的文件

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

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

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

因?yàn)槭褂昧藘蓚€(gè)Dockerfile,需要在docker build時(shí)指定具體的Dockerfile,具體構(gòu)建命令可以放在一個(gè)批處理文件中

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
對(duì)指令進(jìn)行排序

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

Dockerfile編寫(xiě)風(fēng)格

在編寫(xiě)Dockerfile時(shí),一般遵循以下兩點(diǎn)。第一點(diǎn),所有Docker指令在Dockerfile中需要大寫(xiě),從而和具體的操作命令區(qū)分開(kāi)來(lái)。第二點(diǎn),對(duì)于過(guò)長(zhǎng)的指令請(qǐng)合理使用換行符,從而增加Dockerfile的可閱讀性。

總結(jié)

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

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/27307.html

相關(guān)文章

  • Windows Containers 冒險(xiǎn): 出行計(jì)劃(Dockerfile)

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

    tomener 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<