操作系统
目前常用的 Linux 发行版主要包括 Debian/Ubuntu 系列和 CentOS/Fedora 系列。
前者以自带软件包版本较新而出名;后者则宣称运行更稳定一些。
选择哪个操作系统取决于读者的具体需求。同时,社区还推出了精简版的 Busybox 和 Alphine
使用 Docker,只需要一个命令就能快速获取一个 Linux 发行版镜像,这是以往各种虚拟化技术都难以实现的。
这些镜像一般都很精简,但是可以支持完整 Linux 系统的大部分功能。
BusyBox
BusyBox 是一个集成了一百多个最常用 Linux 命令(如 cat、echo、grep、mount、telnet 等)的精简工具箱,它只有不到 2MB 大小,被誉为“Linux 系统的瑞士军刀”。
BusyBox 可运行于多款 POSIX 环境的操作系统中,如 Linux(包括 Android)、Hurd、FreeBSD 等
获取官方镜像:
使用docker search
在 Docker Hub 中搜索 busybox 相关的镜像,如下所示:
| docker search busybox
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
busybox Busybox base image. 1953 [OK]
progrium/busybox 71 [OK]
radial/busyboxplus Full-chain, Internet enabled, busybox made f… 32 [OK]
yauritux/busybox-curl Busybox with CURL 10
arm32v7/busybox Busybox base image. 8
armhf/busybox Busybox base image. 6
odise/busybox-curl 4 [OK]
|
可以看到最受欢迎的镜像同时带有OFFICIAL
标记,说明它是官方镜像。
可使用docker pull
指令下载镜像busybox:latest
:
| docker pull busybox:latest
latest: Pulling from library/busybox
61c5ed1cbdf8: Pull complete
Digest: sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
|
下载后,可以看到 busybox 镜像只有1.22MB
| docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 018c9d7b792b 4 days ago 1.22MB
|
运行 busybox:
启动一个 busybox 镜像,并在容器中执行 grep 命令:
1
2
3
4
5
6
7
8
9
10
11
12
13 | docker run -it busybox
/ # grep
BusyBox v1.32.0 (2020-07-27 19:18:59 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
...
/ #
|
查看容器内的挂载信息:
| / # mount
overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/HKL66PIJWLJXLNLP4PEJOB7NSF:/var/lib/docker/overlay2/l/MRNG4EUELDUSY7KSO7JTKR23Q3,upperdir=/var/lib/docker/overlay2/7dd3ac8379d1442cd49ea9a7c8d5df5e7f9880b335f5e214d37b3f4fe26becd9/diff,workdir=/var/lib/docker/overlay2/7dd3ac8379d1442cd49ea9a7c8d5df5e7f9880b335f5e214d37b3f4fe26becd9/work)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,nosuid,size=65536k,mode=755)
...
|
busybxo 镜像虽然小巧,但包括了大量常见的 Linux 命令,可以用它快速熟悉 Linux 命令
https://busybox.net/
https://git.busybox.net/busybox
https://github.com/docker-library/busybox
Alpine
Alpine 操作系统是一个面向安全的轻型 Linux 发行版,关注安全,性能和资源效能。
不同于其他发行版,Alpine 采用了 musl libc 和 BusyBox 以减小系统的体积和运行时资源消耗,比 BusyBox 功能上更完善。
在保持瘦身的同时,Alpine 还提供了包管理工具 apk 查询和安装软件包
Alpine Docker 镜像继承了 Alpine Linux 发行版的这些优势。
相比于其他镜像,它的容量非常小,仅仅只有 5MB 左右(Ubuntu 系列镜像接近 200MB)。
官方镜像来自 docker-alpine 项目
目前 Docker 官方推荐使用 Alpine 作为默认的基础镜像环境,这可以带来多个优势,如镜像下载速度加快、镜像安全性提高、主机之间的切换更方便、占用更少磁盘空间等。
下面是官方镜像的大小比较,可以看出 Alpine 镜像的显著优势:
| docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest a24bb4013296 2 months ago 5.57MB
ubuntu latest 1e4467b07108 7 days ago 73.9MB
debian latest ae8514941ea4 10 days ago 114MB
centos latest 831691599b88 6 weeks ago 215MB
|
获取并使用官方镜像:
由于镜像很小,下载时间几乎可以忽略,可以使用docker [container] run
指令直接运行一个 Alpine 容器,并指定运行的指令,例如:
| docker run alpine echo "123"
123
|
本地没有提前 pull 镜像的情况下,直接执行 echo 命令,仅需要不到 3 秒的时间
| time docker run alpine echo '123'
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
df20fa9351a1: Already exists
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
123
docker run alpine echo '123' 0.06s user 0.06s system 5% cpu 2.332 total
|
迁移至 Alpine 基础镜像:
目前,大部分 Docker 官方镜像都已经支持 Alpine 作为基础镜像,可以很容易进行迁移。例如:
ubuntu/debian -> alpine
python:2.7 -> python:3.6-alpine
ruby:2.6 -> ruby:2.6-alpine
另外,如果使用 Alpine 镜像,安装软件包时可以使用 apk 工具,则如:
apk add --no-cache <package>
Alpine 中软件安装包的名字可能会与其他发行版有所不同,可以在https://pkgs.alpinelinux.org/packages
网站搜索并确定安装包名称。
如果需要的安装包不在主索引内,但是在测试或社区索引中。那么首先需要更新仓库列表,如下所示:
| echo "http://dl-4.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
apk --update add --no-cache <package>
|
Alpine 官网
Alpine 官方仓库
Alpine 官方镜像
Alpine 官方镜像仓库
Alpine 清华大学镜像仓库
Debian/Ubuntu
Debian 和 Ubuntu 都是目前较为流行的 Debian 系的服务器操作系统,十分适合研发场景。
Docker Hub 上提供了它们的官方镜像,国内各大容器云服务都提供了完整的支持
Debian 系统简介及官方镜像使用:
Debian 是基于 GPL 授权的开源操作系统,是目前个人电脑与服务器中最受欢迎的开源操作系统之一,由 Debian 计划(Debian Project)组织维护。
Debian 以其坚守 Unix 和自由软件的精神,及给予用户的众多选择而闻名。
目前 Debain 包括超过 25000 个软件包并支持 12 个计算机系统结构
可以使用docker search
搜索 Docker Hub,查找 Debian 镜像
| docker search debian
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 11171 [OK]
debian Debian is a Linux distribution that's compos… 3548 [OK]
...
|
使用docker run
命令直接运行 Debian 镜像
| docker run -it --rm debian bash
root@7a8157f34b88:/# cat /etc/issue
Debian GNU/Linux 10 \n \l
|
可以将 Debian 镜像作为基础镜像来构建自定义镜像。
如果需要进行本地化配置,如 UTF-8 支持,可参考:
| RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
&& localdef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8
|
Ubuntu 系统简介及官方镜像使用
Ubuntu 是以桌面应用为主的 GNU/Linux 开源操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词。
官方译名“友帮拓”,另有“乌班图”等译名。
Ubuntu 每 6 个月会发布一个新版本,每两年推出一个长期支持(Long Term Support, LTS)版本,一般支持 3 年时间
https://wiki.ubuntu.com/Releases
Ubuntu 相关的镜像有很多,这里只搜索那些评星 50 以上的镜像:
| docker search --filter=stars=50 ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 11171 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 451 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 246 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 222 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 110 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 98 [OK]
neurodebian NeuroDebian provides neuroscience research s… 68 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
|
下面以 ubuntu 18.04 为例,演示如何使用该镜像安装一些常用软件。
首先启动容器,并查看 ubuntu 镜像的发行版本号
| docker run -it ubuntu:18.04 bash
root@5c333b25440e:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
|
执行apt-get update
命令更新仓库信息。
更新信息后即可成功通过apt-get
命令来安装软件
1
2
3
4
5
6
7
8
9
10
11
12
13 | root@5c333b25440e:/# 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://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [100 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [880 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [9555 B]
Get:8 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [1033 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
...
Fetched 17.3 MB in 59s (293 kB/s)
Reading package lists... Done
|
安装curl
工具:
| root@5c333b25440e:/# apt-get install curl
Reading package lists... Done
...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
root@5c333b25440e:/# curl
curl: try 'curl --help' or 'curl --manual' for more information
|
接下来,再安装 Apache 服务:
| root@5c333b25440e:/# apt-get install -y apache2
|
启动这个 Apache 服务,然后使用 curl 工具来测试本地访问:
| root@5c333b25440e:/# service apache2 start
* Starting Apache httpd web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
*
|
配合使用 -p 参数对外映射服务端口,可以允许容器外来访问该服务
https://www.debian.org/
https://hub.docker.com//debian
https://hub.docker.com//ubuntu
CentOS/Fedora
CentOS 系统简介及官方镜像使用
CentOS 和 Fedora 都是基于 Redhat 的 Linux 发行版。
CentOS 是目前企业级服务器的常用操作系统;Fedora 则主要面向个人桌面用户
CentOS(Community Enterprise Operating System,社区企业操作系统)基于 Red Hat Enterprise Linxu 源代码编译而成。
由于 CentOS 与 RedHat Linux 源于相同的代码基础,所以很多成本敏感且需要高稳定性的公司就使用 CentOS 来替代上夜班 Red Hat Enterprise Linux。CentOS 自身不包含闭源软件。
使用 docker search 命令来搜索标星至少为 50 的 CentOS 相关镜像:
| docker search --filter=stars=50 centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6113 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 118 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 115 [OK]
centos/systemd systemd enabled base container. 86 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 79
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
|
使用 docker run 直接运行最新 CentOS 镜像,并登录 bash:
| docker run -it --rm centos bash
[root@81c2d1bac70a /]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
|
Fedora 系统简介及官方镜像使用
Fedora 是由 Fedora Project 社区开发,Red Hat 公司赞助的 Linux 发行版。
它的目标是创建一套新颖、多功能并且自由和开源的操作系统
使用 docker search 命令来搜索标星至少为 50 的 Fedora 相关镜像,结果如下:
| docker search --filter=stars=50 fedora
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
fedora Official Docker builds of Fedora 897 [OK]
|
使用 dokcer run 命令直接运行 Fedora 官方镜像,并登录 bash:
| docker run -ti --rm fedora bash
[root@3cdf2aea0763 /]# cat /etc/redhat-release
Fedora release 32 (Thirty Two)
|
https://getfedora.org/
https://hub.docker.com//fedora
https://centos.org/
https://hub.docker.com//centos
除了官方的镜像外,在 DockerHub 上还有许多第三方组织或个人维护的 Docker 镜像。
一般来说:
官方镜像体积都比较小,只带有一些基本的组件,适合用来作为基础镜像。
精简的系统有利于安全、稳定和高效的运行,也适合进行定制化
个别第三方(如 tutum,已被 Docker 收购)维护的应用镜像质量也非常高。
这些镜像通常针对某个具体应用进行配置,比如:包含 LAMP 组件的 Ubuntu 镜像。