环境说明
本文详细介绍在 Ubuntu 24.04 系统上安装 Docker 的完整流程,包括:
- 配置阿里云镜像源加速下载
- 配置国内 Docker 镜像加速器
- 添加当前用户到 docker 组,避免每次使用 sudo
- 验证安装和测试
前置准备
在开始之前,建议:
- 系统已经安装完成
- 使用普通用户登录(避免一直使用 sudo)
- 系统网络连接正常
步骤 1:卸载旧版本(如果有)
如果系统之前安装过旧版本的 Docker,建议先卸载:
# 卸载可能存在的旧版本
code sudo apt remove docker docker-engine docker.io containerd runc 2>/dev/null
# 清理残留配置
code sudo apt purge docker docker-engine docker.io containerd runc 2>/dev/null
# 清理 APT 缓存
code sudo apt autoremove -y
步骤 2:更新软件包索引
sudo apt update
在安装软件前执行此命令,刷新本地软件包索引。
步骤 3:安装依赖包
sudo apt install -y ca-certificates curl gnupg
安装必要的依赖:
ca-certificates:允许系统信任 HTTPS 证书curl:命令行下载工具gnupg:GPG 密钥管理工具
步骤 4:创建 GPG 密钥目录
sudo install -m 0755 -d /etc/apt/keyrings
创建用于存储 GPG 密钥的目录。
步骤 5:下载 GPG 密钥(使用阿里云镜像)
sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
下载 Docker 官方的 GPG 密钥,保存到指定目录。使用阿里云镜像加速下载。
步骤 6:设置密钥文件权限
sudo chmod a+r /etc/apt/keyrings/docker.asc
为所有用户添加可读权限,确保所有用户都能验证软件包。
步骤 7:添加 Docker 软件源(使用阿里云镜像)
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] http://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
将 Docker 软件源添加到系统中。使用阿里云镜像地址,并自动适配 Ubuntu 24.04 的代号。
步骤 8:更新软件包索引
sudo apt update
刷新软件包列表,让系统知道新添加的 Docker 源。
步骤 9:安装 Docker
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
安装 Docker 及其所有组件:
docker-ce:Docker 社区版(核心引擎)docker-ce-cli:Docker 命令行工具containerd.io:容器运行时docker-buildx-plugin:构建插件(多平台构建)docker-compose-plugin:Docker Compose 插件
步骤 10:验证安装
docker -v
查看 Docker 版本,确认安装成功。
步骤 11:配置国内镜像源加速
3.1 创建配置文件
sudo mkdir -p /etc/docker
code sudo vim /etc/docker/daemon.json
3.2 添加镜像源配置
将以下内容添加到 daemon.json 文件中:
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"http://hub-mirror.c.163.com",
"https://docker.nju.edu.cn"
]
}
常用国内镜像源:
- DaoCloud:https://docker.m.daocloud.io(老牌稳定)
- 网易云:http://hub-mirror.c.163.com(多节点覆盖)
- 南京大学:https://docker.nju.edu.cn(支持多种仓库)
- 中科大:https://docker.mirrors.ustc.edu.cn
- Docker 中国官方:https://registry.docker-cn.com
3.3 重启 Docker 服务
sudo systemctl restart docker
3.4 验证配置
docker info | grep -A 5 "Registry Mirrors"
步骤 12:添加当前用户到 docker 组
4.1 查看用户组
grep docker /etc/group
如果没有 docker 组,会自动创建。
4.2 添加用户
sudo usermod -aG docker $USER
将当前用户添加到 docker 组。
4.3 刷新组权限
有三种方式刷新组权限:
- 注销后重新登录(推荐)
- 打开新的终端窗口
- 执行
newgrp docker命令
4.4 测试(无需 sudo)
docker ps
执行 docker 命令不再需要 sudo。
步骤 13:设置开机自启
sudo systemctl enable docker
设置 Docker 服务开机自动启动。
步骤 14:测试 Docker
docker run hello-world
拉取并运行 hello-world 镜像,验证 Docker 正常工作。
常用命令
# 查看版本
docker -v
# 查看服务状态
sudo systemctl status docker
# 启动服务
sudo systemctl start docker
# 停止服务
sudo systemctl stop docker
# 重启服务
sudo systemctl restart docker
# 查看镜像
docker images
# 查看容器
docker ps
# 查看所有容器
docker ps -a
# 查看日志
docker logs [container_id]
# 清理未使用的镜像
docker image prune -a
# 清理未使用的容器
docker container prune
常见问题
问题 1:apt update 报错
# 检查 docker.list 文件是否存在
cat /etc/apt/sources.list.d/docker.list
# 检查格式是否正确
sudo apt update
问题 2:权限被拒绝
# 确认用户在 docker 组中
groups
# 重新登录或打开新终端
code newgrp docker
问题 3:拉取镜像失败
# 检查镜像源配置
cat /etc/docker/daemon.json
# 重启 Docker 服务
sudo systemctl restart docker
# 测试拉取镜像
docker pull hello-world
总结
通过以上步骤,您已经成功在 Ubuntu 24.04 上安装并配置了 Docker。使用阿里云镜像源可以大幅提升软件包下载速度,国内镜像加速器可以解决 Docker Hub 访问问题。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END












暂无评论内容