Docker容器技术-优化Docker镜像

一、优化Docker镜像

1.降低部署时间
一个大的Docker应用是如何影响在新Docker宿主机上的部署时间。

(1)编写Dockerfile创建一个大Docker镜像
[root@bogon ~]# cat Dockerfile 
FROM debian:jessie

RUN dd if=/dev/urandom of=/largefile bs=1024 count=524288

(2)编辑这个Dockerfile
[root@bogon ~]# docker build -t hdlptz/largeapp .
Sending build context to Docker daemon  146.9kB
Step 1/2 : FROM debian:jessie
jessie: Pulling from library/debian
85b1f47fba49: Pull complete 
Digest: sha256:f51cf81db2de8b5e9585300f655549812cdb27c56f8bfb992b8b706378cd517d
Status: Downloaded newer image for debian:jessie
 ---> 40aa6d4339d4
Step 2/2 : RUN dd if=/dev/urandom of=/largefile bs=1024 count=524288
 ---> Running in 15ada5b0623d
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 13.5861 s, 39.5 MB/s
 ---> 3561e943c2fa
Removing intermediate container 15ada5b0623d
Successfully built 3561e943c2fa
Successfully tagged hdlptz/largeapp:latest 

(3)检查镜像尺寸
[root@bogon ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED              SIZE
hdlptz/largeapp               latest              3561e943c2fa        About a minute ago   660MB

(4)通过time命令记录上传和拉取时间
这里不做演示了,时间是很长的,因为这依赖于宿主机与Hub之间的网路。

从上面可以看出,当上传和拉取时花费了大量时间。

解决途径:
运行自己私有的Docker registry用于存储和分发Docker镜像。

(1)运行私有Docker registry
[root@bogon ~]# docker run -p 5000:5000 -d registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
49388a8c9c86: Pull complete 
638c4c5f80c0: Pull complete 
da6c9df08ef4: Pull complete 
ee7e568878e6: Pull complete 
386d4eddd833: Pull complete 
Digest: sha256:0694e05b6d0b5fed892ddc60358758bd8341c9a6497ac185f93fc4c93c689810
Status: Downloaded newer image for registry:2
9543c03c9b29473fa0085482dcc17fa73cd644d4309518418ef2d0113826fe86

(2)确认Docker镜像部署速度
对镜像价格标签,用于将它推送到本地
[root@bogon ~]# docker tag hdlptz/largeapp \
> dockerhost:5000/largeapp

(3)测试上传速度
[root@bogon ~]# time docker push dockerhost:5000/largeapp

(4)测试拉取速度
[root@bogon ~]# docker rmi dockerhost:5000/largeapp \
> hdlptz/largeapp
Untagged: dockerhost:5000/largeapp:latest
Untagged: hdlptz/largeapp:latest
Deleted: sha256:3561e943c2fa39a0b523702de9b1079a134f5798a1cbd534183f82a9a51c0f9d
Deleted: sha256:3fd278c7ea286a2a9b48acc65dd9be8fd6141dd9816b13928d05062fba2481c0

[root@bogon ~]# time docker pull dockerhost:5000/largeapp

2.改善镜像编译时间
(1)采用registry镜像
用户网络将仅从Hub下载镜像一次,二用户的其他Docker宿主机可以直接从本地拉取镜像。

A.在装有Debian Jessie系统的Docker宿主机中,通过更新和创建一个Systemd插件文件来配置Docker守护进程,文件位置在:/etc/systemd/system/docker.service.d/10-syslog.conf,在该文件中加入如下内容:
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon-H fd:// \
                                          --registry-mirror=http://dockerhost:5000

B.重启Systemd来加载docker.service的新配置文件
system餐厅了daemon-reload

C.通过重启新配置的Systemd单元来重启Docker守护进程
systemctl restartdocker.service

D.运行作为镜像的registry的Docker容器
[root@bogon ~]# docker run -p 5000:5000 -d \
> -e STANDLONE=false \
> -e MIRROR_SOURCE=https://registry-1.docker.io \
> -e MIRROR_SOURCE_INDEX=https://index.docker.io \
> registry

(2)如何创建Ruby应用程序的Docker镜像
未完待续...

posted @ 2017-11-07 10:14  BXBZ—边学边做  阅读(967)  评论(0编辑  收藏  举报