问题背景:最近需要安装docker,搜到的解决方案都是从docker.com下载,完全没有考虑到国内会被墙的问题,“彗星来了”博主分享了他使用阿里镜像源安装Docker的方法,在此记录一下。
一、卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
二、安装依赖工具
sudo apt-get update
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent
三、添加dockerGPG密钥
# 官方源
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 阿里源
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
四、设置docker APT源
# 官方源
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 阿里源
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
五、更新APT软件包索引
sudo apt-get update
六、安装Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
七、验证Docker是否成功安装
sudo docker --version
补充:使用脚本自动安装:https://get.docker.com/
# 验证脚本是否支持该操作系统
make shellcheck
# 从get.docker.com下载安装
curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh
# 也可以通过--mirror选项使用国内源进行安装
curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
八、启动Docker
sudo systemctl enable docker
sudo systemctl start docker
九、创建docker用户组
注:出于安全考虑,Linux系统不要直接使用root用户。最好是将需要使用Docker的用户加入docker用户组
# 建立docker组
sudo groupadd docker
# 将当前用户加入docker组
sudo usermod -aG docker $USER
十、测试Docker是否安装正确
docker run --rm hello-world
十一、查看正在运行的Docker容器列表
sudo docker ps
十二、Docker安装常见报错
1.GPG error: https://download.docker.com/linux/ubuntu focal InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8
通常是缺少Docker的GPG密钥引起的。重新输入”添加Docker的GPG密钥”命令即可。
2.E: The repository ‘https://download.docker.com/linux/ubuntu focal Release’ does not have a Release file.
可能是Docker APT源配置不正确。确保已正确设置APT源,重新配置试下。
3.Failed to fetch https://download.docker.com/linux/ubuntu/dists/focal/InRelease Could not connect to download.docker.com:443 (13.35.250.16), connection timed out
网络连接问题。检查是不是防火墙或代理阻止了Docker访问。
4.E: Unable to locate package docker-ce
Docker软件包无法找到。确保正确配置Docker APT源,然后运行”sudo apt-get update”命令,更新软件包。
5.The following packages have unmet dependencies: docker-ce : Depends: containerd.io (>= 1.2.2-3) but it is not going to be installed
Docker依赖项问题。需要运行”sudo apt-get install -y containerd.io”命令,安装containerd.io。
6.Got permission denied while trying to connect to the Docker daemon socket
用户权限问题。需要运行”sudo usermod -aG docker $USER”命令,将用户添加到docker组中,需要注销并重新登录才能生效。
如果还遇到了current user does not have permission to run docker,可以参考一下这篇博客
作者: 彗星来了
链接: https://www.huixinglaile.com/archives/117a5c58.html
来源: 彗星来了