docker技术入门与实战 第3版学习笔记之第9章

158 阅读14分钟

第9章 操作系统

目前常用的Linux发行版主要包括Debian/Ubuntu系列和CentOS/.Fedora系列。前者以自带软件包版本较新而出名;后者则宣称运行更稳定一些。选择哪个操作系统取决于读者的具体需求。

BusyBox

BusyBox是一个集成了一百多个最常用Linux命令(如cat、echo、grep、mount.、telnet等)的精简工具箱,它只有不到2MB大小,被誉为“Linux 系统的瑞士军刀”。BusyBox可运行于多款POSX环境的操作系统中,如Linux(包括Android)、Hurd、FreeBSD等。

获取官方镜像

在docker hub中搜索busybox的相关镜像

 17286@LAPTOP-IV46MOMH  ~  docker search busybox
NAME                                DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
busybox                             Busybox base image.                              3153      [OK]
rancher/busybox                                                                      0
openebs/busybox-client                                                               1
antrea/busybox                                                                       1
hugegraph/busybox                   test image                                       2
privatebin/chown                     Docker image providing busybox' chown, stat…   1
yauritux/busybox-curl               Busybox with CURL                                25
radial/busyboxplus                  Full-chain, Internet enabled, busybox made f…   56                   [OK]
vukomir/busybox                     busybox and curl                                 1
arm64v8/busybox                     Busybox base image.                              5
odise/busybox-curl                                                                   4                    [OK]
busybox42/zimbra-docker-centos      A Zimbra Docker image, based in ZCS 8.8.9 an…   2                    [OK]
amd64/busybox                       Busybox base image.                              1
joeshaw/busybox-nonroot             Busybox container with non-root user nobody      2
busybox42/alpine-pod                                                                 0
p7ppc64/busybox                     Busybox base image for ppc64.                    2
ppc64le/busybox                     Busybox base image.                              1
s390x/busybox                       Busybox base image.                              3
arm32v7/busybox                     Busybox base image.                              10
i386/busybox                        Busybox base image.                              3
prom/busybox                        Prometheus Busybox Docker base images            2                    [OK]
spotify/busybox                     Spotify fork of https://hub.docker.com/_/bus…   1
busybox42/nginx_php-docker-centos   This is a nginx/php-fpm server running on Ce…   1                    [OK]

最受欢迎的带有OFFICIAL标记,说明是官方镜像,可以通过docker pull下载镜像,这个工具只有不盗2M

 17286@LAPTOP-IV46MOMH  ~  docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview busybox
 17286@LAPTOP-IV46MOMH  ~  docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
busybox          latest    beae173ccac6   23 months ago   1.24MB

运行busybox

 17286@LAPTOP-IV46MOMH  ~  docker run -it busybox
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # grep
BusyBox v1.34.1 (2021-12-29 21:12:15 UTC) multi-call binary.

Usage: grep [-HhnlLoqvsrRiwFE] [-m N] [-A|B|C N] { PATTERN | -e PATTERN... | -f FILE... } [FILE]...

Search for PATTERN in FILEs (or stdin)

        -H      Add 'filename:' prefix
        -h      Do not add 'filename:' prefix
        -n      Add 'line_no:' prefix
        -l      Show only names of files that match
        -L      Show only names of files that don't match
        -c      Show only count of matching lines
        -o      Show only the matching part of line
        -q      Quiet. Return 0 if PATTERN is found, 1 otherwise
        -v      Select non-matching lines
        -s      Suppress open and read errors
        -r      Recurse
        -R      Recurse and dereference symlinks
        -i      Ignore case
        -w      Match whole words only
        -x      Match whole lines only
        -F      PATTERN is a literal (not regexp)
        -E      PATTERN is an extended regexp
        -m N    Match up to N times per file
        -A N    Print N lines of trailing context
        -B N    Print N lines of leading context
        -C N    Same as '-A N -B N'
        -e PTRN Pattern to match
        -f FILE Read pattern from file
/ # mount
overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/ZQH2DBSTWJACPI42EF4GLBD4AD:/var/lib/docker/overlay2/l/WMXLAUXFB64SK43U4R2HPF6BNV,upperdir=/var/lib/docker/overlay2/2fb111a26b86e18515d68f3e259cd373555becd55445645a012e4acbbff26817/diff,workdir=/var/lib/docker/overlay2/2fb111a26b86e18515d68f3e259cd373555becd55445645a012e4acbbff26817/work)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,nosuid,size=65536k,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,relatime,mode=755)
cpuset on /sys/fs/cgroup/cpuset type cgroup (ro,nosuid,nodev,noexec,relatime,cpuset)
cpu on /sys/fs/cgroup/cpu type cgroup (ro,nosuid,nodev,noexec,relatime,cpu)
cpuacct on /sys/fs/cgroup/cpuacct type cgroup (ro,nosuid,nodev,noexec,relatime,cpuacct)
blkio on /sys/fs/cgroup/blkio type cgroup (ro,nosuid,nodev,noexec,relatime,blkio)
memory on /sys/fs/cgroup/memory type cgroup (ro,nosuid,nodev,noexec,relatime,memory)
devices on /sys/fs/cgroup/devices type cgroup (ro,nosuid,nodev,noexec,relatime,devices)
freezer on /sys/fs/cgroup/freezer type cgroup (ro,nosuid,nodev,noexec,relatime,freezer)
net_cls on /sys/fs/cgroup/net_cls type cgroup (ro,nosuid,nodev,noexec,relatime,net_cls)
perf_event on /sys/fs/cgroup/perf_event type cgroup (ro,nosuid,nodev,noexec,relatime,perf_event)
net_prio on /sys/fs/cgroup/net_prio type cgroup (ro,nosuid,nodev,noexec,relatime,net_prio)
hugetlb on /sys/fs/cgroup/hugetlb type cgroup (ro,nosuid,nodev,noexec,relatime,hugetlb)
pids on /sys/fs/cgroup/pids type cgroup (ro,nosuid,nodev,noexec,relatime,pids)
rdma on /sys/fs/cgroup/rdma type cgroup (ro,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/systemd type cgroup (ro,nosuid,nodev,noexec,relatime,name=systemd)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
/dev/sdc on /etc/resolv.conf type ext4 (rw,relatime,discard,errors=remount-ro,data=ordered)
/dev/sdc on /etc/hostname type ext4 (rw,relatime,discard,errors=remount-ro,data=ordered)
/dev/sdc on /etc/hosts type ext4 (rw,relatime,discard,errors=remount-ro,data=ordered)
devpts on /dev/console type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
proc on /proc/bus type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/fs type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/irq type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/sys type proc (ro,nosuid,nodev,noexec,relatime)
tmpfs on /proc/acpi type tmpfs (ro,relatime)
tmpfs on /proc/kcore type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/keys type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/timer_list type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/sched_debug type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /sys/firmware type tmpfs (ro,relatime)
/ #

查看容器内的挂载信息是mount,镜像内包含了大量的Linux命令

相关资源

BusyBox的相关资源如下:

Alpine

简介

Alpine操作系统是一个面向安全的轻型Linux发行版,关注安全、性能和资源效能。Alpine用了musllibcBusyBox以减小系统的体积和运行时资源消耗,比BusyBox功能上更完善,同时提供了apk包管理工具

Alpine Docker镜像继承了Alpine Linux发行版的这些优势。相比于其他镜像,它的容量非常小,仅仅只有5MB左右(Ubuntu系列镜像接近200MB)。官方镜像来自docker-alpine项目。

目前Docker官方推荐使用Alpine作为默认的基础镜像环境,这可以带来多个优势,如镜像下载速度加快、镜像安全性提高、主机之间的切换更方便、占用更少磁盘空间等。

获取并使用

由于镜像很小,因此可以直接使用docker run运行Alpine容器

在教程中使用time命令可以看到运行时间,但是win上没有,需要使用另一个命令

PS C:\Users\fe> docker run alpine echo '你好'
你好
PS C:\Users\fe> Measure-Command {start-process  docker run alpine echo '你好' -Wait}
Start-Process: A positional parameter cannot be found that accepts argument 'alpine'.

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 9
Ticks             : 94968
TotalDays         : 1.09916666666667E-07
TotalHours        : 2.638E-06
TotalMinutes      : 0.00015828
TotalSeconds      : 0.0094968
TotalMilliseconds : 9.4968

Windows中什么命令类似于Unix系统的"time"命令? - 知乎 (zhihu.com)

迁移至Alpine基础镜像

目前,大部分 Docker 官方镜像都已经支持 Alpine 作为基础镜像,可以很容易进行迁移。如果使用 Alpine 镜像,安装软件包时可以使用apk工具,例如:

apk add --no-cache 包名

Alpine中软件安装包的名字可能会与其他发行版有所不同,可以在``网站搜索并确定安装包名称。如果需要的安装包不在主索引内,但是在测试或社区索引中。那么首先需要更新仓库列表,如下所示。

echo "http://d1-4.alpinelinux.org/alpine/edge/testing">>/etc/apk/repositories
apk --update add --no-cache <package>

相关资源

Apline的相关资源如下:

Debian/Ubuntu

Debian和Ubuntu都是目前较为流行的Debian系的服务器操作系统,十分适合研发场景。Docker Hub上提供了它们的官方镜像,国内各大容器云服务都提供了完整的支持。

Debian 系统简介及官方镜像使用

Debian 是由 GPL 和其他自由软件许可协议授权的自由软件组成的操作系统,由 「Debian 计划(Debian Project)」 组织维护。「Debian 计划」 是一个独立的、分散的组织,由 3000 人志愿者组成,接受世界多个非盈利组织的资金支持,Software in the Public Interest 提供支持并持有商标作为保护机构。Debian 以其坚守 Unix 和自由软件的精神,以及其给予用户的众多选择而闻名。现时 Debian 包括了超过 25,000 个软件包并支持 12 个计算机系统结构。

Debian 作为一个大的系统组织框架,其下有多种不同操作系统核心的分支计划,主要为采用 Linux 核心的 Debian GNU/Linux 系统,其他还有采用 GNU Hurd 核心的 Debian GNU/Hurd 系统、采用 FreeBSD 核心的 Debian GNU/kFreeBSD 系统,以及采用 NetBSD 核心的 Debian GNU/NetBSD 系统。甚至还有利用 Debian 的系统架构和工具,采用 OpenSolaris 核心构建而成的 Nexenta OS 系统。在这些 Debian 系统中,以采用 Linux 核心的 Debian GNU/Linux 最为著名。

众多的 Linux 发行版,例如 UbuntuKnoppixLinspireXandros 等,都基于 Debian GNU/Linux

使用docker run 命令直接运行

PS C:\Users\fe> docker run -it debian bash
Unable to find image 'debian:latest' locally
latest: Pulling from library/debian
0e29546d541c: Pull complete
Digest: sha256:2906804d2a64e8a13a434a1a127fe3f6a28bf7cf3696be4223b06276f32f1f2d
Status: Downloaded newer image for debian:latest
root@7ea3e80ba8e1:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Ubuntu 系统简介及官方镜像使用

Ubuntu 是一个以桌面应用为主的 GNU/Linux 操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词(官方译名“友帮拓”,另有“吾帮托”、“乌班图”、“有奔头”或“乌斑兔”等译名)。Ubuntu 意思是“人性”以及“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想。 Ubuntu 基于 Debian 发行版和 GNOME/Unity 桌面环境,与 Debian 的不同在于它每 6 个月会发布一个新版本,每 2 年推出一个长期支持 「(Long Term Support,LTS)」 版本,一般支持 3 年时间

下面以 ubuntu:18.04 为例,演示如何使用该镜像安装一些常用软件。

首先使用 -ti 参数启动容器,登录 bash,查看 ubuntu 的发行版本号。

PS C:\Users\fe> docker run -ti ubuntu:18.04 /bin/bash
Unable to find image 'ubuntu:18.04' locally
18.04: Pulling from library/ubuntu
284055322776: Pull complete
Digest: sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6
Status: Downloaded newer image for ubuntu:18.04
root@e7fafbe40978:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

执行apt-get update命令更新仓库信息,更新之后就可以正常使用apt-get命令了

root@e7fafbe40978:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [83.3 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [3373 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [3785 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2411 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [1728 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [30.8 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [64.0 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [20.6 kB]
Get:16 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [23.8 kB]
Get:17 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [1688 kB]
Get:18 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1637 kB]
Fetched 28.1 MB in 35s (796 kB/s)
Reading package lists... Done
安装 curl 工具

中间出现询问直接回车

image-20231206101523-7jun91y.png

image

root@e7fafbe40978:/# apt-get install curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  ca-certificates krb5-locales libasn1-8-heimdal libcurl4 libgssapi-krb5-2 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal
  libheimntlm0-heimdal libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libldap-common
  libnghttp2-14 libpsl5 libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libsqlite3-0 libssl1.1 libwind0-heimdal openssl
  publicsuffix
Suggested packages:
  krb5-doc krb5-user libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal libsasl2-modules-ldap libsasl2-modules-otp libsasl2-modules-sql
The following NEW packages will be installed:
  ca-certificates curl krb5-locales libasn1-8-heimdal libcurl4 libgssapi-krb5-2 libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal
  libheimntlm0-heimdal libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libldap-common
  libnghttp2-14 libpsl5 libroken18-heimdal librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libsqlite3-0 libssl1.1 libwind0-heimdal openssl
  publicsuffix
0 upgraded, 30 newly installed, 0 to remove and 34 not upgraded.
Need to get 4845 kB of archives.
After this operation, 14.8 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl1.1 amd64 1.1.1-1ubuntu2.1~18.04.23 [1303 kB]
……
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
root@e7fafbe40978:/# curl
curl: try 'curl --help' or 'curl --manual' for more information
安装Apache
root@e7fafbe40978:/# apt-get install -y apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils file libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libexpat1 libgdbm-compat4 libgdbm5 libicu60
  liblua5.2-0 libmagic-mgc libmagic1 libperl5.26 libxml2 mime-support netbase perl perl-base perl-modules-5.26 ssl-cert xz-utils
Suggested packages:
……

启动apache服务,然后使用curl测试本地

root@e7fafbe40978:/# service apache2 start
 * Starting Apache httpd web server apache2                                                                                                                  *
root@e7fafbe40978:/# curl 127.0.0.1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <!--
    Modified from the Debian original for Ubuntu
    Last updated: 2016-11-16
    See: https://launchpad.net/bugs/1288690
  -->
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Apache2 Ubuntu Default Page: It works</title>
    <style type="text/css" media="screen">
……

配合使用 -p 参数对外映射服务端口,可以允许容器外来访问该服务。

相关资源

CentOS/Fedora

CentOS

CentOSFedora 都是基于 Redhat 的常见 Linux 分支。CentOS 是目前企业级服务器的常用操作系统;Fedora 则主要面向个人桌面用户

image-20231206103820-2t44x3k.png

CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统),它是基于 Red Hat Enterprise Linux 源代码编译而成。由于 CentOSRedhat Linux 源于相同的代码基础,所以很多成本敏感且需要高稳定性的公司就使用 CentOS 来替代商业版 Red Hat Enterprise LinuxCentOS 自身不包含闭源软件。

使用 CentOS 官方镜像

使用 docker run 直接运行 CentOS 7 镜像,并登录 bash

PS C:\Users\fe> docker run -it centos:7 bash
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
[root@b65f8ce930f2 /]#

Fedora 系统简介及官方镜像使用

image-20231206110756-6bpicct.png

FedoraFedora Project 社区开发,红帽公司赞助的 Linux 发行版。它的目标是创建一套新颖、多功能并且自由和开源的操作系统。Fedora 的功能对于用户而言,它是一套功能完备的,可以更新的免费操作系统,而对赞助商 Red Hat 而言,它是许多新技术的测试平台。被认为可用的技术最终会加入到 Red Hat Enterprise Linux 中。

使用 Fedora 官方镜像

使用 docker run 命令直接运行 Fedora 官方镜像,并登录 bash

PS C:\Users\fe> docker run -it fedora bash
Unable to find image 'fedora:latest' locally
latest: Pulling from library/fedora
edad61c68e67: Pull complete
Digest: sha256:40ba585f0e25c096a08c30ab2f70ef3820b8ea5a4bdd16da0edbfc0a6952fa57
Status: Downloaded newer image for fedora:latest
[root@dd819d3d89e8 /]#

相关资源

参考地址yeasy.gitbook.io/docker_prac…