Docker如何使用busybox創(chuàng)建基礎(chǔ)鏡像

小編給大家分享一下Docker如何使用busybox創(chuàng)建基礎(chǔ)鏡像,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、網(wǎng)站建設(shè)、新絳網(wǎng)絡(luò)推廣、微信小程序開發(fā)、新絳網(wǎng)絡(luò)營銷、新絳企業(yè)策劃、新絳品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供新絳建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:bm7419.com

Docker鏡像的首行從FROM alpine之類的鏡像開始,但是最初的基礎(chǔ)鏡像是如何創(chuàng)建的,本文使用一個(gè)busybox創(chuàng)建一個(gè)基礎(chǔ)鏡像,相信在此過程中會(huì)對(duì)docker一些相關(guān)的概念有進(jìn)一步的理解。

什么是基礎(chǔ)鏡像(base image)

簡單來說,基礎(chǔ)鏡像就是沒有From或者FROM scratch開頭的Dockerfile所構(gòu)建出來的鏡像。比如alpine,這個(gè)很小的linux鏡像目前只有4M左右

[root@kong ~]# docker images |grep alpine
docker.io/alpine        latest       3fd9065eaf02    4 months ago    4.15 MB
[root@kong ~]#

它的Dockerfile很簡單,只有三行,這就是一個(gè)基礎(chǔ)鏡像,

FROM scratch
ADD rootfs.tar.xz /
CMD ["/bin/sh"]

在接下來的文章中我們將會(huì)像alpine那樣來創(chuàng)建一個(gè)自己的基礎(chǔ)鏡像。

busybox

概要說明

busybox被稱為嵌入式linux的瑞士軍刀,這句話是在busybox自己介紹自己的時(shí)候提出的(The Swiss Army Knife of Embedded Linux)。busybox整合了很多小的unix下的通用功能到一個(gè)小的可執(zhí)行文件之中,簡單來說在unix或者linux下常用的那些功能在這里你都能找到,但是為了busybox的目標(biāo):嵌入式的linux,大小對(duì)于busybox來說是非常重要的優(yōu)化要素和限制,這些功能有可能會(huì)有所閹割,但是對(duì)于一般需求來說已經(jīng)足夠。而alpine就是在busybox基礎(chǔ)上增加了自己的包管理工具apk等功能創(chuàng)建了風(fēng)靡一時(shí)的小巧鏡像。busybox是用C語言開發(fā)的基于GPL的開源項(xiàng)目,目前的穩(wěn)定版本為1.28.4

Docker如何使用busybox創(chuàng)建基礎(chǔ)鏡像

宿主機(jī)器

[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

簡單可以直接使用的二進(jìn)制最新的版本為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 ~]#

設(shè)定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 ~]#

版本確認(rèn)

輸入busybox可以看出版本以及熟悉的linux下的工具,仔細(xì)看一遍就會(huì)理解busybox號(hào)稱自己是瑞士軍刀一點(diǎn)都不夸張,反過來說,瑞士軍刀如果敢號(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 ~]

創(chuàng)建rootfs

這張圖在鏡像基礎(chǔ)介紹的時(shí)候已經(jīng)被無數(shù)次看到了,rootfs是linux中重要的概念,而alpine中也有ADD rootfs.tar.xz這樣一句,接下來我們將了解一下如何生成一個(gè)簡單的rootfs

Docker如何使用busybox創(chuàng)建基礎(chǔ)鏡像

創(chuàng)建目錄并進(jìn)入

[root@kong ~]# mkdir rootfs
[root@kong ~]# cd rootfs/
[root@kong rootfs]#

創(chuàng)建rootfs

執(zhí)行如下語句

for module in `busybox --list-modules`
do
 mkdir -p `dirname "$module"`
 ln -sf /bin/busybox "$module"
done

執(zhí)行日志

[root@kong rootfs]# for module in `busybox --list-modules`
> do
>  mkdir -p `dirname "$module"`
>  ln -sf /bin/busybox "$module"
> done
[root@kong rootfs]#

結(jié)果確認(rèn)

[root@kong rootfs]# ls
bin linuxrc sbin usr
[root@kong rootfs]# find . -type d
.
./usr
./usr/bin
./usr/sbin
./sbin
./bin
[root@kong rootfs]#

將busybox拷貝至新創(chuàng)建的./bin目錄下

這樣,上述命令的鏈接對(duì)象就存在了

[root@kong rootfs]# cp /usr/local/bin/busybox bin/
[root@kong rootfs]# ls -l bin/busybox
-rwxr-xr-x. 1 root root 1001112 May 25 05:27 bin/busybox
[root@kong rootfs]#

創(chuàng)建rootfs.tar

此處注意相對(duì)路徑,而后續(xù)次相對(duì)路徑會(huì)展開至/下,從而創(chuàng)建新的系統(tǒng)的rootfs,這也是從零搭建linux(linux from scratch)的重要操作之一。

[root@kong rootfs]# tar cpf rootfs.tar .
tar: ./rootfs.tar: file is the archive; not dumped
[root@kong rootfs]#

簡單說明:busybox –list-modules列出了busybox的所有模塊,然后以此為基礎(chǔ),創(chuàng)建了一個(gè)小型的rootfs
[root@kong rootfs]# busybox –list-modules |wc -l
389
[root@kong rootfs]#

準(zhǔn)備Dockerfile

準(zhǔn)備一個(gè)一行的Dockerfile

[root@kong rootfs]# vi Dockerfile
[root@kong rootfs]# cat Dockerfile 
From scratch
[root@kong rootfs]#

創(chuàng)建base鏡像,由于沒有發(fā)現(xiàn)具體的內(nèi)容,所以未創(chuàng)建出具體鏡像。另外,本文為了演示方便,直接在此處創(chuàng)建Dockerfile,這并不是一個(gè)好主意,實(shí)際的時(shí)候請(qǐng)不要這樣做,如果當(dāng)前目錄下有100G的文件,就會(huì)無比緩慢,而且也不規(guī)范,無關(guān)物品需要清場(chǎng)。

[root@kong rootfs]# docker build -t busyboxbase:latest .
Sending build context to Docker daemon 2.415 MB
Step 1/1 : FROM scratch
 ---> 
No image was generated. Is your Dockerfile empty?
[root@kong rootfs]# docker images |grep busyboxbase
[root@kong rootfs]#

from scracth

有From,這個(gè)scratch可以pull么,目前的版本已經(jīng)將其作為一個(gè)保留名稱

[root@kong rootfs]# docker search scratch |grep 'an explicitly empty'
docker.io  docker.io/scratch              an explicitly empty image, especially for ...  407    [OK]    
[root@kong rootfs]# docker pull scratch
Using default tag: latest
Error response from daemon: 'scratch' is a reserved name
[root@kong rootfs]#

將此Dockerfile添加一行沒有實(shí)際作用的,看看scratch到底是什么

[root@kong rootfs]# vi Dockerfile 
[root@kong rootfs]# cat Dockerfile 
From scratch
MAINTAINER LiuMiao <liumiaocn@outlook.com>
[root@kong rootfs]#

進(jìn)行構(gòu)建,發(fā)現(xiàn)產(chǎn)生了一個(gè)0字節(jié)的鏡像文件,也與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確實(shí)不會(huì)有額外的添加,接下來我們像alpine那樣添加如下兩句

ADD rootfs.tar /
CMD ["/bin/sh"]

我們的Dockerfile也是幾乎一樣的三行

[root@kong rootfs]# cat Dockerfile 
From scratch
ADD rootfs.tar /
CMD ["/bin/sh"]
[root@kong rootfs]#

這樣就創(chuàng)建了一個(gè)1M的busybox為基礎(chǔ)的鏡像

[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]#

運(yùn)行并使用

使用docker run發(fā)現(xiàn)此鏡像所啟動(dòng)的容器并無異常之處

[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
/ #

以上是“Docker如何使用busybox創(chuàng)建基礎(chǔ)鏡像”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章題目:Docker如何使用busybox創(chuàng)建基礎(chǔ)鏡像
URL標(biāo)題:http://bm7419.com/article6/goedig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)網(wǎng)站導(dǎo)航、商城網(wǎng)站、手機(jī)網(wǎng)站建設(shè)網(wǎng)站改版、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化