Release v1.5.0: Restructure chapters and update for Docker v30.x
7
14_cases/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# 实战案例
|
||||
|
||||
本章将介绍 Docker 在不同场景下的实战案例。
|
||||
|
||||
* [实战案例 - 操作系统](os/README.md)
|
||||
* [实战案例 - CI/CD](ci/README.md)
|
||||
* [在 IDE 中使用 Docker](ide/README.md)
|
||||
7
14_cases/ci/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# CI/CD
|
||||
|
||||
**持续集成(Continuous integration)** 是一种软件开发实践,每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。
|
||||
|
||||
**持续部署(continuous deployment)** 是通过自动化的构建、测试和部署循环来快速交付高质量的产品。
|
||||
|
||||
与 `Jenkins` 不同的是,基于 Docker 的 CI/CD 每一步都运行在 Docker 容器中,所以理论上支持所有的编程语言。
|
||||
28
14_cases/ci/actions/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# GitHub Actions
|
||||
|
||||
GitHub [Actions](https://github.com/features/actions) 是 GitHub 推出的一款 CI/CD 工具。
|
||||
|
||||
我们可以在每个 `job` 的 `step` 中使用 Docker 执行构建步骤。
|
||||
|
||||
```yaml
|
||||
on: push
|
||||
|
||||
name: CI
|
||||
|
||||
jobs:
|
||||
my-job:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
with:
|
||||
fetch-depth: 2
|
||||
- name: run docker container
|
||||
uses: docker://golang:alpine
|
||||
with:
|
||||
args: go version
|
||||
```
|
||||
|
||||
## 参考资料
|
||||
|
||||
* [Actions Docs](https://docs.github.com/en/actions)
|
||||
74
14_cases/ci/devops_workflow.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# DevOps 工作流完整示例
|
||||
|
||||
本章将演示一个基于 Docker, Kubernetes 和 Jenkins/GitLab CI 的完整 DevOps 工作流。
|
||||
|
||||
## 工作流概览
|
||||
|
||||
1. **Code**: 开发人员提交代码到 GitLab。
|
||||
2. **Build**: GitLab CI 触发构建任务。
|
||||
3. **Test**: 运行单元测试和集成测试。
|
||||
4. **Package**: 构建 Docker 镜像并推送到 Harbor/Registry。
|
||||
5. **Deploy (Staging)**: 自动部署到测试环境 Kubernetes 集群。
|
||||
6. **Verify**: 人工或自动化验证。
|
||||
7. **Release (Production)**: 审批后自动部署到生产环境。
|
||||
|
||||
## 关键配置示例
|
||||
|
||||
### 1. Dockerfile (多阶段构建)
|
||||
|
||||
```dockerfile
|
||||
# Build stage
|
||||
FROM golang:1.18 AS builder
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN go build -o main .
|
||||
|
||||
# Final stage
|
||||
FROM alpine:latest
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app/main .
|
||||
CMD ["./main"]
|
||||
```
|
||||
|
||||
### 2. GitLab CI (.gitlab-ci.yml)
|
||||
|
||||
```yaml
|
||||
stages:
|
||||
- test
|
||||
- build
|
||||
- deploy
|
||||
|
||||
unit_test:
|
||||
stage: test
|
||||
image: golang:1.18
|
||||
script:
|
||||
- go test ./...
|
||||
|
||||
build_image:
|
||||
stage: build
|
||||
image: docker:20.10.16
|
||||
services:
|
||||
- docker:20.10.16-dind
|
||||
script:
|
||||
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
||||
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
|
||||
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
|
||||
|
||||
deploy_staging:
|
||||
stage: deploy
|
||||
image: dtzar/helm-kubectl
|
||||
script:
|
||||
- kubectl config set-cluster k8s --server=$KUBE_URL --insecure-skip-tls-verify=true
|
||||
- kubectl config set-credentials admin --token=$KUBE_TOKEN
|
||||
- kubectl config set-context default --cluster=k8s --user=admin
|
||||
- kubectl config use-context default
|
||||
- kubectl set image deployment/myapp myapp=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA -n staging
|
||||
only:
|
||||
- develop
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **不可变基础设施**: 一旦镜像构建完成,在各个环境(Dev, Staging, Prod)中都应该使用同一个镜像 tag (通常是 commit hash),而不是重新构建。
|
||||
2. **配置分离**: 使用 ConfigMap 和 Secret 管理环境特定的配置,不要打包进镜像。
|
||||
3. **GitOps**: 考虑引入 ArgoCD,将部署配置也作为代码存储在 Git 中,实现 Git 驱动的部署同步。
|
||||
6
14_cases/ci/drone/.env.example
Normal file
@@ -0,0 +1,6 @@
|
||||
DRONE_SERVER_HOST=
|
||||
DRONE_SERVER_PROTO=
|
||||
DRONE_RPC_SECRET=
|
||||
HOSTNAME=
|
||||
DRONE_GITHUB_CLIENT_ID=
|
||||
DRONE_GITHUB_CLIENT_SECRET=
|
||||
2
14_cases/ci/drone/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.env
|
||||
ssl/*
|
||||
99
14_cases/ci/drone/README.md
Normal file
@@ -0,0 +1,99 @@
|
||||
# Drone
|
||||
|
||||
基于 `Docker` 的 `CI/CD` 工具 `Drone` 所有编译、测试的流程都在 `Docker` 容器中进行。
|
||||
|
||||
开发者只需在项目中包含 `.drone.yml` 文件,将代码推送到 git 仓库,`Drone` 就能够自动化的进行编译、测试、发布。
|
||||
|
||||
本小节以 `GitHub` + `Drone` 来演示 `Drone` 的工作流程。当然在实际开发过程中,你的代码也许不在 GitHub 托管,那么你可以尝试使用 `Gogs` + `Drone` 来进行 `CI/CD`。
|
||||
|
||||
## Drone 关联项目
|
||||
|
||||
在 Github 新建一个名为 `drone-demo` 的仓库。
|
||||
|
||||
打开我们已经 [部署好的 Drone 网站](install.md) 或者 [Drone Cloud](https://cloud.drone.io),使用 GitHub 账号登录,在界面中关联刚刚新建的 `drone-demo` 仓库。
|
||||
|
||||
## 编写项目源代码
|
||||
|
||||
初始化一个 git 仓库
|
||||
|
||||
```bash
|
||||
$ mkdir drone-demo
|
||||
|
||||
$ cd drone-demo
|
||||
|
||||
$ git init
|
||||
|
||||
$ git remote add origin git@github.com:username/drone-demo.git
|
||||
```
|
||||
|
||||
这里以一个简单的 `Go` 程序为例,该程序输出 `Hello World!`
|
||||
|
||||
编写 `app.go` 文件
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main(){
|
||||
fmt.Printf("Hello World!\n");
|
||||
}
|
||||
```
|
||||
|
||||
编写 `.drone.yml` 文件
|
||||
|
||||
```yaml
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: build
|
||||
steps:
|
||||
- name: build
|
||||
image: golang:alpine
|
||||
pull: if-not-exists # always never
|
||||
environment:
|
||||
KEY: VALUE
|
||||
commands:
|
||||
- echo $KEY
|
||||
- pwd
|
||||
- ls
|
||||
- CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
||||
- ./app
|
||||
|
||||
trigger:
|
||||
branch:
|
||||
- master
|
||||
```
|
||||
|
||||
现在目录结构如下
|
||||
|
||||
```bash
|
||||
.
|
||||
├── .drone.yml
|
||||
└── app.go
|
||||
```
|
||||
|
||||
## 推送项目源代码到 GitHub
|
||||
|
||||
```bash
|
||||
$ git add .
|
||||
|
||||
$ git commit -m "test drone ci"
|
||||
|
||||
$ git push origin master
|
||||
```
|
||||
|
||||
## 查看项目构建过程及结果
|
||||
|
||||
打开我们部署好的 `Drone` 网站或者 Drone Cloud,即可看到构建结果。
|
||||
|
||||

|
||||
|
||||
当然我们也可以把构建结果上传到 GitHub,Docker Registry,云服务商提供的对象存储,或者生产环境中。
|
||||
|
||||
本书 GitBook 也使用 Drone 进行 CI/CD,具体配置信息请查看本书根目录 [`.drone.yml`](../../../.drone.yml) 文件。
|
||||
|
||||
## 参考链接
|
||||
|
||||
* [Drone Github](https://github.com/drone/drone)
|
||||
* [Drone 文档](https://docs.drone.io/)
|
||||
* [Drone 示例](https://github.com/docker-practice/drone-demo)
|
||||
BIN
14_cases/ci/drone/_images/drone-build.png
Normal file
|
After Width: | Height: | Size: 72 KiB |
19
14_cases/ci/drone/demo/.drone.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: build
|
||||
steps:
|
||||
- name: build
|
||||
image: golang:alpine
|
||||
pull: if-not-exists # always never
|
||||
environment:
|
||||
KEY: VALUE
|
||||
commands:
|
||||
- echo $KEY
|
||||
- pwd
|
||||
- ls
|
||||
- CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
||||
- ./app
|
||||
|
||||
trigger:
|
||||
branch:
|
||||
- master
|
||||
22
14_cases/ci/drone/demo/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Drone CI Demo 项目
|
||||
|
||||
这是一个基于 Go 语言编写的简单 Web 应用示例,用于演示 Drone CI 的持续集成流程。
|
||||
|
||||
## 目录结构
|
||||
|
||||
* `app.go`: 简单的 Go Web 服务器代码。
|
||||
* `.drone.yml`: Drone CI 的配置文件,定义了构建和测试流程。
|
||||
* `Dockerfile`: 定义了如何将该应用构建为 Docker 镜像。
|
||||
|
||||
## 如何运行
|
||||
|
||||
1. 确保本地已安装 Docker 环境。
|
||||
2. 进入本目录构建镜像:
|
||||
```bash
|
||||
docker build -t drone-demo-app .
|
||||
```
|
||||
3. 运行容器:
|
||||
```bash
|
||||
docker run -p 8080:8080 drone-demo-app
|
||||
```
|
||||
4. 访问 `http://localhost:8080` 查看效果。
|
||||
7
14_cases/ci/drone/demo/app.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main(){
|
||||
fmt.Printf("Hello World!\n");
|
||||
}
|
||||
38
14_cases/ci/drone/docker-compose.yml
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
|
||||
services:
|
||||
|
||||
drone-server:
|
||||
image: drone/drone:2.3.1
|
||||
ports:
|
||||
- 443:443
|
||||
- 80:80
|
||||
volumes:
|
||||
- drone-data:/data:rw
|
||||
- ./ssl:/etc/certs
|
||||
restart: always
|
||||
environment:
|
||||
- DRONE_SERVER_HOST=${DRONE_SERVER_HOST:-drone.domain.com}
|
||||
- DRONE_SERVER_PROTO=${DRONE_SERVER_PROTO:-https}
|
||||
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
|
||||
- DRONE_GITHUB_SERVER=https://github.com
|
||||
- DRONE_GITHUB_CLIENT_ID=${DRONE_GITHUB_CLIENT_ID}
|
||||
- DRONE_GITHUB_CLIENT_SECRET=${DRONE_GITHUB_CLIENT_SECRET}
|
||||
|
||||
drone-agent:
|
||||
image: drone/drone-runner-docker:1
|
||||
restart: always
|
||||
depends_on:
|
||||
- drone-server
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:rw
|
||||
environment:
|
||||
- DRONE_RPC_PROTO=http
|
||||
- DRONE_RPC_HOST=drone-server
|
||||
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
|
||||
- DRONE_RUNNER_NAME=${HOSTNAME:-demo}
|
||||
- DRONE_RUNNER_CAPACITY=2
|
||||
dns: 114.114.114.114
|
||||
|
||||
volumes:
|
||||
drone-data:
|
||||
86
14_cases/ci/drone/install.md
Normal file
@@ -0,0 +1,86 @@
|
||||
# 部署 Drone
|
||||
|
||||
## 要求
|
||||
|
||||
* 拥有公网 IP、域名 (如果你不满足要求,可以尝试在本地使用 Gogs + Drone)
|
||||
|
||||
* 域名 SSL 证书 (目前国内有很多云服务商提供免费证书)
|
||||
|
||||
* 熟悉 `Docker` 以及 `Docker Compose`
|
||||
|
||||
* 熟悉 `Git` 基本命令
|
||||
|
||||
* 对 `CI/CD` 有一定了解
|
||||
|
||||
## 新建 GitHub 应用
|
||||
|
||||
登录 GitHub,在 https://github.com/settings/applications/new 新建一个应用。
|
||||
|
||||

|
||||
|
||||
接下来查看这个应用的详情,记录 `Client ID` 和 `Client Secret`,之后配置 Drone 会用到。
|
||||
|
||||
## 配置 Drone
|
||||
|
||||
我们通过使用 `Docker Compose` 来启动 `Drone`,编写 `docker-compose.yml` 文件。
|
||||
|
||||
```yaml
|
||||
|
||||
|
||||
services:
|
||||
|
||||
drone-server:
|
||||
image: drone/drone:2.3.1
|
||||
ports:
|
||||
- 443:443
|
||||
- 80:80
|
||||
volumes:
|
||||
- drone-data:/data:rw
|
||||
- ./ssl:/etc/certs
|
||||
restart: always
|
||||
environment:
|
||||
- DRONE_SERVER_HOST=${DRONE_SERVER_HOST:-https://drone.yeasy.com}
|
||||
- DRONE_SERVER_PROTO=${DRONE_SERVER_PROTO:-https}
|
||||
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
|
||||
- DRONE_GITHUB_SERVER=https://github.com
|
||||
- DRONE_GITHUB_CLIENT_ID=${DRONE_GITHUB_CLIENT_ID}
|
||||
- DRONE_GITHUB_CLIENT_SECRET=${DRONE_GITHUB_CLIENT_SECRET}
|
||||
|
||||
drone-agent:
|
||||
image: drone/drone-runner-docker:1
|
||||
restart: always
|
||||
depends_on:
|
||||
- drone-server
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:rw
|
||||
environment:
|
||||
- DRONE_RPC_PROTO=http
|
||||
- DRONE_RPC_HOST=drone-server
|
||||
- DRONE_RPC_SECRET=${DRONE_RPC_SECRET:-secret}
|
||||
- DRONE_RUNNER_NAME=${HOSTNAME:-demo}
|
||||
- DRONE_RUNNER_CAPACITY=2
|
||||
dns: 114.114.114.114
|
||||
|
||||
volumes:
|
||||
drone-data:
|
||||
```
|
||||
|
||||
新建 `.env` 文件,输入变量及其值
|
||||
|
||||
```bash
|
||||
# 必填 服务器地址,例如 drone.domain.com
|
||||
DRONE_SERVER_HOST=
|
||||
DRONE_SERVER_PROTO=https
|
||||
DRONE_RPC_SECRET=secret
|
||||
HOSTNAME=demo
|
||||
# 必填 在 GitHub 应用页面查看
|
||||
DRONE_GITHUB_CLIENT_ID=
|
||||
# 必填 在 GitHub 应用页面查看
|
||||
DRONE_GITHUB_CLIENT_SECRET=
|
||||
```
|
||||
|
||||
### 启动 Drone
|
||||
|
||||
```bash
|
||||
$ docker-compose up -d
|
||||
```
|
||||
3
14_cases/ide/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# 在 IDE 中使用 Docker
|
||||
|
||||
使用 IDE 进行开发,往往要求本地安装好工具链。一些 IDE 支持 Docker 容器中的工具链,这样充分利用了 Docker 的优点,而无需在本地安装。
|
||||
5
14_cases/ide/vsCode.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# VS Code 中使用 Docker
|
||||
|
||||
## 将 Docker 容器作为远程开发环境
|
||||
|
||||
无需本地安装开发工具,直接将 Docker 容器作为开发环境,具体参考 [官方文档](https://code.visualstudio.com/docs/remote/containers)。
|
||||
9
14_cases/os/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# 操作系统
|
||||
|
||||
目前常用的 Linux 发行版主要包括 `Debian/Ubuntu` 系列和 `CentOS/Fedora` 系列。
|
||||
|
||||
前者以自带软件包版本较新而出名;后者则宣称运行更稳定一些。选择哪个操作系统取决于读者的具体需求。
|
||||
|
||||
使用 Docker,读者只需要一个命令就能快速获取一个 Linux 发行版镜像,这是以往包括各种虚拟化技术都难以实现的。这些镜像一般都很精简,但是可以支持完整 Linux 系统的大部分功能。
|
||||
|
||||
本章将介绍如何使用 Docker 安装和使用 `Busybox`、`Alphine`、`Debian/Ubuntu`、`CentOS/Fedora` 等操作系统。
|
||||
BIN
14_cases/os/_images/alpinelinux-logo.png
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
14_cases/os/_images/busybox-logo.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
14_cases/os/_images/centos-logo.png
Normal file
|
After Width: | Height: | Size: 37 KiB |
BIN
14_cases/os/_images/coreos-login.png
Normal file
|
After Width: | Height: | Size: 25 KiB |
BIN
14_cases/os/_images/coreos-logo.jpg
Normal file
|
After Width: | Height: | Size: 33 KiB |
BIN
14_cases/os/_images/coreos_crt.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
14_cases/os/_images/coreos_list.png
Normal file
|
After Width: | Height: | Size: 8.1 KiB |
BIN
14_cases/os/_images/coreos_run_ip.png
Normal file
|
After Width: | Height: | Size: 35 KiB |
BIN
14_cases/os/_images/debian-logo.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
14_cases/os/_images/docker_version.png
Normal file
|
After Width: | Height: | Size: 28 KiB |
BIN
14_cases/os/_images/fedora-logo.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
14_cases/os/_images/php_pulling.png
Normal file
|
After Width: | Height: | Size: 54 KiB |
BIN
14_cases/os/_images/ubuntu-logo.jpg
Normal file
|
After Width: | Height: | Size: 32 KiB |
BIN
14_cases/os/_images/vmware_coreos.png
Normal file
|
After Width: | Height: | Size: 107 KiB |
69
14_cases/os/alpine.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# Alpine
|
||||
|
||||
## 简介
|
||||
|
||||

|
||||
|
||||
`Alpine` 操作系统是一个面向安全的轻型 `Linux` 发行版。它不同于通常 `Linux` 发行版,`Alpine` 采用了 `musl libc` 和 `busybox` 以减小系统的体积和运行时资源消耗,但功能上比 `busybox` 又完善的多,因此得到开源社区越来越多的青睐。在保持瘦身的同时,`Alpine` 还提供了自己的包管理工具 `apk`,可以通过 `https://pkgs.alpinelinux.org/packages` 网站上查询包信息,也可以直接通过 `apk` 命令直接查询和安装各种软件。
|
||||
|
||||
`Alpine` 由非商业组织维护的,支持广泛场景的 `Linux`发行版,它特别为资深/重度`Linux`用户而优化,关注安全,性能和资源效能。`Alpine` 镜像可以适用于更多常用场景,并且是一个优秀的可以适用于生产的基础系统/环境。
|
||||
|
||||
`Alpine` Docker 镜像也继承了 `Alpine Linux` 发行版的这些优势。相比于其他 `Docker` 镜像,它的容量非常小,仅仅只有 **5 MB** 左右(对比 `Ubuntu` 系列镜像接近 `200 MB`),且拥有非常友好的包管理机制。官方镜像来自 `docker-alpine` 项目。
|
||||
|
||||
目前 Docker 官方已开始推荐使用 `Alpine` 替代之前的 `Ubuntu` 做为基础镜像环境。这样会带来多个好处。包括镜像下载速度加快,镜像安全性提高,主机之间的切换更方便,占用更少磁盘空间等。
|
||||
|
||||
下表是官方镜像的大小比较:
|
||||
|
||||
```bash
|
||||
REPOSITORY TAG IMAGE ID VIRTUAL SIZE
|
||||
alpine latest 4e38e38c8ce0 4.799 MB
|
||||
debian latest 4d6ce913b130 84.98 MB
|
||||
ubuntu latest b39b81afc8ca 188.3 MB
|
||||
centos latest 8efe422e6104 210 MB
|
||||
```
|
||||
|
||||
## 获取并使用官方镜像
|
||||
|
||||
由于镜像很小,下载时间往往很短,读者可以直接使用 `docker run` 指令直接运行一个 `Alpine` 容器,并指定运行的 Linux 指令,例如:
|
||||
|
||||
```bash
|
||||
$ docker run alpine echo '123'
|
||||
123
|
||||
```
|
||||
|
||||
## 迁移至 `Alpine` 基础镜像
|
||||
|
||||
目前,大部分 Docker 官方镜像都已经支持 `Alpine` 作为基础镜像,可以很容易进行迁移。
|
||||
|
||||
例如:
|
||||
|
||||
* `ubuntu/debian` -> `alpine`
|
||||
* `python:3` -> `python:3-alpine`
|
||||
* `ruby:2.6` -> `ruby:2.6-alpine`
|
||||
|
||||
另外,如果使用 `Alpine` 镜像替换 `Ubuntu` 基础镜像,安装软件包时需要用 `apk` 包管理器替换 `apt` 工具,如
|
||||
|
||||
```bash
|
||||
$ apk add --no-cache <package>
|
||||
```
|
||||
|
||||
`Alpine` 中软件安装包的名字可能会与其他发行版有所不同,可以在 `https://pkgs.alpinelinux.org/packages` 网站搜索并确定安装包名称。如果需要的安装包不在主索引内,但是在测试或社区索引中。那么可以按照以下方法使用这些安装包。
|
||||
|
||||
```bash
|
||||
$ echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
|
||||
$ apk --update add --no-cache <package>
|
||||
```
|
||||
|
||||
由于在国内访问 `apk` 仓库较缓慢,建议在使用 `apk` 之前先替换仓库地址为国内镜像。
|
||||
|
||||
```docker
|
||||
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories \
|
||||
&& apk add --no-cache <package>
|
||||
```
|
||||
|
||||
## 相关资源
|
||||
|
||||
* `Alpine` 官网:https://www.alpinelinux.org/
|
||||
* `Alpine` 官方仓库:https://github.com/alpinelinux
|
||||
* `Alpine` 官方镜像:https://hub.docker.com/\_/alpine/
|
||||
* `Alpine` 官方镜像仓库:https://github.com/gliderlabs/docker-alpine
|
||||
115
14_cases/os/busybox.md
Normal file
@@ -0,0 +1,115 @@
|
||||
# Busybox
|
||||
|
||||
## 简介
|
||||
|
||||

|
||||
|
||||
`BusyBox` 是一个集成了一百多个最常用 Linux 命令和工具(如 `cat`、`echo`、`grep`、`mount`、`telnet` 等)的精简工具箱,它只需要几 MB 的大小,很方便进行各种快速验证,被誉为“Linux 系统的瑞士军刀”。
|
||||
|
||||
`BusyBox` 可运行于多款 `POSIX` 环境的操作系统中,如 `Linux`(包括 `Android`)、`Hurd`、`FreeBSD` 等。
|
||||
|
||||
## 获取官方镜像
|
||||
|
||||
可以使用 `docker pull` 指令下载 `busybox:latest` 镜像:
|
||||
|
||||
```bash
|
||||
$ docker pull busybox:latest
|
||||
latest: Pulling from library/busybox
|
||||
5c4213be9af9: Pull complete
|
||||
Digest: sha256:c6b45a95f932202dbb27c31333c4789f45184a744060f6e569cc9d2bf1b9ad6f
|
||||
Status: Downloaded newer image for busybox:latest
|
||||
docker.io/library/busybox:latest
|
||||
```
|
||||
|
||||
下载后,可以看到 `busybox` 镜像只有 **2.433 MB**:
|
||||
|
||||
```bash
|
||||
$ docker image ls
|
||||
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
|
||||
busybox latest e72ac664f4f0 6 weeks ago 2.433 MB
|
||||
```
|
||||
|
||||
## 运行 busybox
|
||||
|
||||
启动一个 `busybox` 容器,并在容器中执行 `grep` 命令。
|
||||
|
||||
```bash
|
||||
$ docker run -it busybox
|
||||
/ # grep
|
||||
BusyBox v1.22.1 (2014-05-22 23:22:11 UTC) multi-call binary.
|
||||
|
||||
Usage: grep [-HhnlLoqvsriwFE] [-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
|
||||
-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
|
||||
```
|
||||
|
||||
查看容器内的挂载信息。
|
||||
|
||||
```bash
|
||||
/ # mount
|
||||
overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/BOTCI5RF24AMC4A2UWF4N6ZWFP:/var/lib/docker/overlay2/l/TWVP5T5DMKJGXZOROR7CAPWGFP,upperdir=/var/lib/docker/overlay2/801ef0bf6cce35288dbb8fe00a4f9cc47760444693bfdf339ed0bdcf926e12a3/diff,workdir=/var/lib/docker/overlay2/801ef0bf6cce35288dbb8fe00a4f9cc47760444693bfdf339ed0bdcf926e12a3/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 (ro,nosuid,nodev,noexec,relatime,mode=755)
|
||||
cgroup on /sys/fs/cgroup/systemd type cgroup (ro,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)
|
||||
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (ro,nosuid,nodev,noexec,relatime,net_cls,net_prio)
|
||||
cgroup on /sys/fs/cgroup/freezer type cgroup (ro,nosuid,nodev,noexec,relatime,freezer)
|
||||
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (ro,nosuid,nodev,noexec,relatime,cpu,cpuacct)
|
||||
cgroup on /sys/fs/cgroup/cpuset type cgroup (ro,nosuid,nodev,noexec,relatime,cpuset)
|
||||
cgroup on /sys/fs/cgroup/blkio type cgroup (ro,nosuid,nodev,noexec,relatime,blkio)
|
||||
cgroup on /sys/fs/cgroup/perf_event type cgroup (ro,nosuid,nodev,noexec,relatime,perf_event)
|
||||
cgroup on /sys/fs/cgroup/memory type cgroup (ro,nosuid,nodev,noexec,relatime,memory)
|
||||
cgroup on /sys/fs/cgroup/devices type cgroup (ro,nosuid,nodev,noexec,relatime,devices)
|
||||
cgroup on /sys/fs/cgroup/pids type cgroup (ro,nosuid,nodev,noexec,relatime,pids)
|
||||
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/vda1 on /etc/resolv.conf type ext3 (rw,noatime,data=ordered)
|
||||
/dev/vda1 on /etc/hostname type ext3 (rw,noatime,data=ordered)
|
||||
/dev/vda1 on /etc/hosts type ext3 (rw,noatime,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,relatime)
|
||||
proc on /proc/fs type proc (ro,relatime)
|
||||
proc on /proc/irq type proc (ro,relatime)
|
||||
proc on /proc/sys type proc (ro,relatime)
|
||||
proc on /proc/sysrq-trigger type proc (ro,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)
|
||||
```
|
||||
|
||||
`busybox` 镜像虽然小巧,但包括了大量常见的 `Linux` 命令,读者可以用它快速熟悉 `Linux` 命令。
|
||||
|
||||
## 相关资源
|
||||
|
||||
* `Busybox` 官网:https://busybox.net/
|
||||
* `Busybox` 官方仓库:https://git.busybox.net/busybox/
|
||||
* `Busybox` 官方镜像:https://hub.docker.com/\_/busybox/
|
||||
* `Busybox` 官方仓库:https://github.com/docker-library/busybox
|
||||
57
14_cases/os/centos.md
Normal file
@@ -0,0 +1,57 @@
|
||||
# CentOS Fedora
|
||||
|
||||
## CentOS 系统简介
|
||||
|
||||
`CentOS` 和 `Fedora` 都是基于 `Redhat` 的常见 Linux 分支。`CentOS` 是目前企业级服务器的常用操作系统;`Fedora` 则主要面向个人桌面用户。
|
||||
|
||||

|
||||
|
||||
CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统),它是基于 `Red Hat Enterprise Linux` 源代码编译而成。由于 `CentOS` 与 `Redhat Linux` 源于相同的代码基础,所以很多成本敏感且需要高稳定性的公司就使用 `CentOS` 来替代商业版 `Red Hat Enterprise Linux`。`CentOS` 自身不包含闭源软件。
|
||||
|
||||
### 使用 CentOS 官方镜像
|
||||
|
||||
**注意:CentOS 8 已于 2021 年 12 月 31 日停止维护(EOL)。对于新部署,推荐使用 CentOS Stream,或 Rocky Linux、AlmaLinux 等替代发行版。**
|
||||
|
||||
使用 `docker run` 直接运行 `CentOS 7` 镜像,并登录 `bash`。
|
||||
|
||||
```bash
|
||||
$ docker run -it centos:7 bash
|
||||
Unable to find image 'centos:latest' locally
|
||||
latest: Pulling from library/centos
|
||||
3d8673bd162a: Pull complete
|
||||
Digest: sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4
|
||||
Status: Downloaded newer image for centos:latest
|
||||
[root@43eb3b194d48 /]# cat /etc/redhat-release
|
||||
CentOS Linux release 7.9.2009 (Core)
|
||||
```
|
||||
|
||||
## Fedora 系统简介
|
||||
|
||||

|
||||
|
||||
`Fedora` 由 `Fedora Project` 社区开发,红帽公司赞助的 `Linux` 发行版。它的目标是创建一套新颖、多功能并且自由和开源的操作系统。`Fedora` 的功能对于用户而言,它是一套功能完备的,可以更新的免费操作系统,而对赞助商 `Red Hat` 而言,它是许多新技术的测试平台。被认为可用的技术最终会加入到 `Red Hat Enterprise Linux` 中。
|
||||
|
||||
### 使用 Fedora 官方镜像
|
||||
|
||||
使用 `docker run` 命令直接运行 `Fedora` 官方镜像,并登录 `bash`。
|
||||
|
||||
$ docker run -it fedora bash
|
||||
Unable to find image 'fedora:latest' locally
|
||||
latest: Pulling from library/fedora
|
||||
2bf01635e2a0: Pull complete
|
||||
Digest: sha256:64a02df6aac27d1200c2572fe4b9949f1970d05f74d367ce4af994ba5dc3669e
|
||||
Status: Downloaded newer image for fedora:latest
|
||||
[root@196ca341419b /]# cat /etc/redhat-release
|
||||
Fedora release 39 (Thirty Nine)
|
||||
|
||||
|
||||
## 相关资源
|
||||
|
||||
* `Fedora` 官网:https://getfedora.org/
|
||||
* `Fedora` 官方仓库:https://github.com/fedora-infra
|
||||
* `Fedora` 官方镜像:https://hub.docker.com/\_/fedora/
|
||||
* `Fedora` 官方镜像仓库:https://github.com/fedora-cloud/docker-brew-fedora
|
||||
* `CentOS` 官网:https://www.centos.org
|
||||
* `CentOS` 官方仓库:https://github.com/CentOS
|
||||
* `CentOS` 官方镜像:https://hub.docker.com/\_/centos/
|
||||
* `CentOS` 官方镜像仓库:https://github.com/CentOS/CentOS-Dockerfiles
|
||||
136
14_cases/os/debian.md
Normal file
@@ -0,0 +1,136 @@
|
||||
# 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` 发行版,例如 `Ubuntu`、`Knoppix` 和 `Linspire` 及 `Xandros` 等,都基于 `Debian GNU/Linux`。
|
||||
|
||||
### 使用 Debian 官方镜像
|
||||
|
||||
官方提供了大家熟知的 `debian` 镜像以及面向科研领域的 `neurodebian` 镜像。可以使用 `docker run` 直接运行 `Debian` 镜像。
|
||||
|
||||
```bash
|
||||
$ docker run -it debian bash
|
||||
root@668e178d8d69:/# cat /etc/issue
|
||||
Debian GNU/Linux 8
|
||||
```
|
||||
|
||||
`Debian` 镜像很适合作为基础镜像,构建自定义镜像。
|
||||
|
||||
## Ubuntu 系统简介
|
||||
|
||||

|
||||
|
||||
`Ubuntu` 是一个以桌面应用为主的 `GNU/Linux` 操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词(官方译名“友帮拓”,另有“吾帮托”、“乌班图”、“有奔头”或“乌斑兔”等译名)。`Ubuntu` 意思是“人性”以及“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想。 `Ubuntu` 基于 `Debian` 发行版和 `GNOME/Unity` 桌面环境,与 `Debian` 的不同在于它每 6 个月会发布一个新版本,每 2 年推出一个长期支持 **(Long Term Support,LTS)** 版本,一般支持 3 年时间。
|
||||
|
||||
### 使用 Ubuntu 官方镜像
|
||||
|
||||
下面以 `ubuntu:24.04` 为例,演示如何使用该镜像安装一些常用软件。
|
||||
|
||||
首先使用 `-ti` 参数启动容器,登录 `bash`,查看 `ubuntu` 的发行版本号。
|
||||
|
||||
```bash
|
||||
$ docker run -ti ubuntu:24.04 /bin/bash
|
||||
root@7d93de07bf76:/# cat /etc/os-release
|
||||
PRETTY_NAME="Ubuntu 24.04 LTS"
|
||||
NAME="Ubuntu"
|
||||
VERSION_ID="24.04"
|
||||
VERSION="24.04 LTS (Noble Numbat)"
|
||||
VERSION_CODENAME=noble
|
||||
ID=ubuntu
|
||||
ID_LIKE=debian
|
||||
HOME_URL="https://www.ubuntu.com/"
|
||||
SUPPORT_URL="https://help.ubuntu.com/"
|
||||
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
|
||||
```
|
||||
|
||||
当试图直接使用 `apt-get` 安装一个软件的时候,会提示 `E: Unable to locate package`。
|
||||
|
||||
```bash
|
||||
root@7d93de07bf76:/# apt-get install curl
|
||||
Reading package lists... Done
|
||||
Building dependency tree... Done
|
||||
Reading state information... Done
|
||||
E: Unable to locate package curl
|
||||
```
|
||||
|
||||
这并非系统不支持 `apt-get` 命令。Docker 镜像在制作时为了精简清除了 `apt` 仓库信息,因此需要先执行 `apt-get update` 命令来更新仓库信息。更新信息后即可成功通过 `apt-get` 命令来安装软件。
|
||||
|
||||
```bash
|
||||
root@7d93de07bf76:/# apt-get update
|
||||
Get:1 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
|
||||
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
|
||||
...
|
||||
Fetched 25.8 MB in 8s (3215 kB/s)
|
||||
Reading package lists... Done
|
||||
```
|
||||
|
||||
首先,安装 `curl` 工具。
|
||||
|
||||
```bash
|
||||
root@7d93de07bf76:/# 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
|
||||
...
|
||||
root@7d93de07bf76:/# curl
|
||||
curl: try 'curl --help' or 'curl --manual' for more information
|
||||
```
|
||||
|
||||
接下来,再安装 `apache` 服务。
|
||||
|
||||
```bash
|
||||
root@7d93de07bf76:/# 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-modules-5.26 ssl-cert xz-utils
|
||||
...
|
||||
```
|
||||
|
||||
启动这个 `apache` 服务,然后使用 `curl` 来测试本地访问。
|
||||
|
||||
```bash
|
||||
root@7d93de07bf76:/# service apache2 start
|
||||
* Starting 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
|
||||
*
|
||||
root@7d93de07bf76:/# 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` 参数对外映射服务端口,可以允许容器外来访问该服务。
|
||||
|
||||
## 相关资源
|
||||
|
||||
* `Debian` 官网:https://www.debian.org/
|
||||
* `Neuro Debian` 官网:http://neuro.debian.net/
|
||||
* `Debian` 官方仓库:https://github.com/Debian
|
||||
* `Debian` 官方镜像:https://hub.docker.com/\_/debian/
|
||||
* `Debian` 官方镜像仓库:https://github.com/tianon/docker-brew-debian/
|
||||
* `Ubuntu` 官网:https://ubuntu.com
|
||||
* `Ubuntu` 官方仓库:https://github.com/ubuntu
|
||||
* `Ubuntu` 官方镜像:https://hub.docker.com/\_/ubuntu/
|
||||
* `Ubuntu` 官方镜像仓库:https://github.com/tianon/docker-brew-ubuntu-core
|
||||
11
14_cases/os/summary.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# 本章小结
|
||||
|
||||
本章讲解了典型操作系统镜像的下载和使用。
|
||||
|
||||
除了官方的镜像外,在 `Docker Hub` 上还有许多第三方组织或个人上传的 Docker 镜像。
|
||||
|
||||
读者可以根据具体情况来选择。一般来说:
|
||||
|
||||
* 官方镜像体积都比较小,只带有一些基本的组件。 精简的系统有利于安全、稳定和高效的运行,也适合进行个性化定制。
|
||||
|
||||
* 出于安全考虑,几乎所有官方制作的镜像都没有安装 SSH 服务,无法通过用户名和密码直接登录到容器中。
|
||||