1. 更新软件包
更新软件包索引首先确保你的软件包索引是最新的:
apt-get update
2. 安装必要的依赖包
安装一些必要的依赖包,以便apt可以通过HTTPS使用仓库:
apt install -y apt-transport-https ca-certificates curl software-properties-common
3. 添加官方GPG密钥
添加Docker的官方GPG密钥来验证下载的软件包:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4. 设置稳定版Docker仓库
将稳定的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/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
如果设置为阿里的可能会遇到密钥问题,解决方法:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
5. 再次更新软件包索引
再次更新软件包索引以包含新的Docker仓库:
sudo apt-get update
6. 安装Docker CE(社区版)
安装最新版本的Docker社区版:
apt install -y docker-ce docker-ce-cli containerd.io
7. 验证Docker是否正确安装
通过运行hello-world镜像来验证Docker是否已成功安装并可以正常工作:
docker run hello-world
8. 配置Docker加速
为了加快Docker镜像的下载速度,你可以配置国内的Docker镜像加速器。编辑或创建/etc/docker/daemon.json文件,加入以下内容:
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
其他加速服务商
服务商 | 加速地址 |
---|---|
腾讯云 | https://mirror.ccs.tencentyun.com |
网易云 | https://hub-mirror.c.163.com |
中科大 | https://docker.mirrors.ustc.edu.cn |
目前,国内已知常规的镜像源都被屏蔽掉了,我都会使用第三方镜像源
重启服务生效
systemctl daemon-reload && systemctl restart docker
9. Docker基本操作
#用默认的url拉取镜像
docker pull hello-world
#尝试用指定url去拉取镜像nginx
docker pull docker.imgdb.de/nginx
#查看docker镜像
docker images
#查看docker端口使用情况
docker ps
# 生成镜像包(当前目录生成tar文件)
docker save -o hello-world.tar hello-world:latest
# 从服务器下载 hello-world.tar 文件到本地,可以使用 sz 命令
sz hello-world.tar
# 上传本地 hello-world.tar 文件到另一台服务器,可以使用 rz 命令
rz
# 如果没有安装,记得使用命令安装
apt install -y lrzsz
# 加载镜像(需提前安装Docker)
docker load -i /path/to/hello-world.tar
# 验证镜像
docker images | grep hello-world
# 用docker rmi 删除镜像 REPOSITORY名称 或者 IMAGE ID 号
docker rmi hello-world:latest
(ID重复时 可以使用 -f 参数 全删)docker rmi -f
root@Ubuntu-24-04-1:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.imgdb.de/hello-world latest 74cc54e27dc4 3 weeks ago 10.1kB
hello-world latest 74cc54e27dc4 3 weeks ago 10.1kB
root@Ubuntu-24-04-1:~# docker rmi -f 74cc54e27dc4
Untagged: docker.imgdb.de/hello-world:latest
Untagged: docker.imgdb.de/hello-world@sha256:e0b569a5163a5e6be84e210a2587e7d447e08f87a0e90798363fa44a0464a1e8
Deleted: sha256:74cc54e27dc41bb10dc4b2226072d469509f2f22f1a3ce74f4a59661a1d44602
Deleted: sha256:63a41026379f4391a306242eb0b9f26dc3550d863b7fdbb97d899f6eb89efe72
root@Ubuntu-24-04-1:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root@Ubuntu-24-04-1:~#
完