The first line of a Docker image starts with an image such as FROM alpine, but how is the initial base image created? This article uses a busybox to create a base image. I believe that in this process, you will have a further understanding of some Docker-related concepts. What is a base image? Simply put, a base image is an image built by a Dockerfile that does not start with From or FROM scratch. For example, alpine, this very small Linux image is only about 4M [root@kong ~]# docker images |grep alpine docker.io/alpine-latest 3fd9065eaf02 4 months ago 4.15 MB [root@kong ~]# Its Dockerfile is very simple, with only three lines, which is a basic image. FROM scratch ADD rootfs.tar.xz / CMD ["/bin/sh"] In the next article we will create our own base image like alpine. busybox Summary Busybox is called the Swiss Army Knife of Embedded Linux. This sentence was put forward when busybox introduced itself (The Swiss Army Knife of Embedded Linux). busybox integrates many small common functions under Unix into a small executable file. In short, you can find all the commonly used functions under Unix or Linux here, but for the goal of busybox: embedded Linux, size is a very important optimization factor and limitation for busybox. These functions may be castrated, but they are sufficient for general needs. Alpine adds its own package management tool apk and other functions based on busybox to create a small and popular image. Busybox is an open source project developed in C language based on GPL. The current stable version is 1.28.4 Host Machine [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 ~]# Download busybox The latest binary version is 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 ~]# Setting up 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 ~]# Version confirmation Type busybox to see the version and familiar Linux tools. After a careful look, you will understand that it is not an exaggeration for busybox to call itself the Swiss Army Knife. On the contrary, if the Swiss Army Knife dares to call itself the busybox in Linux, it may cause controversy. [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 ~] Create rootfs This picture has been seen countless times when introducing the basics of mirroring. Rootfs is an important concept in Linux, and there is also a sentence like ADD rootfs.tar.xz in alpine. Next, we will learn how to generate a simple rootfs Create a directory and enter [root@kong ~]# mkdir rootfs [root@kong ~]# cd rootfs/ [root@kong rootfs]# Create rootfs Execute the following statement for module in `busybox --list-modules` do mkdir -p `dirname "$module"` ln -sf /bin/busybox "$module" done Execution log [root@kong rootfs]# for module in `busybox --list-modules` > do > mkdir -p `dirname "$module"` > ln -sf /bin/busybox "$module" > done [root@kong rootfs]# Result Confirmation [root@kong rootfs]# ls bin linuxrc sbin usr [root@kong rootfs]# find . -type d . ./usr ./usr/bin ./usr/sbin ./sbin ./bin [root@kong rootfs]# Copy busybox to the newly created ./bin directory In this way, the link object of the above command exists [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]# Create rootfs.tar Note the relative path here, and the subsequent relative paths will be expanded to /, thereby creating the rootfs of the new system. This is also one of the important operations of building Linux from scratch. [root@kong rootfs]# tar cpf rootfs.tar . tar: ./rootfs.tar: file is the archive; not dumped [root@kong rootfs]#
Prepare Dockerfile Prepare a one-line Dockerfile [root@kong rootfs]# vi Dockerfile [root@kong rootfs]# cat Dockerfile From scratch [root@kong rootfs]# Create a base image. Since no specific content was found, no specific image was created. In addition, for the convenience of demonstration, this article directly creates a Dockerfile here, which is not a good idea. Please do not do this in practice. If there are 100G files in the current directory, it will be extremely slow and non-standard, and irrelevant items need to be cleared. [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 There is From. Can this scratch be pulled? The current version has reserved it as a name. [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]# Add a line to this Dockerfile that has no practical effect to see what scratch is [root@kong rootfs]# vi Dockerfile [root@kong rootfs]# cat Dockerfile From scratch MAINTAINER LiuMiao <[email protected]> [root@kong rootfs]# After building, it was found that a 0-byte image file was generated, which is consistent with the original meaning of 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 <[email protected]> ---> 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]# At this point, we understand that from scratch does not add anything extra. Next, we add the following two sentences like alpine ADD rootfs.tar / CMD ["/bin/sh"] Our Dockerfile has almost the same three lines [root@kong rootfs]# cat Dockerfile From scratch ADD rootfs.tar / CMD ["/bin/sh"] [root@kong rootfs]# This creates a 1M busybox-based image [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]# Run and use Using docker run, it is found that there is nothing unusual in the container started by this image [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 / # summary This article introduces how to use busybox combined with the from scratch mechanism to create a basic docker image and related principles. This article uses busybox version 1.28.1 to create a usable basic image of 1.01M. From Swiss Army Knife to Swiss Nail Clipper, you can continue to optimize to several K. However, when it is below 10M, you should consider the subsequent scalability and functionality factors. But from another perspective, while integrating so many functions, the unstable factors of these functions are also integrated. Using the from scratch mechanism, it is very useful to create small context environments with few dependencies. References https://docs.docker.com/develop/develop-images/baseimages/ Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links You may also be interested in:
|
<<: Vue based on Element button permission implementation solution
>>: Detailed explanation of MySQL master-slave replication read-write separation construction
When the page is not responding, displaying the l...
When the carriage return character ( Ctrl+M ) mak...
Preface HTTP and HTTPS In our daily life, common ...
When installing in MySQL 8.0.16, some errors may ...
Introduction to CentOS CentOS is an enterprise-cl...
Preface Bootstrap, the most popular front-end dev...
1. First, create a hello-world.cpp file The progr...
Create a new table CREATE TABLE `person` ( `id` i...
This article example shares the specific code of ...
Table of contents Install Redis on Docker 1. Find...
This article shares the specific code of JavaScri...
This article uses an example to describe how to c...
Table of contents 1. Three functions of toString ...
The image integration technology used by American...
This time I will talk about the skills of develop...