系统版本
本文以Ubuntu20.05系统为例安装docker,Ubuntu官方下载地址。
检查卸载老版本docker
ubuntu下自带了docker的库,不需要添加新的源。
但是ubuntu自带的docker版本太低,需要先卸载旧的再安装新的。
注:docker的旧版本不一定被称为docker,docker.io 或 docker-engine也有可能,所以我们卸载的命令为:
apt-get remove docker docker-engine docker.io containerd runc
若提示无权限,则使用以下命令
sudo apt-get remove docker docker-engine docker.io containerd runc
安装步骤
1.更新软件包
在终端中执行以下命令来更新Ubuntu软件包列表和已安装软件的版本
sudo apt update
sudo apt upgrade
2.安装docker依赖
apt-get install ca-certificates curl gnupg lsb-release
3.添加Docker官方GPG密钥
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
4.添加Docker软件源
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
5.安装docker
apt-get install docker-ce docker-ce-cli containerd.io
6.配置用户组(可选)(重新登入生效)
sudo usermod -aG docker $USER
7.安装docker
systemctl start docker
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
service docker restart
8.验证
sudo docker run hello-world
因为我们之前没有拉取过hello-world
,所以运行命令后会出现本地没有该镜像,并且会自动拉取的操作。
9.查看版本
sudo docker version
10.验证
sudo docker images
出现上述情况,即表示我们成功在Ubuntu系统上安装了docker。
常见问题
1.docker: Get “https://registry-1.docker.io/v2/”: net/http: request canceled while waiting for connection
检查网络
curl -Iv https://registry-1.docker.io # 检查HTTPS访问
编辑Docker配置文件(docker加速镜像会失效,请自行获取)
Docker/DockerHub 国内镜像源/加速列表(3月19日更新-长期维护)
#!/bin/sh
cat <<-EOF > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.xuanyuan.me"
]
}
EOF
systemctl daemon-reload
systemctl restart docker
同步时间
sudo apt install ntpdate
sudo ntpdate pool.ntp.org
# 或使用timedatectl
sudo timedatectl set-ntp on