s-blog

Ubuntu 安装 Docker

ssssmy · 2026-05-28 · 3 min · Ubuntu

1. 卸载旧版本

Ubuntu 自带的 Docker 版本太低,需要先卸载旧的再安装新的。

sudo apt-get remove docker docker-engine docker.io containerd runc

2. 更新软件包列表和已安装软件的版本

sudo apt update
sudo apt upgrade

3. 安装必要的证书并允许 apt 通过 HTTPS 使用存储库

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

4. 添加 Docker 的官方 GPG 密钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

5. 添加 Docker 官方库

# ARM64
sudo add-apt-repository \
   "deb [arch=arm64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# AMD64
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

6. 更新 Ubuntu 源列表

sudo apt update

7. 安装最新 Docker CE

sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

安装其他版本

apt-cache madison docker-ce

例如安装 5:27.0.3-1~ubuntu.22.04~jammy

sudo apt install docker-ce=5:27.0.3-1~ubuntu.22.04~jammy docker-ce-cli=5:27.0.3-1~ubuntu.22.04~jammy containerd.io

8-11. 服务管理与验证

# 查看状态
systemctl status docker

# 启动
sudo systemctl start docker

# 开机自启
sudo systemctl enable docker

# 查看版本
sudo docker version

# 测试
sudo docker run hello-world

12. 如果不成功,更换镜像地址

修改 /etc/docker/daemon.json(如果没有先创建文件):

{
  "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://dockerproxy.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.nju.edu.cn",
    "http://mirrors.aliyun.com",
    "https://hxv6d2ac.mirror.aliyuncs.com"
  ]
}
sudo systemctl daemon-reload
sudo systemctl restart docker

13. 可能的错误处理

permission denied

/Users/xxx/.docker/buildx/activity/desktop-linux: permission denied
chmod 755 /Users/xxx/.docker/buildx/activity/desktop-linux

Docker socket 权限拒绝

ERROR: permission denied while trying to connect to the Docker daemon socket

通常因为当前用户没有加入 Docker 用户组:

sudo usermod -aG docker ${user}
sudo groupadd docker
sudo gpasswd -a ${user} docker
newgrp docker

原文链接:https://www.ssssmy.com/notes/ubuntu-an-zhuang-docker