Docker基礎容器的縮減方法
系統環境:
Centos6.5_x86_64
核心:4.11.3-1.el6.elrepo.x86_64 升級核心主要是方便安裝docker,之前的環境被弄壞了;
docker版本:version 1.7.1
效果:基礎的系統映象為109.8MB
具體的製作方法可參考上一篇文章,這裡不作詳細製作,主要是減少映象的大小;
- [[email protected] images]# yum -y --installroot=${centos_root} install yum
這樣製作出來的系統映象就已經是342M了
- [[email protected] images]# du -sh centos65/
- 342M centos65/
優化思路:
刪除一些沒用的安裝包,安裝檔案、語言檔案包,幫助文件等;
- [[email protected] images]# du -h --max-depth=1 /images/centos65/
- 238M /images/centos65/usr
- 4.0K /images/centos65/selinux
- 4.0K /images/centos65/tmp
- 4.0K /images/centos65/sys
- 4.0K /images/centos65/opt
- 4.4M /images/centos65/etc
- 4.0K /images/centos65/mnt
- 1.9M /images/centos65/sbin
- 4.0K /images/centos65/boot
- 64K /images/centos65/lib
- 4.0K /images/centos65/srv
- 20K /images/centos65/root
- 4.0K /images/centos65/media
- 4.0K /images/centos65/home
- 4.0K /images/centos65/proc
- 81M /images/centos65/var
- 4.0K /images/centos65/dev
- 15M /images/centos65/lib64
- 3.0M /images/centos65/bin
- 342M /images/centos65/
可以看到整個映象系統中最大的兩個目錄:/usr和/var
- [[email protected] images]# find /images/centos65/ -size +10M
- /images/centos65/usr/lib/locale/locale-archive
- /images/centos65/var/cache/yum/x86_64/$releasever/local_net_c6/packages/glibc-common-2.12-1.132.el6.x86_64.rpm
- /images/centos65/var/cache/yum/x86_64/$releasever/local_net_c6/0dafccfdbf892f02acca8267ade4bdcee7280a682e65dc7e29145f3341fd7a8c-primary.sqlite
說明這裡要小心點,不然刪除錯了本機的環境哦。
系統中大小10M的檔案就上面幾個,其中兩個在/var目錄中,通過上面路徑看來,都是yum安裝後的安裝包和資料檔案,因為docker裡不會再Yum安裝東西了所以直接刪除:
- [[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/目錄下,這些都可以刪除;
- [[email protected] packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|wc -l
- 90
- 直接刪除吧
- [[email protected] packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|xargs rm -rf
刪除後檢視一下系統現在的大小,已經小了很多了;
- [[email protected] packages]# du -sh /images/centos65/
- 268M /images/centos65/
接下來就是佔空間最多的/usr目錄了:
- [[email protected] centos65]# du -h --max-depth=1 /images/centos65/usr/
- 12K /images/centos65/usr/src
- 4.0K /images/centos65/usr/etc
- 1.7M /images/centos65/usr/sbin
- 79M /images/centos65/usr/share #這個是一個重點
- 98M /images/centos65/usr/lib
- 132K /images/centos65/usr/local
- 4.0K /images/centos65/usr/games
- 768K /images/centos65/usr/libexec
- 40K /images/centos65/usr/include
- 48M /images/centos65/usr/lib64
- 11M /images/centos65/usr/bin
- 238M /images/centos65/usr/
一層一層來檢視哪些檔案佔用了我們的空間;
- [[email protected] centos65]# du --max-depth=1 /images/centos65/usr/share/|sort -rn|head -5
- 80360 /images/centos65/usr/share/
- 31704 /images/centos65/usr/share/locale #這個可以清理一下不需要的語言支援
- 11120 /images/centos65/usr/share/doc #系統的一些說明文件,docker用不著
- 9424 /images/centos65/usr/share/i18n #語言包可以只留英文和中文
- 9156 /images/centos65/usr/share/cracklib
這裡先切換一下shell的環境到些映象目錄下,這裡要小心不要弄壞了機器裡的配置;
- chroot /images/centos65 /bin/bash
刪除原檔案,並重新把需要的加入;
這個檔案是關於語言支援的,預設包含各種語言和字符集支援,伺服器用的是字元介面,根本不需要那麼多,有en_US.UTF-8就差不多了,我這裡把中文zh_CN也加入算了;
- 刪除檔案:
- [[email protected] /]# rm /usr/lib/locale/locale-archive
- 由於上面刪除了檔案,所以這裡檢視是空的
- [[email protected] /]# localedef --list-archive
- 把需要的幾個加入;
- [[email protected] /]# localedef -i en_US -f UTF-8 en_US.UTF-8
- [[email protected] /]# localedef -i zh_CN -f UTF-8 zh_CN.UTF-8
- [[email protected] /]# localedef -i zh_CN -f GB2312 zh_CN
- [[email protected] /]# localedef -i zh_CN -f GB2312 zh_CN.GB2312
- [[email protected] /]# localedef -i zh_CN -f GBK zh_CN.GBK
- 再次檢視列表,就只剩下剛才加入的幾個;
- [[email protected] /]# localedef --list-archive
- en_US.utf8
- zh_CN
- zh_CN.gb2312
- zh_CN.gbk
- zh_CN.utf8
再次檢視檔案的大小變化,從95M變成了4.7M,如果只要utf8的才1.5M左右;
- [[email protected] /]# ls -lh /usr/lib/locale/locale-archive
- -rw-r--r-- 1 root root 4.7M Jun 4 23:40 /usr/lib/locale/locale-archive
#由於製作的系統中很多命令都沒有,比如xargs所以退回到系統的shell執行刪除檔案,上面只是刪除資料檔案,這裡刪除語言包的檔案;別刪錯檔案哦!
- [[email protected] locale]# ls /images/centos65/usr/lib/locale/ |egrep -v ^"en_US|zh" |xargs rm -rf
最後目錄下剩下這幾個檔案即可;
- [[email protected] locale]# ll /images/centos65/usr/lib/locale
- total 28
- drwxr-xr-x 3 root root 4096 Jun 5 11:02 en_US
- drwxr-xr-x 3 root root 4096 Jun 5 11:02 zh
- drwxr-xr-x 4 root root 4096 Jun 5 11:03 zh_CN
- drwxr-xr-x 3 root root 4096 Jun 5 11:02 zh_CN.GB2312
- drwxr-xr-x 3 root root 4096 Jun 5 11:02 zh_HK
- drwxr-xr-x 4 root root 4096 Jun 5 11:03 zh_TW
- drwxr-xr-x 3 root root 4096 Jun 5 11:02 zh_TW.Big5
再次檢視目錄大小;
- [[email protected] locale]# du -h --max-depth=1 /images/centos65/usr/share/locale/
- 8.0K /images/centos65/usr/share/locale/zh_CN.GB2312
- 84K /images/centos65/usr/share/locale/zh_HK
- 8.0K /images/centos65/usr/share/locale/zh_TW.Big5
- 684K /images/centos65/usr/share/locale/zh_CN
- 12K /images/centos65/usr/share/locale/en_US
- 8.0K /images/centos65/usr/share/locale/zh
- 560K /images/centos65/usr/share/locale/zh_TW
- 1.4M /images/centos65/usr/share/locale/
哇,31M變成現在這麼點;
而整個系統映象大小;
- [[email protected] locale]# du -sh /images/centos65/
- 148M /images/centos65/
刪除doc和man目錄下的檔案:
- rm -rf /usr/share/doc/*
- #rm -rf /usr/share/man/*
修改時區:
(將Asia/shanghai-上海時區寫入當前時區)
- cp -f /images/centos65/usr/share/zoneinfo/Asia/Shanghai /images/centos65/etc/localtime
然後把其它的都刪除:
- 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映象:
- tar -C ${centos_root} -c . | docker import - centos65:0605
打包後的映象大小:
- [[email protected] tomcat7]# docker images
- REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
- tomcat7 latest 4f5a59d6a4f6 16 minutes ago 323.7 MB
- centos65 0605 3ab3553c7606 24 minutes ago 109.8 MB
由於是基礎的映象,我再基於此基礎映象新增一個java環境和Tomcat最後大小323.7MB比之前的590.4 MB確實小了很多;
檢視映象打包的層目錄,沒什麼多餘的空間了;
- [[email protected] lib]# docker history --no-trunc centos65:0605
- IMAGE CREATED CREATED BY SIZE COMMENT
- 3ab3553c76061666473f9a4c5d8b0e2df1af468aec42415e39af56053849a160 About an hour ago 109.8 MB Imported from -
- [[email protected] lib]# docker history --no-trunc tomcat7
- IMAGE CREATED CREATED BY SIZE COMMENT
- 4f5a59d6a4f6c0230009fb68bb0087d415cf2ea4e6daf5c6e5cc477d8ef3ce29 About an hour ago /bin/sh -c #(nop) EXPOSE 8080/tcp 0 B
- 75d5966c1a536d1a3433bab90ba334ccf51e155d54d2aadbe63278d79b523e64 About an hour ago /bin/sh -c #(nop) ADD file:be537058d2d41a22938599e91dfa595de5fae97dc27bd1ce5ab0fce130492510 in start_tomcat.sh 331 B
- fb81414b2774393e0003de13774be6891b8bfbfc8224f6f0ef53a94b5c9cef6f About an hour ago /bin/sh -c #(nop) ADD dir:4bcddd0000ed368fe11dbc56b2d217ff4a136203437de12cb37f471cb33bfc30 in /data/tomcat7 8.085 MB
- c93fd5c4452769665aa483902c4e54a8e95546220ef4a156e645bddb3389c2ae About an hour ago /bin/sh -c #(nop) ADD dir:7d35e43fa7ca9bc7a2fad90a23d04a36e287a8b58226fc8e8ab1d6c9671573c7 in /usr/local/java 205.9 MB
- 1c4dbb64eedb4b76a0408daba82aac5756c5b297c0a9b51063a974e231fb4d68 About an hour ago /bin/sh -c mkdir -p /usr/local/java && mkdir -p /data/tomcat7 0 B
- aff1ee23010fe73b5af989db66d798a84139dda94d105b3659a9e909c143b83f About an hour ago /bin/sh -c #(nop) MAINTAINER swper <[email protected]> 0 B
- 3ab3553c76061666473f9a4c5d8b0e2df1af468aec42415e39af56053849a160 About an hour ago 109.8 MB Imported from -
主要還是一個java環境和Tomcat佔用比較大的空間;這裡並不是適合所有的執行環境,我主要考慮到目前公司所用到的Tomcat環境;
映象是變小了但是要能跑起來才是真的;