实现Buildah
snow chuai汇总、整理、撰写---2020/2/15
安装及使用Buildah
本章实验中设备不需要docker存在,但需要一个SSL的私有仓库。请另行建立
1.1 安装及使用Buildah
[root@srv ~]# yum install buildah -y
|
1.2 构建镜像
1) 从centos中构建镜像
[root@srv ~]# buildah from centos
Getting image source signatures
Copying blob 8a29a15cefae done
Copying config 470671670c done
Writing manifest to image destination
Storing signatures
centos-working-container
2) 显示当前的容器
[root@srv ~]# buildah containers
CONTAINER ID BUILDER IMAGE ID IMAGE NAME CONTAINER NAME
fa836e542e63 * 470671670cac docker.io/library/centos:latest centos-working-container
3) 使用scratch创建一个空的容器
[root@srv ~]# buildah from scratch
working-container
[root@srv ~]# buildah containers
CONTAINER ID BUILDER IMAGE ID IMAGE NAME CONTAINER NAME
fa836e542e63 * 470671670cac docker.io/library/centos:latest centos-working-container
e7095a502b94 * scratch working-container
# 使用scratch创建的空容器是没有镜像的,所以无法再镜像列表中显示出来
[root@srv ~]# buildah images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/centos latest 470671670cac 4 weeks ago 245 MB
|
1.3 容器操作
1) 运行容器的指令
[root@srv ~]# buildah containers
CONTAINER ID BUILDER IMAGE ID IMAGE NAME CONTAINER NAME
fa836e542e63 * 470671670cac docker.io/library/centos:latest centos-working-container
e7095a502b94 * scratch working-container
[root@srv ~]# buildah run fa83 echo "Hello 1000cc"
Hello 1000cc
2) 进入容器并操作
[root@srv ~]# buildah run fa83 bash
[root@fa836e542e63 /]#
[root@fa836e542e63 /]# yum install ruby -y
[root@fa836e542e63 /]# exit
exit
[root@srv ~]# buildah run fa83 whereis ruby
ruby: /usr/bin/ruby /usr/lib64/ruby /usr/share/ruby /usr/share/man/man1/ruby.1.gz
|
1.4 复制文件到容器
[root@srv ~]# echo "1000cc.net" > test.txt
[root@srv ~]# buildah copy fa83 test.txt /tmp/test.txt
fa40aa48b5b3e1f9a46f2c3547ce0b1f8691780a49f6a43e20000fae967c3d9c
[root@srv ~]# buildah run fa83 cat /tmp/test.txt
1000cc.net
|
1.5 编辑并设置容器
[root@srv ~]# buildah config --author='snow chuai' d6fc
# 查看当前配置
......
......
......
......
......
......
[root@srv ~]# buildah inspect d6fc
......
......
......
......
......
......
},
{
"created": "2020-02-15T08:25:08.168768644Z",
"author": "snow chuai"
}
]
}
# 查看更多的buildah配置
[root@srv ~]# buildah config -h
......
......
......
......
......
......
|
1.6 挂载容器到当前系统
[root@srv ~]# buildah mount d6fc
/var/lib/containers/storage/overlay/fd247188145......bd65c53b04fae7e9ce019/merged
[root@srv ~]# ll /var/lib/containers/storage/overlay/fd24718814......985418de6dae7e9ce019/merged
total 4
lrwxrwxrwx 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x 2 root root 6 Jan 14 05:48 dev
drwxr-xr-x 51 root root 4096 Jan 14 05:49 etc
drwxr-xr-x 2 root root 6 May 11 2019 home
......
......
......
......
......
......
[root@srv ~]# buildah umount d6fc
d6fc87b0b74ca5b96b30612477d355cbe89bc6f2dbdea49a59da3f251e6a7612
|
1.7 从当前容器创建一个镜像
[root@srv ~]# buildah commit d6fc snow:latest
Getting image source signatures
Copying blob 0683de282177 skipped: already exists
Copying blob 5f70bf18a086 done
Copying config 7de847e3ed done
Writing manifest to image destination
Storing signatures
7de847e3edeb712448e66a7b5f29b1f5995d9a042d67ed4fe4b97de336efc930
[root@srv ~]# buildah images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/snow latest 7de847e3edeb 16 seconds ago 245 MB
docker.io/library/centos latest 470671670cac 4 weeks ago 245 MB
|
1.8 将镜像上传至docker仓库
1) 上传镜像
[root@srv ~]# buildah images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/snow latest 7de847e3edeb 23 minutes ago 245 MB
docker.io/library/centos latest 470671670cac 4 weeks ago 245 MB
[root@srv ~]# buildah push localhost/snow:latest srv.1000cc.net:5000/snow:latest
Getting image source signatures
Copying blob 5f70bf18a086 done
Copying blob 0683de282177 done
Copying config 7de847e3ed done
Writing manifest to image destination
Storing signatures
Successfully pushed //srv.1000cc.net:5000/snow:latest@sha256:0eeea......2f951e5
|
1.9 从空容器中创建一个自定义镜像
1) 创建一个空容器
[root@srv ~]# newcontainer=$(buildah from scratch)
2) 挂载空容器到本地系统
[root@srv ~]# scratchmnt=$(buildah mount $newcontainer)
[root@srv ~]# echo $scratchmnt
/var/lib/containers/storage/overlay/26a1613ad7......d7f39138e283404/merged
3) 对空容器进行安装软件并设置
[root@srv ~]# yum -y install bash coreutils --releasever=7 --installroot=$scratchmnt
4) 卸载
[root@srv ~]# buildah umount $newcontainer
5) 测试容器运行
[root@srv ~]# buildah run $newcontainer bash
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# exit
exit
6) 创建自定义镜像-镜像名为c7-mini.latest
[root@srv ~]# buildah commit $newcontainer c7-mini:latest
Getting image source signatures
Copying blob 5afb193cdcaa done
Copying config 438581fae8 done
Writing manifest to image destination
Storing signatures
438581fae852f651ea998dcab8b8e33da6e07267434cde509022e7c27081a21f
[root@srv ~]# buildah images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/c7-mini latest 438581fae852 45 seconds ago 297 MB
localhost/snow latest 7de847e3edeb 38 minutes ago 245 MB
docker.io/library/centos latest 470671670cac 4 weeks ago 245 MB
|
如对您有帮助,请随缘打个赏。^-^