1. 程式人生 > >Docker實用技巧之更改軟件包源提升構建速度

Docker實用技巧之更改軟件包源提升構建速度

bing aspnet clean int dock tro 雲平臺 定義 找到

一.開篇

地球,中國,成都市,某小區的陽臺上,一青年負手而立,閉目沈思,陣陣的涼風吹得他衣衫呼呼的飄。忽然,他擡起頭,剎那間,睜開了雙眼,好似一到精光射向星空,只見這夜空......一顆星星都沒有。他嘆了下氣,“今日夜觀星象,看來是時候了。”他走到電腦桌前,雙手不斷的做出各種手勢,同時口中念著晦澀難懂的語言——嘛咪嘛咪哄,最後只見他將一只手放在了筆記本電腦上,同時大喊:“出來吧!我的皮卡丘。”,只見貼在筆記本電腦上的一張泛黃的寫著奇怪文字和圖案的紙在燃燒,好像在進行一種神秘的解除封印的儀式。紙燒完,他打開了筆記本,點開了“Typora“,沈思一會,打了幾個字——Docker實用技巧之更改軟件包源提升構建速度。

成都的天氣剛經歷了雨後初晴,還帶著絲絲涼爽的空氣,結束了雨天,是時候感受一下陽光了。轉眼間似火的七月已過了大半,但這個月我還基本沒寫技術性的博客,雖然寫了幾篇關於CentOS下的一些軟件的安裝方法,但那些都是我自己做的一些記錄而形成的,今天給大家帶來一篇關於Docker的實用技巧。

二.問題說明

我的一個開源項目提供了在線示例,項目代碼在github,提交代碼以後通過Jenkins持續集成,以Docker的方式運行。大家用過.NET Core的人應該都知道,.NET Core 默認是不帶 System.Drawing的,前期有第三方的實現,為我們提供了一個解決方案,現在官方也提供了一個,以nuget包的方式發布,名為 System.Drawing.Common

,實用這個包可以正常的進行各種圖片操作,比如生成圖片驗證碼,二維碼等等,但是如果我們將其發布到linux,將會出現異常:Unable to load DLL ‘libgdiplus‘。解決辦法是,我們在構建Docker鏡像的時候,可以通過命令裝上libgdiplus,但是如果直接寫命令apt-get install -y libgdiplus ,你會發現構建會出錯,找不到這個包,我們需要在執行這個命令之前,執行apt-get update更新軟件包源,那麽問題來了,我在第一次構建Docker鏡像(沒有使用Cache)的執行 apt-get update命令時,非常的慢。最後整個構建過程花了12分鐘。

構建的程序為 ASP.NET Core 2.1 應用程序,使用的基礎鏡像為微軟官方提供的:microsoft/dotnet:2.1-aspnetcore-runtime

技術分享圖片

這不能忍啊,簡直是太慢了,查看日誌,發現這裏執行非常慢:

After this operation, 38.8 MB of additional disk space will be used.
Get:1 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libxau6 amd64 1:1.0.8-1 [20.7 kB]
Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 sgml-base all 1.29 [14.8 kB]
Get:3 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libxml2 amd64 2.9.4+dfsg1-2.2+deb9u2 [920 kB]
Get:4 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 ucf all 3.0036 [70.2 kB]
...此處省略28個,一共32個

應該是從 http://cdn-fastly.deb.debian.org/debian獲取數據太慢導致的,所以,準備替換構建所使用的基礎鏡像的軟件包源,準備替換為網易提供的包源 http://mirrors.163.com/

三.問題解決--替換軟件包源

軟件包源的配置文件在基礎鏡像所用的Linux系統中路徑為 /etc/apt/sources.list,我們只需在執行 apt-get update命令之前,將我們編寫好的使用網易包源的配置文件進行替換就行了。

使用網易包源的配置文件:

sources.list

deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib

Dockerfile:

FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY . .
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && mv sources.list /etc/apt/ && apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
EXPOSE 80
ENTRYPOINT ["dotnet", "Alipay.Demo.PCPayment.dll"]

主要是這兩句命令:

#備份舊的配置文件
mv /etc/apt/sources.list /etc/apt/sources.list.bak
#替換為我們自定義的配置文件
mv sources.list /etc/apt/

需要註意的是,sources.list 需要放在我們打包的目錄,保證能復制到鏡像裏面。

然後構建時間由12分鐘縮短到37秒,這個過程是沒有使用Docker Cache所花的時間:

技術分享圖片

四.其他加速

1.騰訊雲

我的服務器是使用的騰訊雲,騰訊雲也提供了軟件包源,分為內網和外網,外網是所有人都能使用,內網只能騰訊雲的服務器使用。使用內網的包源將會獲得更快的速度。詳細說明:https://cloud.tencent.com/document/product/213/8623

技術分享圖片

使用內網的騰訊雲包源配置文件:

deb http://mirrors.tencentyun.com/debian/ jessie main non-free contrib
deb http://mirrors.tencentyun.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.tencentyun.com/debian/ jessie-backports main non-free contrib
deb-src http://mirrors.tencentyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.tencentyun.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.tencentyun.com/debian/ jessie-backports main non-free contrib
deb http://mirrors.tencentyun.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.tencentyun.com/debian-security/ jessie/updates main non-free contrib

2.阿裏雲

阿裏雲作為一個全球第三的雲平臺運營商,也是具有此項服務的。其包源地址為:https://mirrors.aliyun.com

配置文件:

deb https://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb https://mirrors.aliyun.com/debian/ jessie-updates main non-free contrib
deb https://mirrors.aliyun.com/debian/ jessie-backports main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ jessie-updates main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ jessie-backports main non-free contrib
deb https://mirrors.aliyun.com/debian-security/ jessie/updates main non-free contrib
deb-src https://mirrors.aliyun.com/debian-security/ jessie/updates main non-free contrib

五.其他Linux系統鏡像

我用的Docker鏡像所使用的Linux系統為 debian,如果你是用的不是 debian,那麽你可以通過以下幾個步驟來進行包源的更改。

方法一

1.通過你所使用鏡像官方提供的資料,查詢出鏡像所使用的Linux系統包源路徑以及配置文件內容

2.替換加速地址

方法二

1.使用你需要使用的鏡像構建一個簡單的程序,然後運行。

2.通過Docker交互模式,進入容器。

3.查詢出使用的系統Linux鏡像版本

4.找到並查看包源配置文件

5.復制配置文件內容,然後將地址替換為對應的加速地址

六.結束

“貧僧”能解決這個問題,非常感謝其他朋友提供的資料,“百度”(google、bing)不愧是武林絕學,2333:

debian系linux,更換apt-get官方源為國內源

最後,“貧僧”鳩摩智參見,各位後會有期:

技術分享圖片

Docker實用技巧之更改軟件包源提升構建速度