您的位置:首页 > 运维架构 > Docker

基于busybox构建最小linux Docker镜像系统

2017-04-18 17:30 1361 查看
基本概念

1. Dockerfile 中可以通过FROM scratch引用一个docker内置的空镜像

2. Docker 容器的内核都是共享宿主操作系统的内核,容器启动后docker会自动在容器内建立系统目录:dev, etc, proc, run, sys 及系统文件

3. Docker 容器内是一个隔离的基于宿主系统内核的运行环境(或理解为 操作系统)。Docker 内的操作系统是以动态库、静态库、可执行程序及其他资源文件形态体现,如CentOS就是将CentOS的各种系统库、工具库及程序文件打包成Docker镜像。

4. 一个最小的能在Docker运行的程序可以通过静态编译实现,由于没有任何依赖(包括操作系统依赖,不含内核),可以直接在一个只包含该程序的Docker容器中启动起来;

  而一个动态编译的程序,通常引用了系统库和其他第三方库。其中系统库主要有:ld-linux ,libdl, libm, libc, libm。这种情况下,就必须按照linux约定将系统库及第三方库放到镜像的 /lib或/lib64目录下(现代系统通常都用64位,64位库放到/lib64下,注意 /lib64是到/usr/lib64的软链接)。如果依赖库不全,docker 容器启动的时候会报错:standard_init_linux.go:175:
exec user process caused "no such file or directory

  因为要作一个最小的系统,又要实现一个linux系统的基本功能。因此选定busybox来实现这个系统。(busybox是静态编译,没有任何依赖,无需系统库及第三方库,且1.21.1版本的busybox只有951K)。

构建过程:

1. 创建linux系统的基础目录

mkdir minos
cd minos
mkdir usr
mkdir usr/lib
mkdir usr/local
mkdir usr/lib64
mkdir usr/sbin
mkdir usr/include
mkdir usr/bin
mkdir var/
mkdir var/lib
mkdir var/run
mkdir var/local
mkdir var/log
mkdir tmp


2. 创建 /lib /lib64软链接

ln -s usr/lib lib
ln -s usr/lib64 lib64
ln -s usr/bin bin
3. 下载busybox并拷贝至 usr/bin目录

curl -O https://busybox.net/downloads/binaries/1.21.1/busybox-x86_64 mv busybox-x86_64 usr/bin/busybox


4. 生成系统指令软链接

chmod +x usr/bin/busybox
./usr/bin/busybox
BusyBox v1.21.1 (2013-07-08 11:34:59 CDT) multi-call binary.
BusyBox is copyrighted by many authors between 1998-2012.
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, arp, arping, ash, awk, base64, basename,
beep, blkid, blockdev, bootchartd, brctl, bunzip2, bzcat, bzip2, cal, cat, catv, 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, du,
dumpkmap, dumpleases, echo, ed, egrep, eject, env, envdir, envuidgid, ether-wake, expand, expr,
fakeidentd, false, fbset, fbsplash, fdflush, fdformat, fdisk, fgconsole, fgrep, find, findfs,
flock, fold, free, freeramdisk, fsck, fsck.minix, fsync, ftpd, ftpget, ftpput, fuser, getopt,
getty, grep, groups, gunzip, gzip, halt, hd, hdparm, head, hexdump, hostid, hostname, httpd,
hush, hwclock, id, ifconfig, ifdown, ifenslave, ifplugd, ifup, inetd, init, insmod, install,
ionice, iostat, ip, ipaddr, ipcalc, ipcrm, ipcs, iplink, iproute, iprule, iptunnel, kbd_mode,
kill, killall, killall5, klogd, last, less, linux32, linux64, linuxrc, ln, loadfont, loadkmap,
logger, login, logname, logread, losetup, lpd, lpq, lpr, ls, lsattr, lsmod, lsof, lspci, lsusb,
lzcat, lzma, lzop, lzopcat, 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, nmeter, nohup, nslookup, ntpd, od, openvt, passwd, 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, rev, rm, rmdir,
rmmod, route, rpm, rpm2cpio, rtcwake, run-parts, runlevel, runsv, runsvdir, rx, script,
scriptreplay, sed, sendmail, seq, setarch, setconsole, setfont, setkeycodes, setlogcons,
setserial, setsid, setuidgid, sh, sha1sum, sha256sum, sha3sum, sha512sum, showkey, slattach,
sleep, smemcap, softlimit, sort, split, start-stop-daemon, stat, strings, stty, su, sulogin,
sum, sv, svlogd, swapoff, swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, tcpsvd,
tee, telnet, telnetd, test, tftp, tftpd, time, timeout, top, touch, tr, traceroute, traceroute6,
true, tty, ttysize, tunctl, udhcpc, udhcpd, udpsvd, umount, uname, unexpand, uniq, unix2dos,
unlzma, unlzop, unxz, unzip, uptime, users, usleep, uudecode, uuencode, vconfig, vi, vlock,
volname, wall, watch, watchdog, wc, wget, which, who, whoami, whois, xargs, xz, xzcat, yes,
zcat, zcip


利用http://tool.oschina.net/regex/ 提供的在线正则,正则表达式: \s*([^,]+),?\s*, 替换文本:ln -s busybox $1;  最终得到指令如下

cd usr/bin;
ln -s  busybox [;ln -s  busybox [[;ln -s  busybox acpid;ln -s  busybox add-shell;ln -s  busybox addgroup;ln -s  busybox adduser;ln -s  busybox adjtimex;ln -s  busybox arp;ln -s  busybox arping;ln -s  busybox ash;ln -s  busybox awk;ln -s  busybox base64;ln -s  busybox basename;ln -s  busybox beep;ln -s  busybox blkid;ln -s  busybox blockdev;ln -s  busybox bootchartd;ln -s  busybox brctl;ln -s  busybox bunzip2;ln -s  busybox bzcat;ln -s  busybox bzip2;ln -s  busybox cal;ln -s  busybox cat;ln -s  busybox catv;ln -s  busybox chat;ln -s  busybox chattr;ln -s  busybox chgrp;ln -s  busybox chmod;ln -s  busybox chown;ln -s  busybox chpasswd;ln -s  busybox chpst;ln -s  busybox chroot;ln -s  busybox chrt;ln -s  busybox chvt;ln -s  busybox cksum;ln -s  busybox clear;ln -s  busybox cmp;ln -s  busybox comm;ln -s  busybox conspy;ln -s  busybox cp;ln -s  busybox cpio;ln -s  busybox crond;ln -s  busybox crontab;ln -s  busybox cryptpw;ln -s  busybox cttyhack;ln -s  busybox cut;ln -s  busybox date;ln -s  busybox dc;ln -s  busybox dd;ln -s  busybox deallocvt;ln -s  busybox delgroup;ln -s  busybox deluser;ln -s  busybox depmod;ln -s  busybox devmem;ln -s  busybox df;ln -s  busybox dhcprelay;ln -s  busybox diff;ln -s  busybox dirname;ln -s  busybox dmesg;ln -s  busybox dnsd;ln -s  busybox dnsdomainname;ln -s  busybox dos2unix;ln -s  busybox du;ln -s  busybox dumpkmap;ln -s  busybox dumpleases;ln -s  busybox echo;ln -s  busybox ed;ln -s  busybox egrep;ln -s  busybox eject;ln -s  busybox env;ln -s  busybox envdir;ln -s  busybox envuidgid;ln -s  busybox ether-wake;ln -s  busybox expand;ln -s  busybox expr;ln -s  busybox fakeidentd;ln -s  busybox false;ln -s  busybox fbset;ln -s  busybox fbsplash;ln -s  busybox fdflush;ln -s  busybox fdformat;ln -s  busybox fdisk;ln -s  busybox fgconsole;ln -s  busybox fgrep;ln -s  busybox find;ln -s  busybox findfs;ln -s  busybox flock;ln -s  busybox fold;ln -s  busybox free;ln -s  busybox freeramdisk;ln -s  busybox fsck;ln -s  busybox fsck.minix;ln -s  busybox fsync;ln -s  busybox ftpd;ln -s  busybox ftpget;ln -s  busybox ftpput;ln -s  busybox fuser;ln -s  busybox getopt;ln -s  busybox getty;ln -s  busybox grep;ln -s  busybox groups;ln -s  busybox gunzip;ln -s  busybox gzip;ln -s  busybox halt;ln -s  busybox hd;ln -s  busybox hdparm;ln -s  busybox head;ln -s  busybox hexdump;ln -s  busybox hostid;ln -s  busybox hostname;ln -s  busybox httpd;ln -s  busybox hush;ln -s  busybox hwclock;ln -s  busybox id;ln -s  busybox ifconfig;ln -s  busybox ifdown;ln -s  busybox ifenslave;ln -s  busybox ifplugd;ln -s  busybox ifup;ln -s  busybox inetd;ln -s  busybox init;ln -s  busybox insmod;ln -s  busybox install;ln -s  busybox ionice;ln -s  busybox iostat;ln -s  busybox ip;ln -s  busybox ipaddr;ln -s  busybox ipcalc;ln -s  busybox ipcrm;ln -s  busybox ipcs;ln -s  busybox iplink;ln -s  busybox iproute;ln -s  busybox iprule;ln -s  busybox iptunnel;ln -s  busybox kbd_mode;ln -s  busybox kill;ln -s  busybox killall;ln -s  busybox killall5;ln -s  busybox klogd;ln -s  busybox last;ln -s  busybox less;ln -s  busybox linux32;ln -s  busybox linux64;ln -s  busybox linuxrc;ln -s  busybox ln;ln -s  busybox loadfont;ln -s  busybox loadkmap;ln -s  busybox logger;ln -s  busybox login;ln -s  busybox logname;ln -s  busybox logread;ln -s  busybox losetup;ln -s  busybox lpd;ln -s  busybox lpq;ln -s  busybox lpr;ln -s  busybox ls;ln -s  busybox lsattr;ln -s  busybox lsmod;ln -s  busybox lsof;ln -s  busybox lspci;ln -s  busybox lsusb;ln -s  busybox lzcat;ln -s  busybox lzma;ln -s  busybox lzop;ln -s  busybox lzopcat;ln -s  busybox makedevs;ln -s  busybox makemime;ln -s  busybox man;ln -s  busybox md5sum;ln -s  busybox mdev;ln -s  busybox mesg;ln -s  busybox microcom;ln -s  busybox mkdir;ln -s  busybox mkdosfs;ln -s  busybox mke2fs;ln -s  busybox mkfifo;ln -s  busybox mkfs.ext2;ln -s  busybox mkfs.minix;ln -s  busybox mkfs.vfat;ln -s  busybox mknod;ln -s  busybox mkpasswd;ln -s  busybox mkswap;ln -s  busybox mktemp;ln -s  busybox modinfo;ln -s  busybox modprobe;ln -s  busybox more;ln -s  busybox mount;ln -s  busybox mountpoint;ln -s  busybox mpstat;ln -s  busybox mt;ln -s  busybox mv;ln -s  busybox nameif;ln -s  busybox nanddump;ln -s  busybox nandwrite;ln -s  busybox nbd-client;ln -s  busybox nc;ln -s  busybox netstat;ln -s  busybox nice;ln -s  busybox nmeter;ln -s  busybox nohup;ln -s  busybox nslookup;ln -s  busybox ntpd;ln -s  busybox od;ln -s  busybox openvt;ln -s  busybox passwd;ln -s  busybox patch;ln -s  busybox pgrep;ln -s  busybox pidof;ln -s  busybox ping;ln -s  busybox ping6;ln -s  busybox pipe_progress;ln -s  busybox pivot_root;ln -s  busybox pkill;ln -s  busybox pmap;ln -s  busybox popmaildir;ln -s  busybox poweroff;ln -s  busybox powertop;ln -s  busybox printenv;ln -s  busybox printf;ln -s  busybox ps;ln -s  busybox pscan;ln -s  busybox pstree;ln -s  busybox pwd;ln -s  busybox pwdx;ln -s  busybox raidautorun;ln -s  busybox rdate;ln -s  busybox rdev;ln -s  busybox readahead;ln -s  busybox readlink;ln -s  busybox readprofile;ln -s  busybox realpath;ln -s  busybox reboot;ln -s  busybox reformime;ln -s  busybox remove-shell;ln -s  busybox renice;ln -s  busybox reset;ln -s  busybox resize;ln -s  busybox rev;ln -s  busybox rm;ln -s  busybox rmdir;ln -s  busybox rmmod;ln -s  busybox route;ln -s  busybox rpm;ln -s  busybox rpm2cpio;ln -s  busybox rtcwake;ln -s  busybox run-parts;ln -s  busybox runlevel;ln -s  busybox runsv;ln -s  busybox runsvdir;ln -s  busybox rx;ln -s  busybox script;ln -s  busybox scriptreplay;ln -s  busybox sed;ln -s  busybox sendmail;ln -s  busybox seq;ln -s  busybox setarch;ln -s  busybox setconsole;ln -s  busybox setfont;ln -s  busybox setkeycodes;ln -s  busybox setlogcons;ln -s  busybox setserial;ln -s  busybox setsid;ln -s  busybox setuidgid;ln -s  busybox sh;ln -s  busybox sha1sum;ln -s  busybox sha256sum;ln -s  busybox sha3sum;ln -s  busybox sha512sum;ln -s  busybox showkey;ln -s  busybox slattach;ln -s  busybox sleep;ln -s  busybox smemcap;ln -s  busybox softlimit;ln -s  busybox sort;ln -s  busybox split;ln -s  busybox start-stop-daemon;ln -s  busybox stat;ln -s  busybox strings;ln -s  busybox stty;ln -s  busybox su;ln -s  busybox sulogin;ln -s  busybox sum;ln -s  busybox sv;ln -s  busybox svlogd;ln -s  busybox swapoff;ln -s  busybox swapon;ln -s  busybox switch_root;ln -s  busybox sync;ln -s  busybox sysctl;ln -s  busybox syslogd;ln -s  busybox tac;ln -s  busybox tail;ln -s  busybox tar;ln -s  busybox tcpsvd;ln -s  busybox tee;ln -s  busybox telnet;ln -s  busybox telnetd;ln -s  busybox test;ln -s  busybox tftp;ln -s  busybox tftpd;ln -s  busybox time;ln -s  busybox timeout;ln -s  busybox top;ln -s  busybox touch;ln -s  busybox tr;ln -s  busybox traceroute;ln -s  busybox traceroute6;ln -s  busybox true;ln -s  busybox tty;ln -s  busybox ttysize;ln -s  busybox tunctl;ln -s  busybox udhcpc;ln -s  busybox udhcpd;ln -s  busybox udpsvd;ln -s  busybox umount;ln -s  busybox uname;ln -s  busybox unexpand;ln -s  busybox uniq;ln -s  busybox unix2dos;ln -s  busybox unlzma;ln -s  busybox unlzop;ln -s  busybox unxz;ln -s  busybox unzip;ln -s  busybox uptime;ln -s  busybox users;ln -s  busybox usleep;ln -s  busybox uudecode;ln -s  busybox uuencode;ln -s  busybox vconfig;ln -s  busybox vi;ln -s  busybox vlock;ln -s  busybox volname;ln -s  busybox wall;ln -s  busybox watch;ln -s  busybox watchdog;ln -s  busybox wc;ln -s  busybox wget;ln -s  busybox which;ln -s  busybox who;ln -s  busybox whoami;ln -s  busybox whois;ln -s  busybox xargs;ln -s  busybox xz;ln -s  busybox xzcat;ln -s  busybox yes;ln -s  busybox zcat;ln -s  busybox zcip ;
cd -;


执行指令,建立软链接

5. 编辑Dockerfile

FROM scratch
MAINTAINER YeQiang
ADD ./ /
RUN rm /Dockerfile


6. 制作镜像

docker build -t minos .


7. 查看镜像

[yeqiang@localhost minos]$ docker images |  grep minos
minos                                latest              c43240f48fba        25 hours ago        973.4 kB


8. 测试镜像

[yeqiang@localhost minos]$ docker run --rm -it minos /bin/sh
/ # ls -l
total 20
lrwxrwxrwx    1 0        0                7 Apr 18 02:40 bin -> usr/bin
drwxr-xr-x    5 0        0              380 Apr 18 09:28 dev
drwxr-xr-x    2 0        0             4096 Apr 18 09:28 etc
lrwxrwxrwx    1 0        0                7 Apr 18 06:20 lib -> usr/lib
lrwxrwxrwx    1 0        0                9 Apr 18 02:40 lib64 -> usr/lib64
dr-xr-xr-x  288 0        0                0 Apr 18 09:28 proc
drwxr-xr-x    3 0        0             4096 Apr 18 06:22 run
dr-xr-xr-x   13 0        0                0 Apr 18 00:32 sys
drwxrwxr-x    2 0        0             4096 Apr 18 06:18 tmp
drwxr-xr-x    8 0        0             4096 Apr 18 06:22 usr
drwxrwxr-x    6 0        0             4096 Apr 18 06:22 var
/ #
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: