本文将详细介绍如何在操作系统 Ubuntu 22.04.3 LTS 中,在线安装 Docker。
实战服务器配置 (架构 1:1 复刻小规模生产环境,配置略有不同)
主机名 | IP | CPU(核) | 内存(GB) | 系统盘(GB) | 数据盘(GB) | 用途 |
---|---|---|---|---|---|---|
docker-node-1 | 192.168.9.81 | 4 | 16 | 40 | 100 | Docker 节点 1 |
合计 | 1 | 4 | 16 | 40 | 100 |
实战环境涉及软件版本信息
- 操作系统:Ubuntu 22.04.3 LTS
- Docker:27.0.3
1. 前置条件
Ubuntu 安装 Docker 有两种方式:
- 使用 Docker 软件源,在线安装
- 使用 Docker 二进制安装包,离线安装
本文实战演示如何使用 Docker 软件源,在线安装 Docker。整个安装过程还是比较简单的,如果安装失败基本上都是网络原因造成的。
1.1 操作系统基础配置
- 挂载一块独立的数据盘到 /data(建议)
- 创建 Docker 数据目录
mkdir /data/docker
1.2 替换系统软件源
为了加快安装速度,本文使用清华源作为系统和 Docker 的软件源仓库。
- 备份系统默认源
cp /etc/apt/sources.list /etc/apt/sources.list.bak
- 用下面的内容替换原始文件
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
- 更新索引信息
apt-get update
2. 安装 Docker
以下所有操作均在 root 用户下完成,并假定操作系统是新安装的干净环境。
2.1 安装依赖
apt-get install ca-certificates curl gnupg
2.2 信任 Docker 的 GPG 公钥并添加仓库
我们使用清华大学开源软件镜像站的软件仓库 mirrors.tuna.tsinghua.edu.cn
,作为安装源。
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
2.3 安装最新版的 Docker
apt-get update
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
2.4 自定义配置文件
创建配置文件 /etc/docker/daemon.json
cat > /etc/docker/daemon.json <<EOF
{
"data-root": "/data/docker",
"exec-opts": [
"native.cgroupdriver=systemd"
],
"log-level": "info",
"log-opts": {
"max-size": "100m",
"max-file": "5"
},
"storage-driver": "overlay2"
}
EOF
2.5 启动 Docker
启动 Docker 服务,并设置开机自启。
systemctl restart docker
systemctl enable docker --now
2.6 验证 Docker
- 查看 Docker 信息
docker info
- 使用镜像
hello-world
创建测试容器
docker run --rm hello-world
- 正确执行输出结果如下:
$ docker run --rm hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
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/