概述
Docker鏡像的首行從FROM alpine之類的鏡像開始,但是最初的基礎鏡像是如何創建的,本文使用一個busybox創建一個基礎鏡像,相信在此過程中會對docker一些相關的概念有進一步的理解。
什么是基礎鏡像(ba image)簡單來說,基礎鏡像就是沒有From或者FROM scratch開頭的Dockerfile所構建出來的鏡像。比如alpine,這個很小的linux鏡像目前只有4M左右
[root@kong ~]# docker images |grep alpinedocker.io/alpine latest 3fd9065eaf02 4 months ago 4.15 MB[root@kong ~]#
它的Dockerfile很簡單,只有三行,這就是一個基礎鏡像,
在接下來的文章中我們將會像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 -aLinux 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-relea CentOS Linux relea 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_64Resolving busybox.net (busybox.net)... 140.211.167.122Connecting to busybox.net (busybox.net)|140.211.167.122|:443... connected.HTTP request nt, awaiting respon... 200 OKLength: 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版本確認
輸入busybox可以看出版本以及熟悉的linux下的工具,仔細看一遍就會理解busybox號稱自己是瑞士軍刀一點都不夸張,反過來說,瑞士軍刀如果敢號稱linux里的busybox可能會引起非議。
[root@kong ~]# busyboxBusyBox v1.28.1 (2018-02-15 14:34:02 CET) multi-call binary.BusyBox is copyrighted by many authors between 1998-2015.Licend under GPLv2. See source distribution for detailedcopyright 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 u and BusyBox will act like whatever it was invoked as.Currently defined functions: [, [[, acpid, add-shell, addgroup, addur, adjtimex, arch, arp, arping, ash, awk, ba64, baname, 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, delur, depmod, devmem, df, dhcprelay, diff, dirname, dmesg, dnsd, dnsdomainname, dos2unix, dpkg, dpkg-deb, du, dumpkmap, dumpleas, echo, ed, egrep, eject, env, envdir, envuidgid, ether-wake, expand, expr, factor, fakeidentd, fallocate, fal, fatattr, fbt, fbsplash, fdflush, fdformat, fdisk, fgconsole, fgrep, find, findfs, flock, fold, free, freeramdisk, fsck, fsck.minix, fsfreeze, fstrim, fsync, ftpd, ftpget, ftpput, fur, getopt, getty, grep, groups, gunzip, gzip, halt, hd, hdparm, head, hexdump, hexedit, hostid, hostname, httpd, hush, hwclock, i2cdetect, i2cdump, i2cget, i2ct, 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, lotup, 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, nnter, 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, ret, resize, resume, rev, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake, run-init, run-parts, runlevel, runsv, runsvdir, rx, script, scriptreplay, d, ndmail, q, tarch, tconsole, tfattr, tfont, tkeycodes, tlogcons, tpriv, trial, tsid, tuidgid, 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, taskt, 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, urs, 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 ~]創建rootfs
這張圖在鏡像基礎介紹的時候已經被無數次看到了,rootfs是linux中重要的概念,而alpine中也有ADD rootfs.tar.xz這樣一句,接下來我們將了解一下如何生成一個簡單的rootfs
創建目錄并進入創建rootfs執行如下語句
執行日志
結果確認
將busybox拷貝至新創建的./bin目錄下這樣,上述命令的鏈接對象就存在了
創建rootfs.tar此處注意相對路徑,而后續次相對路徑會展開至/下,從而創建新的系統的rootfs,這也是從零搭建linux(linux from scratch)的重要操作之一。
簡單說明:busybox –list-modules列出了busybox的所有模塊,然后以此為基礎,創建了一個小型的rootfs
[root@kong rootfs]# busybox –list-modules |wc -l
389
[root@kong rootfs]#
準備Dockerfile準備一個一行的Dockerfile
創建ba鏡像,由于沒有發現具體的內容,所以未創建出具體鏡像。另外,本文為了演示方便,直接在此處創建Dockerfile,這并不是一個好主意,實際的時候請不要這樣做,如果當前目錄下有100G的文件,就會無比緩慢,而且也不規范,無關物品需要清場。
from scracth有From,這個scratch可以pull么,目前的版本已經將其作為一個保留名稱
[root@kong rootfs]# docker arch scratch |grep 'an explicitly empty'docker.io docker.io/scratch an explicitly empty image, especially for ... 407 [OK] [root@kong rootfs]# docker pull scratchUsing default tag: latestError respon from daemon: 'scratch' is a rerved name[root@kong rootfs]#
將此Dockerfile添加一行沒有實際作用的,看看scratch到底是什么
進行構建,發現產生了一個0字節的鏡像文件,也與scratch的原意相通
[root@kong rootfs]# docker build -t busyboxba:latest .Sending build context to Docker daemon 2.415 MBStep 1/2 : FROM scratch ---> Step 2/2 : MAINTAINER LiuMiao <liumiaocn@outlook.com> ---> Running in b118fd7c73a7 ---> 2074dc76c09eRemoving intermediate container b118fd7c73a7Successfully built 2074dc76c09e[root@kong rootfs]# docker images |grep busyboxbabusyboxba latest 2074dc76c09e 14 conds ago 0 B[root@kong rootfs]#
至此,我們理解了from scratch確實不會有額外的添加,接下來我們像alpine那樣添加如下兩句
我們的Dockerfile也是幾乎一樣的三行
這樣就創建了一個1M的busybox為基礎的鏡像
運行并使用使用docker run發現此鏡像所啟動的容器并無異常之處
[root@kong rootfs]# docker run --rm -it busyboxba sh/ # hostnameb7f9e9646746/ # uname -aLinux 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機制,創建小并且依賴性少的上下文環境是非常有用的。
后面小編會分享更多這方面的干貨,感興趣的朋友走一波關注哩~
本文發布于:2023-02-28 21:04:00,感謝您對本站的認可!
本文鏈接:http://m.newhan.cn/zhishi/a/167772268298392.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:busybox怎么用(busybox怎么用TFTP傳送文件).doc
本文 PDF 下載地址:busybox怎么用(busybox怎么用TFTP傳送文件).pdf
| 留言與評論(共有 0 條評論) |