Ubuntu 24.04 Docker 部署笔记
日期:2025年03月18日 | 系统版本:Ubuntu 24.04 LTS
一、安装 Docker
1. 更新系统并安装依赖
sudo apt update && sudo apt upgrade -y
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
注释:依赖项支持 HTTPS 传输和密钥管理。
2. 添加 Docker 国内镜像源与密钥(推荐阿里云或清华源)
# 阿里云镜像源(密钥与仓库)
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
注释:
• 密钥路径需与仓库配置的 signed-by
参数一致。
• 若密钥路径报错,检查 /etc/apt/keyrings/
或 /usr/share/keyrings/
目录。
3. 安装 Docker 核心组件
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
组件说明:
• docker-ce
:Docker 引擎
• docker-buildx-plugin
:多平台镜像构建工具
• docker-compose-plugin
:容器编排工具
二、配置与优化
1. 启动服务并设置开机自启
sudo systemctl start docker
sudo systemctl enable docker
验证服务状态:
systemctl status docker | grep "Active:" # 应显示 "active (running)"
2. 允许非 root 用户操作 Docker
sudo usermod -aG docker $USER
newgrp docker # 立即生效,无需重启
验证权限:
docker ps # 无报错则成功
3. 配置国内镜像加速器
# 编辑配置文件
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://mirror.ccs.tencentyun.com", "https://docker.m.daocloud.io"]
}
EOF
# 重启生效
sudo systemctl restart docker
验证镜像加速:
docker info | grep "Registry Mirrors" # 显示配置的镜像地址
三、验证与测试
1. 运行测试容器
docker run --rm hello-world
预期输出:
Hello from Docker!
This message shows your installation appears to be working correctly.
2. 指定镜像源拉取测试(如 hub.rat.dev
)
docker pull hub.rat.dev/library/hello-world
docker run --rm hub.rat.dev/library/hello-world
注释:若镜像源不可用,尝试 docker.m.daocloud.io
等替代源。
四、常用命令速查
功能 | 命令 |
---|---|
查看 Docker 版本 | docker --version |
列出本地镜像 | docker images |
启动/停止容器 | docker start/stop <容器名> |
查看容器日志 | docker logs -f <容器名> |
删除无用镜像/容器 | docker system prune -a (谨慎操作) |
五、故障排查与卸载
1. 常见问题
• 密钥路径错误:检查 /etc/apt/sources.list.d/docker.list
中的 signed-by
路径。
• 镜像拉取失败:
• 检查 /etc/docker/daemon.json
格式(需 JSON 无注释)。
• 临时切换镜像源:docker pull docker.m.daocloud.io/library/hello-world
。
2. 卸载 Docker
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
sudo rm -rf /var/lib/docker /var/lib/containerd # 删除残留数据
参考来源:
• 密钥与镜像源配置
• 服务管理与权限设置
• 镜像加速器优化