Files
docker_practice/03_install/3.1_ubuntu.md
T

167 lines
6.7 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.1 Ubuntu
Ubuntu Docker 最常用的运行环境之一本节将介绍如何在 Ubuntu 系统上安装 Docker并配置国内镜像加速
### 为什么推荐 APT 源安装而不是脚本
虽然 Docker 官方提供了便捷的安装脚本`get.docker.com`),但笔者在生产环境中**强烈推荐通过 Docker 官方 APT 仓库安装**原因如下
- **版本管理**通过 APT 仓库安装后可以像管理其他系统软件包一样升级回滚和锁定版本
- **安全更新**Docker 官方仓库会持续发布新版本和安全修复适合长期维护
- **一致性**团队更容易锁定同一版本避免在我的机器上可以运行的问题
- **卸载干净**APT 包管理系统会负责清理所有相关文件脚本安装的清理往往不够彻底
如果你只是想快速尝试 Docker脚本安装没有问题但一旦涉及持久运维APT 源是更成熟的选择
> 警告切勿在没有配置 Docker APT 源的情况下直接使用 apt 命令安装 Docker
### 3.1.1 准备工作
在开始安装之前我们需要确认系统版本是否满足要求并清理可能存在的旧版本
#### 系统要求
根据 Docker 官方安装文档当前受支持的 [Ubuntu](https://ubuntu.com/server) 64 位版本包括:
* Ubuntu Resolute Raccoon 26.04 (LTS)
* Ubuntu Questing Quokka 25.10
* Ubuntu Noble 24.04 (LTS)
* Ubuntu Jammy 22.04 (LTS)
> **警告**Ubuntu 20.04 LTS 已不在 Docker 当前支持列表中不建议用于新部署对于仍在运行 20.04 的生产系统应尽快升级到 22.04 LTS 24.04 LTS若短期内无法迁移可通过 Ubuntu Pro 获取操作系统层面的扩展安全维护ESM),但这并不改变 Docker 官方支持矩阵
Ubuntu LTS 版本上目前 Docker 支持 amd64arm64armhfppc64els390x 5 个平台而非 LTS 版本支持的平台通常较少同时LTS 版本会获得 5 年的升级维护支持这样的系统会获得更长期的安全保障因此在生产环境中推荐使用 LTS 版本
#### 卸载旧版本
Docker 官方建议先卸载可能冲突的非官方软件包
```bash
$ for pkg in docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc;
do
sudo apt remove $pkg;
done
```
### 3.1.2 使用 APT 安装
先安装基础依赖并准备 Docker 官方密钥目录
```bash
$ sudo apt update
$ sudo apt install ca-certificates curl
$ sudo install -m 0755 -d /etc/apt/keyrings
```
如果企业内网已经维护了受信任的软件包镜像可在后续步骤中替换 `URIs` 的域名默认建议优先以 Docker 官方仓库为准
为了确认所下载软件包的合法性需要添加仓库签名密钥
```bash
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
$ sudo chmod a+r /etc/apt/keyrings/docker.asc
```
然后向 `apt` 添加 Docker 仓库
```bash
$ sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF
```
> 如果需要测试频道可将 `Components: stable` 改为 `test`或改用 `test.docker.com` 脚本在测试环境验证
更新 APT 缓存并安装 Docker Engine 及常用 CLI 插件
```bash
$ sudo apt update
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```
### 3.1.3 使用脚本自动安装
在测试或开发环境中Docker 官方提供了便捷安装脚本但官方明确不建议把它作为生产环境的标准安装方式
在真正执行前建议先用 `--dry-run` 预览脚本动作
```bash
$ curl -fsSL 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.1.4 启动 Docker
```bash
$ sudo systemctl enable --now docker
```
### 3.1.5 建立 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.1.6 测试 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.1.7 镜像加速
如果在使用过程中发现拉取 Docker 镜像十分缓慢可以配置 Docker [国内镜像加速](3.9_mirror.md)