Docker实用技巧之更改软件包源提升构建速度
<h2 id="一开篇">一.开篇</h2><p>地球,中国,成都市,某小区的阳台上,一青年负手而立,闭目沉思,阵阵的凉风吹得他衣衫呼呼的飘。忽然,他抬起头,刹那间,睁开了双眼,好似一到精光射向星空,只见这夜空......一颗星星都没有。他叹了下气,“今日夜观星象,看来是时候了。”他走到电脑桌前,双手不断的做出各种手势,同时口中念着晦涩难懂的语言——嘛咪嘛咪哄,最后只见他将一只手放在了笔记本电脑上,同时大喊:“出来吧!我的皮卡丘。”,只见贴在笔记本电脑上的一张泛黄的写着奇怪文字和图案的纸在燃烧,好像在进行一种神秘的解除封印的仪式。纸烧完,他打开了笔记本,点开了“Typora“,沉思一会,打了几个字——Docker实用技巧之更改软件包源提升构建速度。</p>
<p>成都的天气刚经历了雨后初晴,还带着丝丝凉爽的空气,结束了雨天,是时候感受一下阳光了。转眼间似火的七月已过了大半,但这个月我还基本没写技术性的博客,虽然写了几篇关于CentOS下的一些软件的安装方法,但那些都是我自己做的一些记录而形成的,今天给大家带来一篇关于Docker的实用技巧。</p>
<h2 id="二问题说明">二.问题说明</h2>
<p>我的一个开源项目提供了在线示例,项目代码在github,提交代码以后通过Jenkins持续集成,以Docker的方式运行。大家用过.NET Core的人应该都知道,.NET Core 默认是不带 System.Drawing的,前期有第三方的实现,为我们提供了一个解决方案,现在官方也提供了一个,以nuget包的方式发布,名为 <code>System.Drawing.Common</code>,实用这个包可以正常的进行各种图片操作,比如生成图片验证码,二维码等等,但是如果我们将其发布到linux,将会出现异常:<code>Unable to load DLL 'libgdiplus' </code>。解决办法是,我们在构建Docker镜像的时候,可以通过命令装上<code>libgdiplus </code>,但是如果直接写命令<code>apt-get install -y libgdiplus </code> ,你会发现构建会出错,找不到这个包,我们需要在执行这个命令之前,执行<code> apt-get update</code>更新软件包源,那么问题来了,我在第一次构建Docker镜像(没有使用Cache)的执行 <code>apt-get update</code>命令时,非常的慢。最后整个构建过程花了12分钟。</p>
<p>构建的程序为 ASP.NET Core 2.1 应用程序,使用的基础镜像为微软官方提供的:<code>microsoft/dotnet:2.1-aspnetcore-runtime</code></p>
<p><img src="https://images2018.cnblogs.com/blog/668104/201807/668104-20180720004254867-848862348.png" alt="构建用时" loading="lazy"></p>
<p>这不能忍啊,简直是太慢了,查看日志,发现这里执行非常慢:</p>
<pre><code class="language-shell">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
Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 sgml-base all 1.29
Get:3 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libxml2 amd64 2.9.4+dfsg1-2.2+deb9u2
Get:4 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 ucf all 3.0036
...此处省略28个,一共32个
</code></pre>
<p>应该是从 <code>http://cdn-fastly.deb.debian.org/debian</code>获取数据太慢导致的,所以,准备替换构建所使用的基础镜像的软件包源,准备替换为网易提供的包源<code>http://mirrors.163.com/</code></p>
<h2 id="三问题解决--替换软件包源">三.问题解决--替换软件包源</h2>
<p>软件包源的配置文件在<strong>基础镜像所用的Linux系统</strong>中路径为 <code>/etc/apt/sources.list </code>,我们只需在执行 <code>apt-get update</code>命令之前,将我们编写好的使用网易包源的配置文件进行替换就行了。</p>
<p>使用网易包源的配置文件:</p>
<p><strong>sources.list</strong></p>
<pre><code>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
</code></pre>
<p>Dockerfile:</p>
<pre><code class="language-shell">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"]
</code></pre>
<p>主要是这两句命令:</p>
<pre><code class="language-shell">#备份旧的配置文件
mv /etc/apt/sources.list /etc/apt/sources.list.bak
#替换为我们自定义的配置文件
mv sources.list /etc/apt/
</code></pre>
<p>需要注意的是,<strong>sources.list</strong> 需要放在我们打包的目录,保证能复制到镜像里面。</p>
<p>然后构建时间由12分钟缩短到37秒,这个过程是没有使用Docker Cache所花的时间:</p>
<p><img src="https://images2018.cnblogs.com/blog/668104/201807/668104-20180720004254521-1449078740.png" alt="1532016705467" loading="lazy"></p>
<h2 id="四其他加速">四.其他加速</h2>
<h3 id="1腾讯云">1.腾讯云</h3>
<p>我的服务器是使用的腾讯云,腾讯云也提供了软件包源,分为内网和外网,外网是所有人都能使用,内网只能腾讯云的服务器使用。使用内网的包源将会获得更快的速度。详细说明:https://cloud.tencent.com/document/product/213/8623</p>
<p><img src="https://images2018.cnblogs.com/blog/668104/201807/668104-20180720004254192-238946712.png" alt="1532017048199" loading="lazy"></p>
<p>使用内网的腾讯云包源配置文件:</p>
<pre><code>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
</code></pre>
<h3 id="2阿里云">2.阿里云</h3>
<p>阿里云作为一个全球第三的云平台运营商,也是具有此项服务的。其包源地址为:<code>https://mirrors.aliyun.com</code></p>
<p>配置文件:</p>
<pre><code>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
</code></pre>
<h2 id="五其他linux系统镜像">五.其他Linux系统镜像</h2>
<p>我用的Docker镜像所使用的Linux系统为 debian,如果你是用的不是 debian,那么你可以通过以下几个步骤来进行包源的更改。</p>
<h3 id="方法一">方法一</h3>
<p>1.通过你所使用镜像官方提供的资料,查询出镜像所使用的Linux系统包源路径以及配置文件内容</p>
<p>2.替换加速地址</p>
<h3 id="方法二">方法二</h3>
<p>1.使用你需要使用的镜像构建一个简单的程序,然后运行。</p>
<p>2.通过Docker交互模式,进入容器。</p>
<p>3.查询出使用的系统Linux镜像版本</p>
<p>4.找到并查看包源配置文件</p>
<p>5.复制配置文件内容,然后将地址替换为对应的加速地址</p>
<h2 id="六结束">六.结束</h2>
<p>“贫僧”能解决这个问题,非常感谢其他朋友提供的资料,“百度”(google、bing)不愧是武林绝学,2333:</p>
<p>debian系linux,更换apt-get官方源为国内源</p>
<p>最后,“贫僧”鸠摩智参见,各位后会有期:</p>
<p><img src="https://images2018.cnblogs.com/blog/668104/201807/668104-20180720004252699-2106816856.png" alt="1532018498089" loading="lazy"></p>
</div>
<div id="MySignature" role="contentinfo">
<blockquote>
<strong>目前学习.NET Core 最好的教程 .NET Core 官方教程 ASP.NET Core 官方教程</strong>
</blockquote><br><br>
来源:https://www.cnblogs.com/stulzq/p/9339250.html
頁:
[1]