在 ubuntu20.04 安装 docker

1、替换清华源

替换 sources.list 里面的内容

sh 复制代码
sudo vim /etc/apt/sources.list
sh 复制代码
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse

2、更新包缓存和下载

sh 复制代码
sudo apt update
sh 复制代码
sudo apt upgrade

3、删除存在的 docker

sh 复制代码
sudo apt remove docker docker-engine docker.io containerd runc

查看版本

sh 复制代码
sudo docker --version

查看是否还存在 docker 服务

sh 复制代码
sudo dpkg -l | grep docker

4、安装 docker

安装 docker 依赖库

sh 复制代码
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release software-properties-common

导入 GPG key

sh 复制代码
sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

添加 docker apt 源

sh 复制代码
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

安装 docker

sh 复制代码
sudo apt install docker-ce docker-ce-cli containerd.io

5、验证安装

sh 复制代码
sudo docker --version
sh 复制代码
sudo docker run hello-world

6、FAQ

1)导入GPG key失败

本地下载 GPG

sh 复制代码
https://download.docker.com/linux/ubuntu/gpg

添加本地 GPG。注意替换本地路径

sh 复制代码
sudo apt-key add /root/docker/gpg

2)docker run hello-world 失败

可以自己从阿里云获取一个免费的专属地址 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

设置 registry mirror

sh 复制代码
sudo mkdir -p /etc/docker
sh 复制代码
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://w1m3bf6y.mirror.aliyuncs.com"]
}
EOF

重启 docker 服务

sh 复制代码
sudo systemctl restart docker

或者

sh 复制代码
service docker restart
相关推荐
JohnYan2 小时前
工作笔记 - CentOS7环境运行Bun应用
javascript·后端·容器
ezreal_pan2 小时前
Kubernetes 负载均衡现象解析:为何同一批次请求集中于单个 Pod
运维·云原生·k8s·traefik
朱皮皮呀2 小时前
Spring Cloud——服务注册与服务发现原理与实现
运维·spring cloud·eureka·服务发现·php
科大饭桶3 小时前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器
云边云科技4 小时前
零售行业新店网络零接触部署场景下,如何选择SDWAN
运维·服务器·网络·人工智能·安全·边缘计算·零售
城管不管4 小时前
Docker核心---数据卷(堵门秘籍)
运维·docker·容器
AOwhisky4 小时前
Linux 文本处理三剑客:awk、grep、sed 完全指南
linux·运维·服务器·网络·云计算·运维开发
xuanerya5 小时前
使用 SSH 方式克隆 GitHub 仓库没有权限解决办法
运维·ssh·github
BestAns5 小时前
Docker+Jenkins 自定义镜像指南
docker
runfarther5 小时前
搭建LLaMA-Factory环境
linux·运维·服务器·python·自然语言处理·ai编程·llama-factory