#构建rootfs文件系统目录结构
[root@www ~]# mkdir -p /rootfs/{dev,bin,lib64}
#植入运行程序和依赖
[root@www rootfs]# ldd /bin/echo #查找应用程序依赖的库文件
#将库文件拷贝
[root@www lib64]# cp -P /lib64/libdl.so.2 /rootfs/lib64/
[root@www lib64]# cp /lib64/libdl-2.28.so /rootfs/lib64/
#拷贝某个程序依赖库的快速方式(for循环),这个可能有问题
[root@www rootfs]# for i in $(ldd /bin/echo|awk '{print $3}'|grep -v '^$');do cp $i /rootfs/lib64/;done
#也可以使用我自己写的shell脚本
[root@www ~]#vim i.sh
#!/bin/bash
DESTDIR=/test/lib64/
read -p "请输入需要拷贝的命令" agr
WUI=$(which $agr | grep /)
for i in $(ldd $WUI | awk '{print $3}' | grep -v '^$')
do
cp -P $i $DESTDIR
for q in $(ls -l $DESTDIR | awk '{print $11}')
do
cp /lib64/$q $DESTDIR
done
done
#打包根文件系统
[root@www /]# tar -cf demo.tar -C rootfs/ .
#生成哈希
[root@www /]# sha256sum demo.tar
d6d603ccca9e6223c26603cde5961cbab0d889505e4ecceb92c4786171cc6ab7 demo.tar
#配置元数据文件
[root@www /]# cat config.json
{
"architecture": "amd64",
"os": "linux",
"config": {
"Entrypoint": ["/bin/bash"],
"Env": ["PATH=/bin"],
"WorkingDir": "/"
},
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:d6d603ccca9e6223c26603cde5961cbab0d889505e4ecceb92c4786171cc6ab7"
]
},
"history": [
{
"created": "2024-06-18T00:00:00Z",
"created_by": "yai_yang"
}
]
}
#配置清单文件
[root@www /]# cat manifest.json
[
{
"Config": "config.json",
"RepoTags": ["zgs:v1"],
"Layers": ["demo.tar"]
}
]
#手工构建镜像
[root@www /]# tar -cf zgs.tar demo.image.tar config.json manifest.json
#导入镜像
[root@www /]# docker load -i zgs.tar
#测试镜像
[root@www ~]# docker run --rm zgs:v3 -c "echo hello"
hello
#继续构建第二层(添加ls命令)
[root@www ~]# vim i.sh
#!/bin/bash
DESTDIR=/rootfs/lib64/
read -p "请输入需要拷贝的命令" agr
WUI=$(which $agr | grep /)
for i in $(ldd $WUI | awk '{print $3}' | grep -v '^$')
do
cp -P $i $DESTDIR
for q in $(ls -l $DESTDIR | awk '{print $11}')
do
cp /lib64/$q $DESTDIR
done
done
#打包根文件系统
[root@www /]# tar -cf demo-v2.tar -C rootfs/ .
#生成哈希
[root@www /]# sha256sum demo-v2.tar
326f73e9c09cd642ec995a2925c2d2a8e281ef8aef048bf10fef4192b4f1cb0b
#将哈希添加到元数据文件
[root@www /]# vim config.json
{
"architecture": "amd64",
"os": "linux",
"config": {
"Entrypoint": ["/bin/bash"],
"Env": ["PATH=/bin"],
"WorkingDir": "/"
},
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:5902b9a4064d52db3b570c44654a9817ebff970b6b320408f7c2e4ac921e5ccb",
"sha256:326f73e9c09cd642ec995a2925c2d2a8e281ef8aef048bf10fef4192b4f1cb0b"
]
},
"history": [
{
"created": "2024-06-18T00:00:00Z",
"created_by": "yai_yang"
}
]
}
#将tar包添加到清单文件
[root@www /]# vim manifest.json
[
{
"Config": "config.json",
"RepoTags": ["zgs:v4"],
"Layers": ["demo.tar","demo-v2.tar"]
}
]
#手工构建镜像
tar -cf zgs-v2.tar demo-v2.tar manifest.json config.json
#拉取镜像
docker load -i zgs-v2.tar
#运行测试
[root@www ~]# docker run --rm zgs:v4 -c "ls -l"
total 0
drwxr-xr-x 1 0 0 40 Nov 20 05:08 bin
drwxr-xr-x 5 0 0 340 Nov 20 05:12 dev
drwxr-xr-x 2 0 0 66 Nov 20 05:12 etc
drwxr-xr-x 1 0 0 335 Nov 20 05:08 lib64
dr-xr-xr-x 270 0 0 0 Nov 20 05:12 proc
dr-xr-xr-x 13 0 0 0 Nov 20 05:12 sys
利用Dockerfile构建原始镜像
#准备rootfs根文件系统
[root@www ~]# cd /opt/
[root@www ~]# mkdir rootfs
#创建一个最小化的 Linux 根文件系统
[root@www ~]# yum install -y --installroot=$PWD/rootfs --releasever=8.5 --setopt=install_weak_deps=False --nodocs glibc-minimal-langpack dnf bash coreutils curl rpm rpm-libs rpm-sign --exclude=kernel*
1. 基本参数
-y:自动回答 "yes",无需手动确认安装
--installroot=$PWD/rootfs:指定安装根目录为当前目录下的 rootfs 文件夹
2. 版本和配置参数
--releasever=8.5:指定系统版本为 8.5(Rocky/CentOS/RHEL 8.5)
--setopt=install_weak_deps=False:不安装弱依赖包
--nodocs:不安装文档文件,减少安装大小
3. 软件包列表
glibc-minimal-langpack:最小化的 glibc 语言包
dnf:新一代的包管理器(Yum 的替代品)
bash:Bourne Again Shell,命令行解释器
coreutils:核心工具集(ls, cp, mv, rm 等基本命令)
curl:网络数据传输工具
rpm:RPM 包管理器
rpm-libs:RPM 相关的库文件
rpm-sign:RPM 包签名工具
4. 排除参数
--exclude=kernel*:排除所有内核相关的包
#清理缓存
[root@www ~]# rm -rf rootfs/usr/share/{man,doc,info,locale,i18n,zoneinfo}
[root@www ~]# rm -rf rootfs/usr/lib/locale/*
[root@www ~]# rm -rf rootfs/var/cache/dnf/*
[root@www ~]# rm -rf rootfs/var/log/*
[root@www ~]# rm -rf rootfs/var/tmp/*
[root@www ~]# rm -rf rootfs/tmp/*
[root@www ~]# rm -rf rootfs/etc/systemd
[root@www ~]# rm -rf rootfs/lib/udev
#将根文件系统打包
[root@www opt]# tar -cf centos85.tar -C rootfs/ .
#编写Dockerfile
[root@www opt]# vim Dockerfile
FROM scratch
ADD centos85.tar /
CMD ["/bin/bash"]
#构建镜像
[root@www opt]# docker build -t centos85:latest .
#运行测试
[root@www opt]# docker run -it centos85:latest /bin/bash
bash-4.4# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var