Docker基础容器的缩减方法

系统环境:

Centos6.5_x86_64

内核:4.11.3-1.el6.elrepo.x86_64   升级内核主要是方便安装docker,之前的环境被弄坏了;

docker版本:version 1.7.1

 效果:基础的系统镜像为109.8MB

具体的制作方法可参考上一篇文章,这里不作详细制作,主要是减少镜像的大小;

 
  1. [root@centos images]# yum -y --installroot=${centos_root} install yum 

这样制作出来的系统镜像就已经是342M了

 
  1. [root@centos images]# du -sh centos65/ 
  2. 342M    centos65/ 

 

优化思路:

删除一些没用的安装包,安装文件、语言文件包,帮助文档等;

 
  1. [root@centos images]# du -h --max-depth=1 /images/centos65/ 
  2. 238M    /images/centos65/usr 
  3. 4.0K    /images/centos65/selinux 
  4. 4.0K    /images/centos65/tmp 
  5. 4.0K    /images/centos65/sys 
  6. 4.0K    /images/centos65/opt 
  7. 4.4M    /images/centos65/etc 
  8. 4.0K    /images/centos65/mnt 
  9. 1.9M    /images/centos65/sbin 
  10. 4.0K    /images/centos65/boot 
  11. 64K /images/centos65/lib 
  12. 4.0K    /images/centos65/srv 
  13. 20K /images/centos65/root 
  14. 4.0K    /images/centos65/media 
  15. 4.0K    /images/centos65/home 
  16. 4.0K    /images/centos65/proc 
  17. 81M /images/centos65/var 
  18. 4.0K    /images/centos65/dev 
  19. 15M /images/centos65/lib64 
  20. 3.0M    /images/centos65/bin 
  21. 342M    /images/centos65/ 

 

可以看到整个镜像系统中最大的两个目录:/usr和/var

 
  1. [root@centos images]# find /images/centos65/ -size +10M  
  2.  
  3. /images/centos65/usr/lib/locale/locale-archive 
  4. /images/centos65/var/cache/yum/x86_64/$releasever/local_net_c6/packages/glibc-common-2.12-1.132.el6.x86_64.rpm 
  5. /images/centos65/var/cache/yum/x86_64/$releasever/local_net_c6/0dafccfdbf892f02acca8267ade4bdcee7280a682e65dc7e29145f3341fd7a8c-primary.sqlite 

说明这里要小心点,不然删除错了本机的环境哦。

系统中大小10M的文件就上面几个,其中两个在/var目录中,通过上面路径看来,都是yum安装后的安装包和数据文件,因为docker里不会再Yum安装东西了所以直接删除:

 
  1. [root@centos packages]# find /images/centos65/var/ -size +10M |xargs rm -rf 

系统中的rpm安装包文件,有90个是在/images/centos65/var/cache/yum/x86_64/$releasever/local_net_c6/packages/目录下,这些都可以删除;

 
  1. [root@centos packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|wc -l 
  2.  
  3. 90 
  4. 直接删除吧 
  5. [root@centos packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|xargs rm -rf 

删除后查看一下系统现在的大小,已经小了很多了;

 
  1. [root@centos packages]# du -sh /images/centos65/ 
  2. 268M    /images/centos65/ 

 

接下来就是占空间最多的/usr目录了:

 
  1. [root@centos centos65]# du -h --max-depth=1 /images/centos65/usr/ 
  2. 12K /images/centos65/usr/src 
  3. 4.0K    /images/centos65/usr/etc 
  4. 1.7M    /images/centos65/usr/sbin 
  5. 79M /images/centos65/usr/share   #这个是一个重点 
  6. 98M /images/centos65/usr/lib 
  7. 132K    /images/centos65/usr/local 
  8. 4.0K    /images/centos65/usr/games 
  9. 768K    /images/centos65/usr/libexec 
  10. 40K /images/centos65/usr/include 
  11. 48M /images/centos65/usr/lib64
  12. 11M /images/centos65/usr/bin 
  13. 238M    /images/centos65/usr/ 

一层一层来查看哪些文件占用了我们的空间;

 
  1. [root@centos centos65]# du --max-depth=1 /images/centos65/usr/share/|sort -rn|head -5 
  2. 80360   /images/centos65/usr/share/ 
  3. 31704   /images/centos65/usr/share/locale   #这个可以清理一下不需要的语言支持 
  4. 11120   /images/centos65/usr/share/doc      #系统的一些说明文档,docker用不着 
  5. 9424    /images/centos65/usr/share/i18n     #语言包可以只留英文和中文 
  6. 9156    /images/centos65/usr/share/cracklib 

这里先切换一下shell的环境到些镜像目录下,这里要小心不要弄坏了机器里的配置;

 
  1. chroot /images/centos65 /bin/bash 

删除原文件,并重新把需要的加入;

这个文件是关于语言支持的,默认包含各种语言和字符集支持,服务器用的是字符界面,根本不需要那么多,有en_US.UTF-8就差不多了,我这里把中文zh_CN也加入算了;

 
  1. 删除文件: 
  2. [root@centos /]# rm /usr/lib/locale/locale-archive 
  3. 由于上面删除了文件,所以这里查看是空的 
  4. [root@centos /]# localedef --list-archive 
  5. 把需要的几个加入; 
  6. [root@centos /]# localedef -i en_US -f UTF-8 en_US.UTF-8 
  7. [root@centos /]# localedef -i zh_CN -f UTF-8 zh_CN.UTF-8 
  8. [root@centos /]# localedef -i zh_CN -f GB2312 zh_CN 
  9. [root@centos /]# localedef -i zh_CN -f GB2312 zh_CN.GB2312 
  10. [root@centos /]# localedef -i zh_CN -f GBK zh_CN.GBK 
  11. 再次查看列表,就只剩下刚才加入的几个; 
  12. [root@centos /]# localedef --list-archive 
  13. en_US.utf8 
  14. zh_CN 
  15. zh_CN.gb2312 
  16. zh_CN.gbk 
  17. zh_CN.utf8 

再次查看文件的大小变化,从95M变成了4.7M,如果只要utf8的才1.5M左右;

 
  1. [root@centos /]# ls -lh /usr/lib/locale/locale-archive 
  2. -rw-r--r-- 1 root root 4.7M Jun  4 23:40 /usr/lib/locale/locale-archive 

 

#由于制作的系统中很多命令都没有,比如xargs所以退回到系统的shell执行删除文件,上面只是删除数据文件,这里删除语言包的文件;别删错文件哦!

 
  1. [root@centos locale]# ls /images/centos65/usr/lib/locale/ |egrep -v ^"en_US|zh" |xargs rm -rf 

最后目录下剩下这几个文件即可;

 

 
  1. [root@centos locale]# ll /images/centos65/usr/lib/locale
  2.  
  3. total 28 
  4. drwxr-xr-x 3 root root 4096 Jun  5 11:02 en_US 
  5. drwxr-xr-x 3 root root 4096 Jun  5 11:02 zh 
  6. drwxr-xr-x 4 root root 4096 Jun  5 11:03 zh_CN 
  7. drwxr-xr-x 3 root root 4096 Jun  5 11:02 zh_CN.GB2312 
  8. drwxr-xr-x 3 root root 4096 Jun  5 11:02 zh_HK 
  9. drwxr-xr-x 4 root root 4096 Jun  5 11:03 zh_TW 
  10. drwxr-xr-x 3 root root 4096 Jun  5 11:02 zh_TW.Big5 

 

再次查看目录大小;

 
  1. [root@centos locale]# du -h --max-depth=1 /images/centos65/usr/share/locale/ 
  2. 8.0K    /images/centos65/usr/share/locale/zh_CN.GB2312 
  3. 84K /images/centos65/usr/share/locale/zh_HK 
  4. 8.0K    /images/centos65/usr/share/locale/zh_TW.Big5 
  5. 684K    /images/centos65/usr/share/locale/zh_CN 
  6. 12K /images/centos65/usr/share/locale/en_US 
  7. 8.0K    /images/centos65/usr/share/locale/zh 
  8. 560K    /images/centos65/usr/share/locale/zh_TW 
  9. 1.4M    /images/centos65/usr/share/locale/ 

 

哇,31M变成现在这么点;

而整个系统镜像大小;

 
  1. [root@centos locale]# du -sh /images/centos65/ 
  2. 148M    /images/centos65/ 

 

删除doc和man目录下的文件:

 
  1. rm -rf /usr/share/doc/* 
  2. #rm -rf /usr/share/man/* 

修改时区:

(将Asia/shanghai-上海时区写入当前时区)

 
  1. cp -f /images/centos65/usr/share/zoneinfo/Asia/Shanghai   /images/centos65/etc/localtime 

然后把其它的都删除:

 
  1. ls -l /images/centos65/usr/share/zoneinfo|grep "^d"|egrep -v "Asia"|xargs rm -rf 

 

rpm --rebuilddb

--rebuilddb:从已安装的包头文件,反向重建RPM数据库,这里可以减少至少1M的空间;

列出目前已经安装的rpm包:rpm -q --all  可以把一些用不着的卸载,如果ldap什么的;

最后确定没什么rpm包可以卸载了,那剩下的rpm管理工具也没什么用了,除非你要在docker里安装rpm工具;

最后打成docker镜像:

 
  1. tar -C ${centos_root} -c . | docker import - centos65:0605 

打包后的镜像大小:

 
  1. [root@docker tomcat7]# docker images 
  2.  
  3. REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE 
  4. tomcat7             latest              4f5a59d6a4f6        16 minutes ago      323.7 MB 
  5. centos65            0605                3ab3553c7606        24 minutes ago      109.8 MB 

由于是基础的镜像,我再基于此基础镜像添加一个java环境和Tomcat最后大小323.7MB比之前的590.4 MB确实小了很多;

查看镜像打包的层目录,没什么多余的空间了;

 
  1. [root@docker lib]# docker history --no-trunc centos65:0605 
  2. IMAGE                                                              CREATED             CREATED BY          SIZE                COMMENT 
  3. 3ab3553c76061666473f9a4c5d8b0e2df1af468aec42415e39af56053849a160   About an hour ago                       109.8 MB            Imported from - 
  4. [root@docker lib]# docker history --no-trunc tomcat7 
  5. IMAGE                                                              CREATED             CREATED BY                                                                                                       SIZE                COMMENT 
  6. 4f5a59d6a4f6c0230009fb68bb0087d415cf2ea4e6daf5c6e5cc477d8ef3ce29   About an hour ago   /bin/sh -c #(nop) EXPOSE 8080/tcp                                                                                0 B                  
  7. 75d5966c1a536d1a3433bab90ba334ccf51e155d54d2aadbe63278d79b523e64   About an hour ago   /bin/sh -c #(nop) ADD file:be537058d2d41a22938599e91dfa595de5fae97dc27bd1ce5ab0fce130492510 in start_tomcat.sh   331 B                
  8. fb81414b2774393e0003de13774be6891b8bfbfc8224f6f0ef53a94b5c9cef6f   About an hour ago   /bin/sh -c #(nop) ADD dir:4bcddd0000ed368fe11dbc56b2d217ff4a136203437de12cb37f471cb33bfc30 in /data/tomcat7      8.085 MB             
  9. c93fd5c4452769665aa483902c4e54a8e95546220ef4a156e645bddb3389c2ae   About an hour ago   /bin/sh -c #(nop) ADD dir:7d35e43fa7ca9bc7a2fad90a23d04a36e287a8b58226fc8e8ab1d6c9671573c7 in /usr/local/java    205.9 MB             
  10. 1c4dbb64eedb4b76a0408daba82aac5756c5b297c0a9b51063a974e231fb4d68   About an hour ago   /bin/sh -c mkdir -p /usr/local/java && mkdir -p /data/tomcat7                                                    0 B                  
  11. aff1ee23010fe73b5af989db66d798a84139dda94d105b3659a9e909c143b83f   About an hour ago   /bin/sh -c #(nop) MAINTAINER swper <hz328@qq.com>                                                                0 B                  
  12. 3ab3553c76061666473f9a4c5d8b0e2df1af468aec42415e39af56053849a160   About an hour ago                                                                                                                    109.8 MB            Imported from - 

主要还是一个java环境和Tomcat占用比较大的空间;这里并不是适合所有的运行环境,我主要考虑到目前公司所用到的Tomcat环境;

镜像是变小了但是要能跑起来才是真的;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值