使用 apt 存储库安装
在新主机上首次安装 Docker Engine 之前,您需要设置 Docker 存储库。之后,您可以从存储库安装和更新 Docker。
设置存储库
- 更新apt包索引并安装包以允许apt通过 HTTPS 使用存储库:
#更新本地的软件包列表
sudo apt-get update
#安装必要的依赖项,包括 ca-certificates 和 curl
sudo apt-get install ca-certificates curl gnupg
问题
sudo apt-get update 报错
root@BM01-cyzx-sqmyfxyppt-001:/etc/docker# sudo apt-get update
Err:1 https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal InRelease
Temporary failure resolving 'mirrors.ustc.edu.cn'
Err:2 http://archive.ubuntu.com/ubuntu focal InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:5 http://archive.ubuntu.com/ubuntu focal-security InRelease
Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-security/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/dists/focal/InRelease Temporary failure resolving 'mirrors.ustc.edu.cn'
W: Some index files failed to download. They have been ignored, or old ones used instead.
解决办法:
ubuntu的服务器在国外,使用国内的软件源在下载时更新受到限制,将服务地址修改成国内的地址
找到软件源文件
cd /etc/apt
备份
sudo tar -zcvf sources.list.tar.gz sources.list
修改源文件地址
输入以下命令:sudo vim sources.list
将地址archive.ubuntu.com 更改为 mirrors.aliyun.com
按ESC,然后 wq保存退出
重新测试 sudo apt-get update
root@BM01-cyzx-sqmyfxyppt-001:/etc/apt# sudo apt-get update
Err:1 https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu focal InRelease
Temporary failure resolving 'mirrors.ustc.edu.cn'
Err:2 http://mirrors.aliyun.com/ubuntu focal InRelease
Temporary failure resolving 'mirrors.aliyun.com'
Err:3 http://mirrors.aliyun.com/ubuntu focal-updates InRelease
Temporary failure resolving 'mirrors.aliyun.com'
Err:4 http://mirrors.aliyun.com/ubuntu focal-backports InRelease
Temporary failure resolving 'mirrors.aliyun.com'
Err:5 http://mirrors.aliyun.com/ubuntu focal-security InRelease
Temporary failure resolving 'mirrors.aliyun.com'
Reading package lists... Done
W: Failed to fetch http://mirrors.aliyun.com/ubuntu/dists/focal/InRelease Temporary failure resolving 'mirrors.aliyun.com'
W: Failed to fetch http://mirrors.aliyun.com/ubuntu/dists/focal-updates/InRelease Temporary failure resolving 'mirrors.aliyun.com'
W: Failed to fetch http://mirrors.aliyun.com/ubuntu/dists/focal-backports/InRelease Temporary failure resolving 'mirrors.aliyun.com'
W: Failed to fetch http://mirrors.aliyun.com/ubuntu/dists/focal-security/InRelease Temporary failure resolving 'mirrors.aliyun.com'
W: Failed to fetch https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/dists/focal/InRelease Temporary failure resolving 'mirrors.ustc.edu.cn'
W: Some index files failed to download. They have been ignored, or old ones used instead.
解决办法
修改/etc/resolv.conf,在里边加上如下:
nameserver 8.8.8.8
nameserver 114.114.114.114
可以ping通www.baidu.com之后,执行sudo apt-get update
- 添加 Docker 的官方 GPG 密钥:
#创建 /etc/apt/keyrings 目录,该目录用于存储 Docker 的 GPG 密钥
sudo install -m 0755 -d /etc/apt/keyrings
#从 Docker 官方网站下载 GPG 密钥并将其保存到 /etc/apt/keyrings/docker.asc 文件中。
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
#更改密钥文件的权限以确保只有管理员可以访问它
sudo chmod a+r /etc/apt/keyrings/docker.gpg
问题:官方采用的软件源,慢,下载不下来
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
解决办法:可更换软件源
阿里云软件源:
sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
中科大
sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
- 使用以下命令设置存储库:
# Add the repository to Apt sources:
#将 Docker APT 源添加到系统中,并保存在 /etc/apt/sources.list.d/ 目录下的 docker.list 文件中。
#此命令会自动检测系统架构和发行版名称,并使用它们来构建正确的源地址。
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
问题
Cannot initiate the connection to download.docker.com:443 (2a03:2880:f112:83:face:b00c:0:25de). - connect (101: Network is unreachable) Could not connect to download.docker.com:443 (108.160.167.147), connection timed out
Reading package lists... Done
W: Failed to fetch https://download.docker.com/linux/ubuntu/dists/focal/InRelease Cannot initiate the connection to download.docker.com:443 (2a03:2880:f112:83:face:b00c:0:25de). - connect (101: Network is unreachable) Could not connect to download.docker.com:443 (108.160.167.147), connection timed out
W: Some index files failed to download. They have been ignored, or old ones used instead.
解决方案是:找一个国内的镜像站,换源。 采用的是中科大镜像源。
将官方安装文档中的命令换为如下命令:
sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装 Docker 引擎
- 更新apt包索引:
sudo apt-get update
- 安装 Docker Engine、containerd 和 Docker Compose
要安装最新版本,请运行:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- Li通过运行映像验证 Docker 引擎安装是否成功 hello-world。
sudo docker run hello-world