Docker 在 Ubuntu(国内网络)安装及问题解决总结

1. 背景

在中国大陆网络环境下,直接使用 Docker 官方源安装和拉取镜像会遇到:

  • GPG 密钥下载失败(连接被重置)
  • 仓库添加后签名验证失败
  • 拉取镜像失败(connection refused / timeout)

2. 完整安装步骤(2025 年 12 月最新可用方案)

bash 复制代码
# 安装依赖
sudo apt update
sudo apt install ca-certificates curl gnupg

# 创建 keyrings 目录
sudo install -m 0755 -d /etc/apt/keyrings

# 使用阿里云镜像下载并转换 GPG 密钥(推荐,稳定)
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
  sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# 设置权限
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# 添加阿里云 Docker 仓库
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 更新包索引并安装 Docker
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

3. 常见问题及解决

问题1:权限问题(permission denied ... docker.sock)

原因:普通用户无权访问 Docker daemon。

解决:

bash 复制代码
sudo usermod -aG docker $USER

执行后必须注销重登录 (或重启电脑)才能生效。

生效后即可直接使用 docker 命令,无需加 sudo。

问题2:拉取镜像失败(hello-world 无法下载)

原因:官方 registry-1.docker.io 被干扰,旧加速器失效。

解决:配置最新有效的镜像加速器(2025 年 12 月亲测可用)

bash 复制代码
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://hub.rat.dev",
    "https://dockerproxy.net",
    "https://docker.xuanyuan.me",
    "https://proxy.vvvv.ee",
    "https://docker.m.daocloud.io"
  ]
}
EOF

# 重启 Docker 使配置生效
sudo systemctl daemon-reload
sudo systemctl restart docker

验证配置:

bash 复制代码
docker info | grep -A 10 "Registry Mirrors"

4. 最终验证安装成功

bash 复制代码
docker run hello-world

成功输出包含以下内容即表示完全正常:

复制代码
Hello from Docker!
This message shows that your installation appears to be working correctly.

运行后查看:

bash 复制代码
docker images          # 能看到 hello-world
docker ps -a           # 能看到一个 Exited 的 hello-world 容器(正常,运行完就退出)

5. 额外建议

  • docker ps 只显示正在运行的容器,hello-world 运行完会自动退出,所以默认看不到是正常的。
  • 用 docker ps -a 查看所有容器(包括已停止的)。
  • 加速器偶尔会变化,如某个失效,Docker 会自动尝试下一个。多配置几个最保险。

至此,Docker 在国内 Ubuntu 环境下的安装和常见问题全部解决!🎉

相关推荐
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
分布式存储与RustFS4 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
wuyk5554 天前
《WiFi 嵌入式物联网开发全套实战》| 第 16 章 ESP32 AP+STA 双模共存原理与工程坑点
网络·stm32·物联网
玉&心4 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing4 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
guo_wen_qiang4 天前
上传本地镜像到harbor中
docker·容器·持续部署
羑悻的小杀马特4 天前
Dockerfile 全景指南:从入门编写dockerfile到生产环境镜像优化实战,一步步带你从啃透制作指令到镜像制作落地!
docker·dockerfile·镜像制作
BianHuanShiZhe4 天前
docker常见命令
docker
QYRdata4 天前
年均增速24.2%!机器人数据湖未来六年增长动能强劲
网络·机器人·服务发现
CHENKONG_CK4 天前
破解制鞋打磨痛点:RFID赋能去毛刺工序自动化升级
网络·单片机·嵌入式硬件·网络协议·tcp/ip