Docker镜像的首行从FROM alpine之类的镜像开始,但是最初的基础镜像是如何创建的,本文使用一个busybox创建一个基础镜像,相信在此过程中会对docker一些相关的概念有进一步的理解。
什么是基础镜像(base image)
简单来说,基础镜像就是没有From或者FROM scratch开头的Dockerfile所构建出来的镜像。比如alpine,这个很小的linux镜像目前只有4M左右
[root@kong ~]# docker images |grep alpine
docker.io/alpine latest 3fd9065eaf02 4 months ago 4.15 MB
[root@kong ~]#
它的Dockerfile很简单,只有三行,这就是一个基础镜像,
FROM scratch
ADD rootfs.tar.xz /
CMD ["/bin/sh"]
在接下来的文章中我们将会像alpine那样来创建一个自己的基础镜像。
busybox
概要说明
busybox被称为嵌入式linux的瑞士军刀,这句话是在busybox自己介绍自己的时候提出的(The Swiss Army Knife of Embedded Linux)。busybox整合了很多小的unix下的通用功能到一个小的可执行文件之中,简单来说在unix或者linux下常用的那些功能在这里你都能找到,但是为了busybox的目标:嵌入式的linux,大小对于busybox来说是非常重要的优化要素和限制,这些功能有可能会有所阉割,但是对于一般需求来说已经足够。而alpine就是在busybox基础上增加了自己的包管理工具apk等功能创建了风靡一时的小巧镜像。busybox是用C语言开发的基于GPL的开源项目,目前的稳定版本为1.28.4

宿主机器
[root@kong ~]# uname -a
Linux kong 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@kong ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@kong ~]#
下载busybox
简单可以直接使用的二进制最新的版本为1.28.1
[root@kong ~]# wget https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
--2018-05-25 04:51:20-- https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64
Resolving busybox.net (busybox.net)... 140.211.167.122
Connecting to busybox.net (busybox.net)|140.211.167.122|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1001112 (978K)
Saving to: 'busybox-x86_64'
100%[==============================================================================================>] 1,001,112 19.3KB/s in 30s
2018-05-25 04:51:57 (32.4 KB/s) - 'busybox-x86_64' saved [1001112/1001112]
[root@kong ~]#
设定busybox
[root@kong ~]# cp busybox-x86_64 /usr/local/bin/busybox
[root@kong ~]# chmod +x /usr/local/bin/busybox
[root@kong ~]# which busybox
/usr/local/bin/busybox
[root@kong ~]#
版本确认
输入busybox可以看出版本以及熟悉的linux下的工具,仔细看一遍就会理解busybox号称自己是瑞士军刀一点都不夸张,反过来说,瑞士军刀如果敢号称linux里的busybox可能会引起非议。
[root@kong ~]# busybox
BusyBox v1.28.1 (2018-02-15 14:34:02 CET) multi-call binary.
BusyBox is copyrighted by many authors between 1998-2015.
Licensed under GPLv2. See source distribution for detailed
copyright notices.
Usage: busybox [function [arguments]...]
or: busybox --list[-full]
or: busybox --install [-s] [DIR]
or: function [arguments]...
BusyBox is a multi-call binary that combines many common Unix
utilities into a single executable. Most people will create a
link to busybox for each function they wish to use and BusyBox
will act like whatever it was invoked as.
Currently defined functions:
[, [[, acpid, add-shell, addgroup, adduser, adjtimex, arch, arp, arping, ash, awk, base64, basename, beep, blkdiscard, blkid,
blockdev, bootchartd, brctl, bunzip2, bzcat, bzip2, cal, cat, chat, chattr, chgrp, chmod, chown, chpasswd, chpst, chroot, chrt,
chvt, cksum, clear, cmp, comm, conspy, cp, cpio, crond, crontab, cryptpw, cttyhack, cut, date, dc, dd, deallocvt, delgroup,
deluser, depmod, devmem, df, dhcprelay, diff, dirname, dmesg, dnsd, dnsdomainname, dos2unix, dpkg, dpkg-deb, du, dumpkmap,
dumpleases, echo, ed, egrep, eject, env, envdir, envuidgid, ether-wake, expand, expr, factor, fakeidentd, fallocate, false,
fatattr, fbset, fbsplash, fdflush, fdformat, fdisk, fgconsole, fgrep, find, findfs, flock, fold, free, freeramdisk, fsck,
fsck.minix, fsfreeze, fstrim, fsync, ftpd, ftpget, ftpput, fuser, getopt, getty, grep, groups, gunzip, gzip, halt, hd, hdparm,
head, hexdump, hexedit, hostid, hostname, httpd, hush, hwclock, i2cdetect, i2cdump, i2cget, i2cset, id, ifconfig, ifdown,
ifenslave, ifplugd, ifup, inetd, init, insmod, install, ionice
[root@kong rootfs]# docker pull scratch
Using default tag: latest
Error response from daemon: 'scratch' is a reserved name
[root@kong rootfs]#
将此Dockerfile添加一行没有实际作用的,看看scratch到底是什么
[root@kong rootfs]# vi Dockerfile
[root@kong rootfs]# cat Dockerfile
From scratch
MAINTAINER LiuMiao <liumiaocn@outlook.com>
[root@kong rootfs]#
进行构建,发现产生了一个0字节的镜像文件,也与scratch的原意相通
[root@kong rootfs]# docker build -t busyboxbase:latest .
Sending build context to Docker daemon 2.415 MB
Step 1/2 : FROM scratch
--->
Step 2/2 : MAINTAINER LiuMiao <liumiaocn@outlook.com>
---> Running in b118fd7c73a7
---> 2074dc76c09e
Removing intermediate container b118fd7c73a7
Successfully built 2074dc76c09e
[root@kong rootfs]# docker images |grep busyboxbase
busyboxbase latest 2074dc76c09e 14 seconds ago 0 B
[root@kong rootfs]#
至此,我们理解了from scratch确实不会有额外的添加,接下来我们像alpine那样添加如下两句
ADD rootfs.tar /
CMD ["/bin/sh"]
我们的Dockerfile也是几乎一样的三行
[root@kong rootfs]# cat Dockerfile
From scratch
ADD rootfs.tar /
CMD ["/bin/sh"]
[root@kong rootfs]#
这样就创建了一个1M的busybox为基础的镜像
[root@kong rootfs]# docker build -t busyboxbase:latest .
Sending build context to Docker daemon 2.415 MB
Step 1/3 : FROM scratch
--->
Step 2/3 : ADD rootfs.tar /
---> 0fbb0c8c7579
Removing intermediate container 8311e96f456c
Step 3/3 : CMD /bin/sh
---> Running in efb85c4526bf
---> 02270c80a4e4
Removing intermediate container efb85c4526bf
Successfully built 02270c80a4e4
[root@kong rootfs]# docker images |grep busyboxbase
busyboxbase latest 02270c80a4e4 9 seconds ago 1.01 MB
[root@kong rootfs]#
运行并使用
使用docker run发现此镜像所启动的容器并无异常之处
[root@kong rootfs]# docker run --rm -it busyboxbase sh
/ # hostname
b7f9e9646746
/ # uname -a
Linux b7f9e9646746 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 GNU/Linux
/ #
小结
这篇文章介绍了如何使用busybox结合from scratch机制创建docker的基础镜像以及相关原理。本文利用busybox 1.28.1版本创建了一个1.01M的可用的基础镜像,从瑞士军刀到瑞士指甲刀,你可以继续优化到若干K都是可行的,但是到了10M以下其实应该更多考虑的是后续的扩展性和功能性的因素了。但是从另外的角度考虑,集成进来了那么多功能的同时,也将这些功能的不安定因素也集成进来了。利用from scratch机制,创建小并且依赖性少的上下文环境是非常有用的。
参考文献
https://docs.docker.com/develop/develop-images/baseimages/
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对社区的支持。如果你想了解更多相关内容请查看下面相关链接 |