Docker鏡像的首行從FROM alpine之類的鏡像開始,但是最初的基礎(chǔ)鏡像是如何創(chuàng)建的,本文使用一個(gè)busybox創(chuàng)建一個(gè)基礎(chǔ)鏡像,相信在此過(guò)程中會(huì)對(duì)docker一些相關(guān)的概念有進(jìn)一步的理解。
簡(jiǎn)單來(lái)說(shuō),基礎(chǔ)鏡像就是沒有From或者FROM scratch開頭的Dockerfile所構(gòu)建出來(lái)的鏡像。比如alpine,這個(gè)很小的linux鏡像目前只有4M左右
busybox被稱為嵌入式linux的瑞士軍刀,這句話是在busybox自己介紹自己的時(shí)候提出的(The Swiss Army Knife of Embedded Linux)。busybox整合了很多小的unix下的通用功能到一個(gè)小的可執(zhí)行文件之中,簡(jiǎn)單來(lái)說(shuō)在unix或者linux下常用的那些功能在這里你都能找到,但是為了busybox的目標(biāo):嵌入式的linux,大小對(duì)于busybox來(lái)說(shuō)是非常重要的優(yōu)化要素和限制,這些功能有可能會(huì)有所閹割,但是對(duì)于一般需求來(lái)說(shuō)已經(jīng)足夠。而alpine就是在busybox基礎(chǔ)上增加了自己的包管理工具apk等功能創(chuàng)建了風(fēng)靡一時(shí)的小巧鏡像。busybox是用C語(yǔ)言開發(fā)的基于GPL的開源項(xiàng)目,目前的穩(wěn)定版本為1.28.4
輸入busybox可以看出版本以及熟悉的linux下的工具,仔細(xì)看一遍就會(huì)理解busybox號(hào)稱自己是瑞士軍刀一點(diǎn)都不夸張,反過(guò)來(lái)說(shuō),瑞士軍刀如果敢號(hào)稱linux里的busybox可能會(huì)引起非議。
[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, iostat, ip, ipaddr, ipcalc, ipcrm, ipcs, iplink, ipneigh,
iproute, iprule, iptunnel, kbd_mode, kill, killall, killall5, klogd, last, less, link, linux32, linux64, linuxrc, ln, loadfont,
loadkmap, logger, login, logname, logread, losetup, lpd, lpq, lpr, ls, lsattr, lsmod, lsof, lspci, lsscsi, lsusb, lzcat, lzma,
lzop, makedevs, makemime, man, md5sum, mdev, mesg, microcom, mkdir, mkdosfs, mke2fs, mkfifo, mkfs.ext2, mkfs.minix, mkfs.vfat,
mknod, mkpasswd, mkswap, mktemp, modinfo, modprobe, more, mount, mountpoint, mpstat, mt, mv, nameif, nanddump, nandwrite,
nbd-client, nc, netstat, nice, nl, nmeter, nohup, nproc, nsenter, nslookup, ntpd, nuke, od, openvt, partprobe, passwd, paste,
patch, pgrep, pidof, ping, ping6, pipe_progress, pivot_root, pkill, pmap, popmaildir, poweroff, powertop, printenv, printf, ps,
pscan, pstree, pwd, pwdx, raidautorun, rdate, rdev, readahead, readlink, readprofile, realpath, reboot, reformime,
remove-shell, renice, reset, resize, resume, rev, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake, run-init, run-parts,
runlevel, runsv, runsvdir, rx, script, scriptreplay, sed, sendmail, seq, setarch, setconsole, setfattr, setfont, setkeycodes,
setlogcons, setpriv, setserial, setsid, setuidgid, sh, sha1sum, sha256sum, sha3sum, sha512sum, showkey, shred, shuf, slattach,
sleep, smemcap, softlimit, sort, split, ssl_client, start-stop-daemon, stat, strings, stty, su, sulogin, sum, sv, svc, svlogd,
swapoff, swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset, tcpsvd, tee, telnet, telnetd, test, tftp, tftpd,
time, timeout, top, touch, tr, traceroute, traceroute6, true, truncate, tty, ttysize, tunctl, ubiattach, ubidetach, ubimkvol,
ubirename, ubirmvol, ubirsvol, ubiupdatevol, udhcpc, udhcpd, udpsvd, uevent, umount, uname, unexpand, uniq, unix2dos, unlink,
unlzma, unshare, unxz, unzip, uptime, users, usleep, uudecode, uuencode, vconfig, vi, vlock, volname, w, wall, watch, watchdog,
wc, wget, which, who, whoami, whois, xargs, xxd, xz, xzcat, yes, zcat, zcip
[root@kong ~]
這張圖在鏡像基礎(chǔ)介紹的時(shí)候已經(jīng)被無(wú)數(shù)次看到了,rootfs是linux中重要的概念,而alpine中也有ADD rootfs.tar.xz這樣一句,接下來(lái)我們將了解一下如何生成一個(gè)簡(jiǎn)單的rootfs
此處注意相對(duì)路徑,而后續(xù)次相對(duì)路徑會(huì)展開至/下,從而創(chuàng)建新的系統(tǒng)的rootfs,這也是從零搭建linux(linux from scratch)的重要操作之一。
創(chuàng)建base鏡像,由于沒有發(fā)現(xiàn)具體的內(nèi)容,所以未創(chuàng)建出具體鏡像。另外,本文為了演示方便,直接在此處創(chuàng)建Dockerfile,這并不是一個(gè)好主意,實(shí)際的時(shí)候請(qǐng)不要這樣做,如果當(dāng)前目錄下有100G的文件,就會(huì)無(wú)比緩慢,而且也不規(guī)范,無(wú)關(guān)物品需要清場(chǎng)。
這篇文章介紹了如何使用busybox結(jié)合from scratch機(jī)制創(chuàng)建docker的基礎(chǔ)鏡像以及相關(guān)原理。本文利用busybox 1.28.1版本創(chuàng)建了一個(gè)1.01M的可用的基礎(chǔ)鏡像,從瑞士軍刀到瑞士指甲刀,你可以繼續(xù)優(yōu)化到若干K都是可行的,但是到了10M以下其實(shí)應(yīng)該更多考慮的是后續(xù)的擴(kuò)展性和功能性的因素了。但是從另外的角度考慮,集成進(jìn)來(lái)了那么多功能的同時(shí),也將這些功能的不安定因素也集成進(jìn)來(lái)了。利用from scratch機(jī)制,創(chuàng)建小并且依賴性少的上下文環(huán)境是非常有用的。
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接