为了账号安全,请及时绑定邮箱和手机立即绑定

Docker实用技巧之更改软件包源提升构建速度

标签:
Docker
一.开篇

地球,中国,成都市,某小区的阳台上,一青年负手而立,闭目沉思,阵阵的凉风吹得他衣衫呼呼的飘。忽然,他抬起头,刹那间,睁开了双眼,好似一到精光射向星空,只见这夜空......一颗星星都没有。他叹了下气,“今日夜观星象,看来是时候了。”他走到电脑桌前,双手不断的做出各种手势,同时口中念着晦涩难懂的语言——嘛咪嘛咪哄,最后只见他将一只手放在了笔记本电脑上,同时大喊:“出来吧!我的皮卡丘。”,只见贴在笔记本电脑上的一张泛黄的写着奇怪文字和图案的纸在燃烧,好像在进行一种神秘的解除封印的仪式。纸烧完,他打开了笔记本,点开了“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所花的时间:

1532016705467

四.其他加速

1.腾讯云

我的服务器是使用的腾讯云,腾讯云也提供了软件包源,分为内网和外网,外网是所有人都能使用,内网只能腾讯云的服务器使用。使用内网的包源将会获得更快的速度。详细说明:https://cloud.tencent.com/document/product/213/8623

1532017048199

使用内网的腾讯云包源配置文件:

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官方源为国内源

最后,“贫僧”鸠摩智参见,各位后会有期:

1532018498089

点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
1.7万
获赞与收藏
175

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消