Skip to content

Commit 18a91f7

Browse files
committed
Add os cases
1 parent b0e680d commit 18a91f7

30 files changed

+582
-251
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Docker — 从入门到实践
22

3-
0.8.2
3+
0.8.3
44

55
[Docker](http://www.docker.com) 是个划时代的开源项目,它彻底释放了计算虚拟化的威力,极大提高了应用的运行效率,降低了云计算资源供应的成本! 使用 Docker,可以让应用的部署、测试和分发都变得前所未有的高效和轻松!
66

SUMMARY.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@
6565
* [工具和示例](advanced_network/example.md)
6666
* [编辑网络配置文件](advanced_network/config_file.md)
6767
* [实例:创建一个点到点连接](advanced_network/ptp.md)
68-
* [实战案例](cases/README.md)
69-
* [使用 Supervisor 来管理进程](cases/supervisor.md)
70-
* [创建 tomcat\/weblogic 集群](cases/tomcat.md)
71-
* [多台物理主机之间的容器互联](cases/container_connect.md)
72-
* [标准化开发测试和生产环境](cases/environment.md)
7368
* [安全](security/README.md)
7469
* [内核命名空间](security/kernel_ns.md)
7570
* [控制组](security/control_group.md)
@@ -133,6 +128,13 @@
133128
* [腾讯云](cloud/qcloud.md)
134129
* [阿里云](cloud/alicloud.md)
135130
* [小结](cloud/summary.md)
131+
* [实战案例-操作系统](cases/os/README.md)
132+
* [Busybox](cases/os/busybox.md)
133+
* [Alpine](cases/os/alpine.md)
134+
* [Debian\/Ubuntu](cases/os/debian.md)
135+
* [CentOS\/Fedora](cases/os/centos.md)
136+
* [CoreOS](cases/os/coreos.md)
137+
* [本章小结](cases/os/summary.md)
136138
* [附录](appendix/README.md)
137139
* [附录一:常见问题总结](appendix/faq/README.md)
138140
* [附录二:热门镜像介绍](appendix/repo/README.md)

cases/README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

cases/container_connect.md

Lines changed: 0 additions & 78 deletions
This file was deleted.

cases/environment.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

cases/os/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 操作系统
2+
3+
目前常用的 Linux 发行版主要包括 Debian/Ubuntu 系列和 CentOS/Fedora 系列。
4+
5+
前者以自带软件包版本较新而出名;后者则宣称运行更稳定一些。选择哪个操作系统取决于读者的具体需求。同时,社区还推出了完全基于 Docker 的 Linux 发行版 CoreOS。
6+
7+
使用 Docker,读者只需要一个命令就能快速获取一个 linux 发行版镜像,这是以往包括各种虚拟化技术都难以实现的。这些镜像一般都很精简,但是可以支持完整 linux 系统的大部分功能。
8+
9+
本章将介绍如何使用 Docker 安装和使用 Busybox、Alphine、Debian/Ubuntu、CentOS/Fedora、CoreOS 等操作系统。

cases/os/_images/alpinelinux-logo.png

16.7 KB
Loading

cases/os/_images/busybox-logo.png

10.7 KB
Loading

cases/os/_images/centos-logo.png

36.8 KB
Loading

cases/os/_images/coreos-login.png

25 KB
Loading

cases/os/_images/coreos-logo.jpg

33.3 KB
Loading

cases/os/_images/coreos_crt.png

12.4 KB
Loading

cases/os/_images/coreos_list.png

8.14 KB
Loading

cases/os/_images/coreos_run_ip.png

35.2 KB
Loading

cases/os/_images/debian-logo.png

5.18 KB
Loading

cases/os/_images/docker_version.png

28 KB
Loading

cases/os/_images/fedora-logo.png

13.7 KB
Loading

cases/os/_images/php_pulling.png

54.3 KB
Loading

cases/os/_images/ubuntu-logo.jpg

31.7 KB
Loading

cases/os/_images/vmware_coreos.png

107 KB
Loading

cases/os/alpine.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## Alpine
2+
3+
### 简介
4+
5+
![Apline Linux 操作系统](_images/alpinelinux-logo.png)
6+
7+
`Alpine` 操作系统是一个面向安全的轻型 `Linux` 发行版。它不同于通常 `Linux` 发行版,`Alpine` 采用了 `musl libc``busybox` 以减小系统的体积和运行时资源消耗,但功能上比 `busybox` 又完善的多,因此得到开源社区越来越多的青睐。在保持瘦身的同时,`Alpine` 还提供了自己的包管理工具 `apk`,可以通过 `https://pkgs.alpinelinux.org/packages` 网站上查询包信息,也可以直接通过 `apk` 命令直接查询和安装各种软件。
8+
9+
`Alpine` 由非商业组织维护的,支持广泛场景的 `Linux`发行版,它特别为资深/重度`Linux`用户而优化,关注安全,性能和资源效能。`Alpine` 镜像可以适用于更多常用场景,并且是一个优秀的可以适用于生产的基础系统/环境。
10+
11+
`Alpine` Docker 镜像也继承了 Alpine Linux 发行版的这些优势。相比于其他 `Docker` 镜像,它的容量非常小,仅仅只有 5 MB 左右(对比 Ubuntu 系列镜像接近 200 MB),且拥有非常友好的包管理机制。官方镜像来自 `docker-alpine` 项目。
12+
13+
目前 Docker 官方已开始推荐使用 `Alpine` 替代之前的 `Ubuntu` 做为基础镜像环境。这样会带来多个好处。包括镜像下载速度加快,镜像安全性提高,主机之间的切换更方便,占用更少磁盘空间等。
14+
15+
下表是官方镜像的大小比较:
16+
17+
```sh
18+
REPOSITORY TAG IMAGE ID VIRTUAL SIZE
19+
alpine latest 4e38e38c8ce0 4.799 MB
20+
debian latest 4d6ce913b130 84.98 MB
21+
ubuntu latest b39b81afc8ca 188.3 MB
22+
centos latest 8efe422e6104 210 MB
23+
```
24+
25+
### 获取并使用官方镜像
26+
27+
由于镜像很小,下载时间往往很短,读者可以直接使用 `docker run` 指令直接运行一个 `Alpine` 容器,并指定运行的 Linux 指令,例如:
28+
29+
```sh
30+
$ docker run alpine echo '123'
31+
123
32+
```
33+
34+
笔者使用 time 工具来测试下在本地没有提前 pull 镜像情况下,执行 echo 命令的时间,仅需要 3 秒左右。
35+
36+
```sh
37+
$ time docker run alpine echo '123'Unable to find image 'alpine:latest' locallylatest: Pulling from library/alpine
38+
39+
e110a4a17941: Pull completeDigest: sha256:3dcdb92d7432d56604d4545cbd324b14e647b313626d99b889d0626de158f73aStatus: Downloaded newer image for alpine:latest123
40+
41+
real 0m3.367s user 0m0.040s sys 0m0.007s
42+
```
43+
44+
### 迁移至 `Alpine` 基础镜像
45+
46+
目前,大部分 Docker 官方镜像都已经支持 Alpine 作为基础镜像,可以很容易进行迁移。
47+
48+
例如:
49+
50+
* ubuntu/debian -> alpine
51+
* python:2.7 -> python:2.7-alpine
52+
* ruby:2.3 -> ruby:2.3-alpine
53+
54+
另外,如果使用 `Alpine` 镜像替换 `Ubuntu` 基础镜像,安装软件包时需要用 apk 包管理器替换 apt 工具,如
55+
56+
```sh
57+
$ apk add --no-cache <package>
58+
```
59+
60+
`Alpine` 中软件安装包的名字可能会与其他发行版有所不同,可以在 `https://pkgs.alpinelinux.org/packages` 网站搜索并确定安装包名称。如果需要的安装包不在主索引内,但是在测试或社区索引中。那么可以按照以下方法使用这些安装包。
61+
62+
```sh
63+
$ echo "http://dl-4.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
64+
$ apk --update add --no-cache <package>
65+
```
66+
67+
### 相关资源
68+
69+
* `Apline` 官网:`http://alpinelinux.org/`
70+
* `Apline` 官方仓库:`https://github.com/alpinelinux`
71+
* `Apline` 官方镜像:`https://hub.docker.com/_/alpine/`
72+
* `Apline` 官方镜像仓库:`https://github.com/gliderlabs/docker-alpine`
73+

cases/os/busybox.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
## Busybox
2+
3+
### 简介
4+
5+
![Busybox - Linux 瑞士军刀](_images/busybox-logo.png)
6+
7+
BusyBox 是一个集成了一百多个最常用 Linux 命令和工具(如 cat、echo、grep、mount、telnet 等)的精简工具箱,它只需要几 MB 的大小,很方便进行各种快速验证,被誉为“Linux 系统的瑞士军刀”。
8+
9+
BusyBox 可运行于多款 POSIX 环境的操作系统中,如 Linux(包括 Android)、Hurd、FreeBSD 等。
10+
11+
### 获取官方镜像
12+
13+
在 Docker Hub 中搜索 busybox 相关的镜像。
14+
15+
```sh
16+
$ docker search busybox
17+
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
18+
busybox Busybox base image. 755 [OK]
19+
progrium/busybox 63 [OK]
20+
radial/busyboxplus Full-chain, Internet enabled, busybox made... 11 [OK]
21+
odise/busybox-python 3 [OK]
22+
multiarch/busybox multiarch ports of ubuntu-debootstrap 2 [OK]
23+
azukiapp/busybox This image is meant to be used as the base... 2 [OK]
24+
...
25+
```
26+
27+
读者可以看到最受欢迎的镜像同时带有 OFFICIAL 标记,说明它是官方镜像。用户使用 docker pull 指令下载镜像 `busybox:latest`
28+
29+
```sh
30+
$ docker pull busybox:latest
31+
busybox:latest: The image you are pulling has been verified
32+
e433a6c5b276: Pull complete
33+
e72ac664f4f0: Pull complete
34+
511136ea3c5a: Pull complete
35+
df7546f9f060: Pull complete
36+
Status: Downloaded newer image for busybox:latest
37+
```
38+
39+
下载后,可以看到 busybox 镜像只有2.433 MB:
40+
41+
```sh
42+
$ docker images
43+
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
44+
busybox latest e72ac664f4f0 6 weeks ago 2.433 MB
45+
```
46+
47+
### 运行 busybox
48+
49+
启动一个 busybox 容器,并在容器中执行 grep 命令。
50+
51+
```sh
52+
$ docker run -it busybox
53+
/ # grep
54+
BusyBox v1.22.1 (2014-05-22 23:22:11 UTC) multi-call binary.
55+
56+
Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...
57+
58+
Search for PATTERN in FILEs (or stdin)
59+
60+
-H Add 'filename:' prefix
61+
-h Do not add 'filename:' prefix
62+
-n Add 'line_no:' prefix
63+
-l Show only names of files that match
64+
-L Show only names of files that don't match
65+
-c Show only count of matching lines
66+
-o Show only the matching part of line
67+
-q Quiet. Return 0 if PATTERN is found, 1 otherwise
68+
-v Select non-matching lines
69+
-s Suppress open and read errors
70+
-r Recurse
71+
-i Ignore case
72+
-w Match whole words only
73+
-x Match whole lines only
74+
-F PATTERN is a literal (not regexp)
75+
-E PATTERN is an extended regexp
76+
-m N Match up to N times per file
77+
-A N Print N lines of trailing context
78+
-B N Print N lines of leading context
79+
-C N Same as '-A N -B N'
80+
-e PTRN Pattern to match
81+
-f FILE Read pattern from file
82+
```
83+
84+
查看容器内的挂载信息。
85+
86+
```sh
87+
/ # mount
88+
rootfs on / type rootfs (rw)
89+
none on / type aufs (rw,relatime,si=b455817946f8505c)
90+
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
91+
tmpfs on /dev type tmpfs (rw,nosuid,mode=755)
92+
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
93+
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
94+
sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime)
95+
/dev/disk/by-uuid/b1f2dba7-d91b-4165-a377-bf1a8bed3f61 on /etc/resolv.conf type ext4 (rw,relatime,errors=remount-ro,data=ordered)
96+
/dev/disk/by-uuid/b1f2dba7-d91b-4165-a377-bf1a8bed3f61 on /etc/hostname type ext4 (rw,relatime,errors=remount-ro,data=ordered)
97+
/dev/disk/by-uuid/b1f2dba7-d91b-4165-a377-bf1a8bed3f61 on /etc/hosts type ext4 (rw,relatime,errors=remount-ro,data=ordered)
98+
devpts on /dev/console type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
99+
proc on /proc/sys type proc (ro,nosuid,nodev,noexec,relatime)
100+
proc on /proc/sysrq-trigger type proc (ro,nosuid,nodev,noexec,relatime)
101+
proc on /proc/irq type proc (ro,nosuid,nodev,noexec,relatime)
102+
proc on /proc/bus type proc (ro,nosuid,nodev,noexec,relatime)
103+
tmpfs on /proc/kcore type tmpfs (rw,nosuid,mode=755)
104+
```
105+
106+
busybox 镜像虽然小巧,但包括了大量常见的 Linux 命令,读者可以用它快速熟悉 Linux 命令。
107+
108+
### 相关资源
109+
110+
* `Busybox` 官网:`https://busybox.net/`
111+
* `Busybox` 官方仓库:`https://git.busybox.net/busybox/`
112+
* `Busybox` 官方镜像:`https://hub.docker.com/_/busybox/`
113+
* `Busybox` 官方仓库:`https://github.com/docker-library/busybox`
114+

0 commit comments

Comments
 (0)