Files
docker_practice/03_install/3.4_centos.md
T

187 lines
7.0 KiB
Go
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 3.4 CentOS
CentOS (及其替代品 Rocky LinuxAlmaLinux) 是企业级服务器常用的操作系统本节介绍在这些系统上安装 Docker 的步骤
### 企业级部署的版本选择
值得注意的是**CentOS 8 已停止维护CentOS 7 已停止支持**如果你正在规划新的生产部署强烈建议选择 Rocky Linux AlmaLinux——这两个项目是由社区维护的 CentOS 替代品延续了 CentOS 的企业级特性同时提供了更长的生命周期承诺选择稳定的基础系统才能为 Docker 的长期运维奠定坚实基础
> 警告切勿在没有配置 Docker YUM 源的情况下直接使用 yum 命令安装 Docker
### 3.4.1 准备工作
安装前请确认系统版本和内核版本满足 Docker 的运行要求
#### 系统要求
> **严重警告**CentOS 7 已于 2024 6 30 日结束所有支持不再接收任何安全更新CentOS 8 已于 2021 12 31 日停止维护强烈建议新项目使用 **Rocky Linux** **AlmaLinux** 替代这两个项目由社区维护提供长期支持承诺
Docker 官方当前安装文档覆盖的 CentOS 平台为 **CentOS Stream 9** **CentOS Stream 10**具体以官方 [安装文档](https://docs.docker.com/engine/install/centos/) 为准)。Rocky Linux、AlmaLinux 等 RHEL 兼容发行版通常可以沿用相近步骤,但建议先在测试环境验证仓库与依赖是否匹配。
对于 Rocky LinuxAlmaLinux CentOS Stream推荐使用 `dnf` 包管理器
#### 卸载旧版本
旧版本的 Docker 称为 `docker` 或者 `docker-engine`使用以下命令卸载旧版本
```bash
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
docker-ce-cli \
containerd.io
```
### 3.4.2 使用 yum 安装
使用 yum/dnf 安装是管理 Docker 生命周期的标准方式
执行以下命令安装依赖包
```bash
$ sudo dnf install -y dnf-plugins-core
```
默认建议优先使用 Docker 官方仓库如果企业内网维护了受信任镜像站可自行替换仓库 URL
执行下面的命令添加 `yum` 软件源
```bash
$ sudo dnf config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
```
如果需要测试版本的 Docker 请执行以下命令
```bash
$ sudo dnf config-manager --set-enabled docker-ce-test
```
#### 安装 Docker
更新 `dnf` 软件源缓存并安装 Docker Engine 及常用 CLI 插件
```bash
$ sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```
### 3.4.3 防火墙注意事项
Docker 官方提醒如果主机使用 `ufw` `firewalld` 管理防火墙容器暴露出的端口可能绕过现有规则同时 Docker 仅兼容 `iptables-nft` `iptables-legacy`不支持直接由 `nft` 创建的规则集
因此生产环境中更稳妥的做法是
- 明确使用 `iptables-nft` `iptables-legacy` 维护规则
- 需要限制容器流量时优先把自定义规则写入 `DOCKER-USER`
- 在变更防火墙策略前先用测试容器验证端口暴露和东西向流量是否符合预期
### 3.4.4 使用脚本自动安装
在测试或开发环境中Docker 官方提供了便捷安装脚本但官方明确不建议把它作为生产环境的标准安装方式
在真正执行前建议先用 `--dry-run` 预览脚本动作
```bash
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh ./get-docker.sh --dry-run
# 若需要测试频道:
# curl -fsSL https://test.docker.com -o test-docker.sh
# sudo sh ./test-docker.sh
```
确认无误后再执行 `sudo sh ./get-docker.sh` 安装稳定版
### 3.4.5 启动 Docker
```bash
$ sudo systemctl enable --now docker
```
### 3.4.6 建立 docker 用户组
默认情况下`docker` 命令会使用 [Unix socket](https://en.wikipedia.org/wiki/Unix_domain_socket) 与 Docker 引擎通讯。而只有 `root` 用户和 `docker` 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 `root` 用户。因此,更好的做法是将需要使用 `docker` 的用户加入 `docker` 用户组。
> **安全警告`docker` 用户组等同于 `root` 权限**
>
> 将用户加入 `docker` 组免去了每次执行 `docker` 命令时输入 `sudo` 的繁琐但这也意味着该用户可以轻易获取主机的最高 root 权限例如通过挂载根目录运行容器)。
> 如果你在一个多用户共享的生产系统上配置切勿随意将普通用户加入此组此时更安全的替代方案是使用官方提供的 **[Rootless 模式 (Rootless mode)](https://docs.docker.com/engine/security/rootless/)**,它允许在没有任何 root 权限的情况下运行 Docker 守护进程和容器。
建立 `docker`
```bash
$ sudo groupadd docker
```
将当前用户加入 `docker`
```bash
$ sudo usermod -aG docker $USER
```
退出当前终端并重新登录进行如下测试
### 3.4.7 测试 Docker 是否安装正确
```bash
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
```
若能正常输出以上信息则说明安装成功
### 3.4.8 镜像加速
如果在使用过程中发现拉取 Docker 镜像十分缓慢可以配置 Docker [国内镜像加速](3.9_mirror.md)
### 3.4.9 添加内核参数
如果在 CentOS 使用 Docker 看到下面的这些警告信息
```bash
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
```
请添加内核配置参数以启用这些功能
```bash
$ sudo tee -a /etc/sysctl.conf <<-EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
```
然后重新加载 `sysctl.conf` 即可
```bash
$ sudo sysctl -p
```