系统制成docker镜像_如何让Docker基础镜像变得更小?
本次實驗是基于上一篇在《Centos7系統(tǒng)上制作一個7系的Docker鏡像》方法制作,只是這次先在Centos65的系統(tǒng)上實現(xiàn),并配置本人在減少鏡像大小的一些思路和方法;
系統(tǒng)環(huán)境:
Centos6.5_x86_64
內(nèi)核:4.11.3-1.el6.elrepo.x86_64 升級內(nèi)核主要是方便安裝docker,之前的環(huán)境被弄壞了;
docker版本:version 1.7.1
效果:基礎(chǔ)的系統(tǒng)鏡像為109.8MB
具體的制作方法可參考上一篇文章,這里不作詳細制作,主要是減少鏡像的大小;
[root@centos images]# yum -y --installroot=${centos_root} install yum
這樣制作出來的系統(tǒng)鏡像就已經(jīng)是342M了
[root@centos images]# du -sh centos65/
342M centos65/
優(yōu)化思路:
刪除一些沒用的安裝包,安裝文件、語言文件包,幫助文檔等;
[root@centos 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/
可以看到整個鏡像系統(tǒng)中最大的兩個目錄:/usr和/var
[root@centos 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
說明這里要小心點,不然刪除錯了本機的環(huán)境哦。
系統(tǒng)中大小10M的文件就上面幾個,其中兩個在/var目錄中,通過上面路徑看來,都是yum安裝后的安裝包和數(shù)據(jù)文件,因為docker里不會再Yum安裝東西了所以直接刪除:
[root@centos packages]# find /images/centos65/var/ -size +10M |xargs rm -rf
系統(tǒng)中的rpm安裝包文件,有90個是在/images/centos65/var/cache/yum/x86_64/$releasever/local_net_c6/packages/目錄下,這些都可以刪除;
[root@centos packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|wc -l
90
直接刪除吧
[root@centos packages]# find /images/centos65/var/cache/yum/ -name "*.rpm"|xargs rm -rf
刪除后查看一下系統(tǒng)現(xiàn)在的大小,已經(jīng)小了很多了;
[root@centos packages]# du -sh /images/centos65/
268M /images/centos65/
接下來就是占空間最多的/usr目錄了:
[root@centos 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/
一層一層來查看哪些文件占用了我們的空間;
[root@centos 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 #系統(tǒng)的一些說明文檔,docker用不著
9424 /images/centos65/usr/share/i18n #語言包可以只留英文和中文
9156 /images/centos65/usr/share/cracklib
這里先切換一下shell的環(huán)境到些鏡像目錄下,這里要小心不要弄壞了機器里的配置;
chroot /images/centos65 /bin/bash
刪除原文件,并重新把需要的加入;
這個文件是關(guān)于語言支持的,默認包含各種語言和字符集支持,服務(wù)器用的是字符界面,根本不需要那么多,有en_US.UTF-8就差不多了,我這里把中文zh_CN也加入算了;
刪除文件:
[root@centos /]# rm /usr/lib/locale/locale-archive
由于上面刪除了文件,所以這里查看是空的
[root@centos /]# localedef --list-archive
把需要的幾個加入;
[root@centos /]# localedef -i en_US -f UTF-8 en_US.UTF-8
[root@centos /]# localedef -i zh_CN -f UTF-8 zh_CN.UTF-8
[root@centos /]# localedef -i zh_CN -f GB2312 zh_CN
[root@centos /]# localedef -i zh_CN -f GB2312 zh_CN.GB2312
[root@centos /]# localedef -i zh_CN -f GBK zh_CN.GBK
再次查看列表,就只剩下剛才加入的幾個;
[root@centos /]# localedef --list-archive
en_US.utf8
zh_CN
zh_CN.gb2312
zh_CN.gbk
zh_CN.utf8
再次查看文件的大小變化,從95M變成了4.7M,如果只要utf8的才1.5M左右;
[root@centos /]# ls -lh /usr/lib/locale/locale-archive
-rw-r--r-- 1 root root 4.7M Jun 4 23:40 /usr/lib/locale/locale-archive
由于制作的系統(tǒng)中很多命令都沒有,比如xargs所以退回到系統(tǒng)的shell執(zhí)行刪除文件,上面只是刪除數(shù)據(jù)文件,這里刪除語言包的文件;別刪錯文件哦!
[root@centos locale]# ls /images/centos65/usr/lib/locale/ |egrep -v ^"en_US|zh" |xargs rm -rf
最后目錄下剩下這幾個文件即可;
[root@centos 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
再次查看目錄大小;
[root@centos 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變成現(xiàn)在這么點;
而整個系統(tǒng)鏡像大小;
[root@centos locale]# du -sh /images/centos65/
148M /images/centos65/
刪除doc和man目錄下的文件:
rm -rf /usr/share/doc/*
rm -rf /usr/share/man/*
修改時區(qū):
(將Asia/shanghai-上海時區(qū)寫入當(dāng)前時區(qū))
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數(shù)據(jù)庫,這里可以減少至少1M的空間;
列出目前已經(jīng)安裝的rpm包:rpm -q —all 可以把一些用不著的卸載,如果ldap什么的;
最后確定沒什么rpm包可以卸載了,那剩下的rpm管理工具也沒什么用了,除非你要在docker里安裝rpm工具;
最后打成docker鏡像:
tar -C ${centos_root} -c . | docker import - centos65:0605
打包后的鏡像大小:
[root@docker 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
由于是基礎(chǔ)的鏡像,我再基于此基礎(chǔ)鏡像添加一個java環(huán)境和Tomcat最后大小323.7MB比之前的590.4 MB確實小了很多;
查看鏡像打包的層目錄,沒什么多余的空間了;
[root@docker lib]# docker history --no-trunc centos65:0605
IMAGE CREATED CREATED BY SIZE COMMENT
3ab3553c76061666473f9a4c5d8b0e2df1af468aec42415e39af56053849a160 About an hour ago 109.8 MB Imported from -
[root@docker 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 0 B
3ab3553c76061666473f9a4c5d8b0e2df1af468aec42415e39af56053849a160 About an hour ago 109.8 MB Imported from -
主要還是一個java環(huán)境和Tomcat占用比較大的空間;這里并不是適合所有的運行環(huán)境,我主要考慮到目前公司所用到的Tomcat環(huán)境;
鏡像是變小了但是要能跑起來才是真的;
總結(jié)
以上是生活随笔為你收集整理的系统制成docker镜像_如何让Docker基础镜像变得更小?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 管理费用负数报不了怎么办_我的心脏血管堵
- 下一篇: bootstrap 一排5个_9个非常实