1. 程式人生 > >Docker基礎容器的縮減方法

Docker基礎容器的縮減方法

系統環境:

Centos6.5_x86_64

核心:4.11.3-1.el6.elrepo.x86_64   升級核心主要是方便安裝docker,之前的環境被弄壞了;

docker版本:version 1.7.1

 效果:基礎的系統映象為109.8MB

具體的製作方法可參考上一篇文章,這裡不作詳細製作,主要是減少映象的大小;

 
  1. [[email protected] images]# yum -y --installroot=${centos_root} install yum 

這樣製作出來的系統映象就已經是342M了

 
  1. [[email protected] images]# du -sh centos65/ 
  2. 342M    centos65/ 

 

優化思路:

刪除一些沒用的安裝包,安裝檔案、語言檔案包,幫助文件等;

 
  1. [[email protected] 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. [[email protected] 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. [[email protected] 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. [[email protected] packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|wc -l 
  2.  
  3. 90 
  4. 直接刪除吧 
  5. [[email protected] packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|xargs rm -rf 

刪除後檢視一下系統現在的大小,已經小了很多了;

 
  1. [[email protected] packages]# du -sh /images/centos65/ 
  2. 268M    /images/centos65/ 

 

接下來就是佔空間最多的/usr目錄了:

 
  1. [[email protected] 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. [[email protected] 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. [[email protected] /]# rm /usr/lib/locale/locale-archive 
  3. 由於上面刪除了檔案,所以這裡檢視是空的 
  4. [[email protected] /]# localedef --list-archive 
  5. 把需要的幾個加入; 
  6. [[email protected] /]# localedef -i en_US -f UTF-8 en_US.UTF-8 
  7. [[email protected] /]# localedef -i zh_CN -f UTF-8 zh_CN.UTF-8 
  8. [[email protected] /]# localedef -i zh_CN -f GB2312 zh_CN 
  9. [[email protected] /]# localedef -i zh_CN -f GB2312 zh_CN.GB2312 
  10. [[email protected] /]# localedef -i zh_CN -f GBK zh_CN.GBK 
  11. 再次檢視列表,就只剩下剛才加入的幾個; 
  12. [[email protected] /]# 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. [[email protected] /]# 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. [[email protected] locale]# ls /images/centos65/usr/lib/locale/ |egrep -v ^"en_US|zh" |xargs rm -rf 

最後目錄下剩下這幾個檔案即可;

 

 
  1. [[email protected] 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. [[email protected] 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. [[email protected] 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. [[email protected] 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. [[email protected] 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. [[email protected] 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 <[email protected]>                                                                0 B                  
  12. 3ab3553c76061666473f9a4c5d8b0e2df1af468aec42415e39af56053849a160   About an hour ago                                                                                                                    109.8 MB            Imported from - 

主要還是一個java環境和Tomcat佔用比較大的空間;這裡並不是適合所有的執行環境,我主要考慮到目前公司所用到的Tomcat環境;

映象是變小了但是要能跑起來才是真的;