Docker Hub 上有非常多的高质量的官方镜像,可以在其中寻找一个最符合我们需要的镜像为基础镜像进行定制。

基础镜像一般有以下几种类型:

    (1) 应用/服务镜像:nginx、redis、mysql、httpd、php、tomcat、mongo 等;
    (2) 开发/构建镜像:openjdk、python、php、ruby、golang 等;
    (3) 操作系统 (Linux) 镜像: ubuntu、debian、centos、fedora、alpine 等。

Docker Hub 上常用的操作系统 (Linux) 镜像地址如下:

    Debian: https://hub.docker.com/_/debian
    Ubuntu: https://hub.docker.com/_/ubuntu
    Fedora:https://hub.docker.com/_/fedora
    CentOS: https://hub.docker.com/_/centos

一般情况下 Docker Hub 上的操作系统 (Linux) 镜像,能满足我们大部分的开发/应用需求,特殊情况下我们可以使用如下工具定制操作系统 (Linux) 基础镜像:

    (1) debootstrap: 是 debian/ubuntu 下的一个工具,用来构建一套基本的操作系统(根文件系统)。生成的目录符合 Linux 文件系统标准 (FHS),即包含了 /boot、/etc、/bin、/usr 等等目录,它比发行版本的 Linux 体积小很多,可以说是 “基本的操作系统”。
    (2) febootstrap: 是 fedora (或 centos) 下的一个工具,功能类似 debootstrap。
    (3) busybox: 是一个集成了多个最常用 Linux 命令和工具(如 cat、echo、grep、mount、telnet 等)的工具箱,它不是用来构建操作系统的工具,它本身就像一个迷你版的操作系统,通常会在嵌入式环境中使用它。
    (4) LFS (Linux form scratch): 是一种从网上直接下载 Linux 源码,从头编译 Linux 的安装方式。本文不做介绍。

1. debootstrap

    本文在 Ubuntu 20.04 下使用 debootstrap 制作一个 Ubuntu 镜像。

    Docker 版本: 20.10.7
    Docker Compose 版本: 2.6.1
    Debootstrap 版本:1.0.118ubuntu1.8

    1) 安装 debootstrap

        直接运行 apt 命令安装 debootstrap:

$ sudo apt install debootstrap

                Reading package lists... Done
                Building dependency tree
                Reading state information... Done
                Suggested packages:
                arch-test squid-deb-proxy-client
                The following NEW packages will be installed:
                debootstrap
                0 upgraded, 1 newly installed, 0 to remove and 53 not upgraded.
                Need to get 39.2 kB of archives.
                After this operation, 304 kB of additional disk space will be used.

                ...

            $ debootstrap --version

                debootstrap 1.0.118ubuntu1.8

        debootstrap 命令格式:

            debootstrap --arch=[平台] [发行版本代号] [目录] [镜像源地址]

        参数说明:

            --arch:指定要制作文件系统的处理器体系结构,比如 arm64、i386、amd64 等;
            发行版本代号:指 Ubuntu 的版本代号,就是运行命令 lsb_release -a 显示的 Codename ,以下是 Ubuntu 的版本列表;

版本                代号全称
                Ubuntu 22.10        Kinetic Kudu
                Ubuntu 22.04        Jammy Jellyfish
                Ubuntu 21.10        Impish Indri
                Ubuntu 21.04        Hirsute Hippo
                Ubuntu 20.10        Groovy Gorilla
                Ubuntu 20.04 LTS    Focal Fossa
                Ubuntu 19.10        Eoan Ermine
                Ubuntu 19.04        Disco Dingo
                Ubuntu 18.10        Cosmic Cuttlefish
                Ubuntu 18.04 LTS    Bionic Beaver
                Ubuntu 17.10        Artful Aardvark
                Ubuntu 17.04        Zesty Zapus
                Ubuntu 16.10        Yakkety Yak
                Ubuntu 16.04 LTS    Xenial Xerus
                Ubuntu 15.10        Wily Werewolf
                Ubuntu 15.04        Vivid Vervet
                Ubuntu 14.10        Utopic Unicorn
                Ubuntu 14.04 LTS    Trusty Tahr
                Ubuntu 13.10        Saucy Salamander
                Ubuntu 13.04        Raring Ringtail
                Ubuntu 12.10        Quantal Quetzal
                Ubuntu 12.04 LTS    Precise Pangolin
                Ubuntu 11.10        Oneiric Ocelot
                Ubuntu 11.04        Natty Narwhal
                Ubuntu 10.10        Maverick Meerkat
                Ubuntu 10.04 LTS    Lucid Lynx

                注:本文使用网易源 (http://mirrors.163.com/ubuntu/dists/),当前支持的最老版本是 Ubuntu 14.04 LTS Trusty Tahr。

            目录:本地目录,保存制作好的文件系统录;
            镜像源地址:一般指定国内的 Ubuntu 镜像源地址

    2) 制作 Ubuntu 14.04 LTS 镜像

        $ cd /home/xxx | mkdir ubuntu_14.04
        
        # 在 ubuntu_14.04 目录下生成根文件系统
        $ sudo debootstrap --arch=amd64 trusty ubuntu_14.04 http://mirrors.163.com/ubuntu

I: Retrieving InRelease
            I: Retrieving Release
            I: Retrieving Release.gpg
            I: Checking Release signature
            I: Valid Release signature (key id 790BC7277767219C42C86F933B4FE6ACC0B21F32)
            I: Retrieving Packages
            I: Validating Packages
            I: Resolving dependencies of required packages...
            I: Resolving dependencies of base packages...
            I: Checking component main on http://mirrors.163.com/ubuntu...

            ...

            I: Base system installed successfully.

            注:trusty 是版本代号(小写),它是代号全称的第一个单词。http://mirrors.163.com/ubuntu 是网易源的 ubuntu 镜像地址。

        # 打包并导入镜像
        $ sudo tar -C ubuntu_14.04/ -c . | docker import - ubuntu_local:14.04

            sha256:f8185f42652c581bdd8ef11b940a3a2bbc6af259a6888753543018c87bb2aeed

            注: tar -C ubuntu_14.04/ -c . 命令用于将 ubuntu_14.04 目录下的内容打包为 tar 文件,并输出至 stdout。通过 | 管道可以将输出内容传递给 docker import命令。-C 选项表示切换到目录,-c 选项表示打包。

                docker import 命令格式如下: docker import <URL或 - >  <镜像名称>:<标签>。 - 表示通过 | 管道接收数据。

        # 查看镜像
        $ docker images

            REPOSITORY          TAG         IMAGE ID       CREATED              SIZE
            ubuntu_local        14.04       f8185f42652c   About a minute ago   228MB
            ...

        # 创建容器
        $ docker run -it --name ubuntu-test ubuntu_local:14.04 /bin/bash

root@cca106fd6458:/# lsb_release -a
            No LSB modules are available.
            Distributor ID: Ubuntu
            Description:    Ubuntu 14.04 LTS
            Release:        14.04
            Codename:       trusty

2. febootstrap

    本文在 CentOS 7.9 下使用 febootstrap 制作一个 CentOS 镜像。

    Docker 版本: 20.10.7
    Docker Compose 版本: 2.6.1
    Febootstrap 版本:3.21

    1) 安装 febootstrap

        在 CentOS 6.x 中,可以直接运行命令 yum install -y febootstrap 安装,在 CentOS 7.x 中,默认的源中不带此包,需要下载安装包手动安装,这里下载 CentOS 6.7 下的安装包。

# 下载 rmp 包
            $ wget https://vault.centos.org/6.7/os/x86_64/Packages/febootstrap-3.21-4.el6.x86_64.rpm
            $ wget https://vault.centos.org/6.7/os/x86_64/Packages/fakechroot-2.9-24.5.el6_1.1.x86_64.rpm
            $ wget https://vault.centos.org/6.7/os/x86_64/Packages/febootstrap-supermin-helper-3.21-4.el6.x86_64.rpm
            $ wget https://vault.centos.org/6.7/os/x86_64/Packages/fakechroot-libs-2.9-24.5.el6_1.1.x86_64.rpm
            $ wget https://vault.centos.org/6.7/os/x86_64/Packages/fakeroot-1.12.2-22.2.el6.x86_64.rpm
            $ wget https://vault.centos.org/6.7/os/x86_64/Packages/fakeroot-libs-1.12.2-22.2.el6.x86_64.rpm

            注:如果 wget 无法下载,可以使用浏览器手动下载。

            # 安装 rpm 包
            $ rpm -ivh *.rpm

                warning: fakechroot-2.9-24.5.el6_1.1.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
                Preparing...                          ################################# [100%]
                Updating / installing...
                1:febootstrap-supermin-helper-3.21-################################# [ 17%]
                2:fakeroot-libs-1.12.2-22.2.el6    ################################# [ 33%]
                3:fakeroot-1.12.2-22.2.el6         ################################# [ 50%]
                4:fakechroot-libs-2.9-24.5.el6_1.1 ################################# [ 67%]
                5:fakechroot-2.9-24.5.el6_1.1      ################################# [ 83%]
                6:febootstrap-3.21-4.el6           ################################# [100%]

      
        febootstrap 命令格式:

            febootstrap  [--options] [发行版本代号] [目录] [镜像源地址]        

        参数说明:

            --options:这里使用 -i,表示需要安装的 package,如 coreutils、yum、bash、wget、vim、iputils 等。一般情况我们不清楚某个命令属于哪个 package,比如 cat 命令,我们可以通过如下方法查看:

$ which cat

                    /usr/bin/cat

                $ yum whatprovides /usr/bin/cat

                    Loaded plugins: fastestmirror
                    Loading mirror speeds from cached hostfile
                    * base: mirrors.aliyun.com
                    * extras: mirrors.aliyun.com
                    * updates: mirrors.aliyun.com
                    coreutils-8.22-24.el7.x86_64 : A set of basic GNU tools commonly used in shell scripts
                    Repo        : base
                    Matched from:
                    Filename    : /usr/bin/cat   

                    ...

                注:从命令的输出可以知道,cat 命令属于 coreutils package.               
            
            发行版本代号:指 CentOS 的版本代号。运行命令 cat /etc/centos-release 查看版本,比如版本 CentOS Linux release 7.9.2009 (Core),对应的版本代码就是 centos79;
            目录:本地目录,保存制作好的文件系统录;
            镜像源地址:一般指定国内的 CentOS 镜像源地址
      
    2) 制作 CentOS 7.9 镜像

        $ cd /home/xxx | mkdir centos_7.9
        
        # 在 centos_7.9 目录下生成根文件系统,安装 coreutils、vim 等 package
        $ sudo febootstrap -i coreutils -i vim centos79 centos_7.9 https://mirrors.163.com/centos/7/os/x86_64/

febootstrap                                                                                      | 3.6 kB  00:00:00
            (1/2): febootstrap/group_gz                                                                      | 153 kB  00:00:00
            (2/2): febootstrap/primary_db                                                                    | 6.1 MB  00:00:01
            Resolving Dependencies

            ...

            Complete!

        # 打包并导入镜像
        $ sudo tar -C centos_7.9/ -c . | docker import - centos_local:7.9

            sha256:7b8b19d4e8b80a5570f68cdefad31fa20e76a2fbb7ab9eba7a5c222b340a49da

            注: tar -C  centos_7.9/ -c . 命令用于将 centos_7.9 目录下的内容打包为 tar 文件,并输出至 stdout。通过 | 管道可以将输出内容传递给 docker import命令。-C 选项表示切换到目录,-c 选项表示打包。

                docker import 命令格式如下: docker import <URL或 - >  <镜像名称>:<标签>。 - 表示通过 | 管道接收数据。

        # 查看镜像
        $ docker images

            REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
            centos_local        7.9       7b8b19d4e8b8   41 seconds ago   299MB
            ...

        # 创建容器
        $ docker run -it --name centos-test centos_local:7.9 /bin/bash

            bash-4.2# cat /etc/centos-release
            CentOS Linux release 7.9.2009 (Core)

 

3. busybox

    本文在 Ubuntu 20.04 下,部署从 Docker Hub 下载的 busybox 官方镜像。

    Docker 版本: 20.10.7
    Docker Compose 版本: 2.6.1
    
    Busybox:https://busybox.net/
    Busybox Docker Hub: https://hub.docker.com/_/busybox

    1) 部署 busybox

        $ docker pull busybox:1.34.1

1.34.1: Pulling from library/busybox
            2123501b93d4: Pull complete
            Digest: sha256:05a79c7279f71f86a2a0d05eb72fcb56ea36139150f0a75cd87e80a4272e4e39
            Status: Downloaded newer image for busybox:1.34.1
            docker.io/library/busybox:1.34.1

        $ docker images

            REPOSITORY          TAG           IMAGE ID       CREATED         SIZE
            busybox             1.34.1        827365c7baf1   2 months ago    4.86MB

            注:busybox 镜像虽然小,但它含了大量常见的 Linux 命令。

        # 查看镜像信息
        $ docker inspect busybox:1.34.1

[
                {
                    "Id": "sha256:827365c7baf137228e94bcfc6c47938b4ffde26c68c32bf3d3a7762cd04056a5",
                    "RepoTags": [
                        "busybox:1.34.1"
                    ],

                    ...

                    "Architecture": "amd64",
                    "Os": "linux",
                    "Size": 4859010,
                    "VirtualSize": 4859010,

                    ...

                }
            ]

    2) 运行 busybox

        # 运行容器
        $ docker run -itd --rm --name busybox-test busybox:1.34.1 sh

            注:--rm 表示如果容器存在,删除存在的容器,再创建新容器。-d 表示容器再后台运行。

        # 进入容器
        $ docker exec -it busybox-test sh

/ # ls

                bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var

            / # busybox

                BusyBox v1.34.1 (2022-12-21 18:28:04 UTC) 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 --show SCRIPT
                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, ar, arch, arp, arping, ascii, ash, awk, base32, base64,
                    basename, bc, 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,

                    ...

            / # busybox ls

                bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var