在使用docker 的时候,我们通常都有用到base image, 这里以centos为例,我们通常通过: docker search centos , docker pull centos来下载官方的centos, 然后用作base image 来build 我们自己的docker 镜像。
可能你会和我一样想build自己的centos base 镜像,但是可能苦于不知道如何去提取一个base image所需要的相关文件来生成rootfs ,因此无从下手,这里share一个生成rootfs的方法(从网络找到的方法,然后加上了个人的理解和总结):
1.
首先创建一个目录,用做rootfs的根目录, 设置rpm 操作的根目录为rootfs的目录
[root@localhost ~]# mkdir my_rootfs
[root@localhost ~]# cd my_rootfs/
[root@localhost my_rootfs]# cd ..
[root@localhost ~]# rpm --root /root/my_rootfs/ --initdb #设置rpm操作的根目录
2.
到网站:http://vault.centos.org/,根据自己需要的centos版本,定位到相应的package 目录,找到 centos-release 包,下载到本地;然后安装这个包到 上面指定的rootfs的目录,这里用的例子如下:
[root@localhost ~]# wget http://vault.centos.org/7.5.1804/os/x86_64/Packages/centos-release-7-5.1804.el7.centos.x86_64.rpm
--2019-09-01 22:22:57-- http://vault.centos.org/7.5.1804/os/x86_64/Packages/centos-release-7-5.1804.el7.centos.x86_64.rpm
Resolving vault.centos.org (vault.centos.org)... 108.61.16.227
Connecting to vault.centos.org (vault.centos.org)|108.61.16.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 24400 (24K) [application/x-rpm]
Saving to: ‘centos-release-7-5.1804.el7.centos.x86_64.rpm’
100%[=============================================================================================>] 24,400 33.4KB/s in 0.7s
2019-09-01 22:22:58 (33.4 KB/s) - ‘centos-release-7-5.1804.el7.centos.x86_64.rpm’ saved [24400/24400]
[root@localhost ~]#
[root@localhost ~]# rpm -ivh --nodeps --root /root/my_rootfs/ --package ./centos-release-7-5.1804.el7.centos.x86_64.rpm #这里安装的时候需要忽略dependency.
warning: ./centos-release-7-5.1804.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:centos-release-7-5.1804.el7.cento################################# [100%]
warning: %post(centos-release-7-5.1804.el7.centos.x86_64) scriptlet failed, exit status 127
[root@localhost ~]#
3.
配置本机的yum源,确保相应的repository可以使用,然后把yum package 安装到上述的rootfs目录:注意这里安装yum package的时候不可以忽略dependency.
#yum 源的配置过程忽略,centos自带yum源,不用配置就可以使用;
[root@localhost my_rootfs]# yum --installroot=/root/my_rootfs/ install yum
...... #内容太多,这里省略安装过程;
Total 1.6 MB/s | 51 MB 00:00:30
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-5.1804.el7.centos.x86_64 (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
......
Complete!
[root@localhost my_rootfs]#ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
4.
yum package安装完成之后,我们发现目录大小如下, 其中 /root/my_rootfs/var/cache 下的大小为86M, 这些内容为yum 安装时候的缓存,这些缓存是可以清理的,但是我不推荐用直接删除对应目录这种暴力方式来清理;我们在下一步进行清理操作。
[root@localhost my_rootfs]# du -hs ./* | sort -k1h
0 ./bin
0 ./boot
0 ./dev
0 ./home
0 ./lib
0 ./lib64
0 ./media
0 ./mnt
0 ./opt
0 ./proc
0 ./root
0 ./run
0 ./sbin
0 ./srv
0 ./sys
0 ./tmp
1.9M ./etc
94M ./var
265M ./usr
[root@localhost my_rootfs]#
[root@localhost var]# du -hxa . | sort -k1h | tail
31M ./cache/yum/x86_64/7/base/gen
37M ./cache/yum/x86_64/7/base
40M ./cache/yum/x86_64/7/updates/gen
40M ./cache/yum/x86_64/7/updates/gen/primary_db.sqlite
48M ./cache/yum/x86_64/7/updates
86M ./cache
86M ./cache/yum
86M ./cache/yum/x86_64
86M ./cache/yum/x86_64/7
94M .
[root@localhost var]#
5.
通过chroot的方式,切换到rootfs对应的base centos中, 然后清理不必要的yum 缓存;
[root@localhost my_rootfs]# mount --rbind /dev dev/
[root@localhost my_rootfs]# mount --rbind /proc proc/
[root@localhost my_rootfs]# mount --rbind /sys sys/
[root@localhost my_rootfs]# chroot /root/my_rootfs/
bash-4.2#
bash-4.2# yum clean all #进行yum 的缓存清理
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors
bash-4.2#
bash-4.2# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
bash-4.2# du -hsx ./* | sort -k1h | tail
du: cannot access ‘./proc/13246/task/13246/fd/3’: No such file or directory
du: cannot access ‘./proc/13246/task/13246/fdinfo/3’: No such file or directory
du: cannot access ‘./proc/13246/fd/3’: No such file or directory
du: cannot access ‘./proc/13246/fdinfo/3’: No such file or directory
0 ./proc
0 ./root
0 ./run
0 ./sbin
0 ./srv
0 ./sys
0 ./tmp
1.9M ./etc
8.0M ./var
265M ./usr
bash-4.2#
bash-4.2# exit
exit
[root@localhost my_rootfs]#
6.
我们umount之前bind的proc, sys,dev, 然后删除不必要的man帮助文档;最后把/root/my_rootfs/* 进行压缩打包,生成的my_rootfs.tar.gz就是我们的目标文件;
[root@localhost my_rootfs]# umount -l proc
[root@localhost my_rootfs]# umount -l dev
[root@localhost my_rootfs]# umount -l sys
[root@localhost my_rootfs]# mount | grep my_rootfs
[root@localhost my_rootfs]#
[root@localhost my_rootfs]# find . -iname man -exec rm -rf {} \;
[root@localhost my_rootfs]# find . -iname man
[root@localhost my_rootfs]# tar -czvf my_rootfs.tar.gz * #打包的时候要确保只是对需要的内容打包,不要附带目录
......
[root@localhost my_rootfs]# tar -tvf my_rootfs.tar.gz | head
#打包完成后,可以用tar -tvf 命令查看内容,内容需要类似下面格式
lrwxrwxrwx root/root 0 2019-09-01 22:33 bin -> usr/bin
dr-xr-xr-x root/root 0 2018-04-11 12:59 boot/
drwxr-xr-x root/root 0 2019-09-01 22:34 dev/
-rw-r--r-- root/root 0 2019-09-01 22:34 dev/null
drwxr-xr-x root/root 0 2019-09-01 22:34 etc/
drwxr-xr-x root/root 0 2019-09-01 22:34 etc/pki/
drwxr-xr-x root/root 0 2019-09-01 22:26 etc/pki/rpm-gpg/
-rw-r--r-- root/root 1690 2018-04-29 00:35 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
-rw-r--r-- root/root 1004 2018-04-29 00:35 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
-rw-r--r-- root/root 1690 2018-04-29 00:35 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7
[root@localhost my_rootfs]# mv my_rootfs.tar.gz ../
[root@localhost my_rootfs]# cd ..
[root@localhost ~]# ls -lht my_rootfs.tar.gz
-rw-r--r--. 1 root root 81M Sep 4 21:47 my_rootfs.tar.gz
[root@localhost ~]#
那么我们是否可以使用这个my_rootfs.tar.gz 来构建自己的centos base image呢?
请看下篇: 基于centos的rootfs 创建自己的base image
本文原创,转载请注明出处
网友评论