LV020-linux-docker安装
一、准备工作
1. Ubuntu
我使用的是ubuntu-24.04.3-desktop-amd64.iso这个版本。
2. VMware
因为我是在Windows11下,所以还装了一个虚拟机VMware:
产品:VMware® Workstation 17 Pro
版本:17.6.0 build-24238078二、开始安装Docker
1. 卸载旧版本docker
要是存在老版本docker的话,可以用以下命令卸载:
sudo apt-get remove docker docker-engine docker.io containerd runc我这里没有安装过就什么都没有了。
2. 使用Docker仓库安装
2.1 安装基本工具
sudo apt-get update
sudo apt-get install -y ca-certificates curl2.2 添加Docker的GPG密钥
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg -o docker.asc
sudo mkdir /etc/apt/keyrings/
sudo mv docker.asc /etc/apt/keyrings/docker.asc2.3 设置稳定版仓库
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
sudo apt-get update
# 或者
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null有的时候可能会因为网络问题出现下面的报错:
sumu@sumu-vm:~$ sudo apt-get update
命中:1 http://mirrors.aliyun.com/ubuntu focal InRelease
命中:2 http://mirrors.aliyun.com/ubuntu focal-security InRelease
命中:3 http://mirrors.aliyun.com/ubuntu focal-updates InRelease
命中:4 http://mirrors.aliyun.com/ubuntu focal-proposed InRelease
忽略:5 https://download.docker.com/linux/ubuntu focal InRelease
命中:6 http://mirrors.aliyun.com/ubuntu focal-backports InRelease
错误:7 https://download.docker.com/linux/ubuntu focal Release
Could not handshake: Error in the pull function. [IP: 18.172.31.28 443]
正在读取软件包列表... 完成
E: 仓库 “https://download.docker.com/linux/ubuntu focal Release” 没有 Release 文件。
N: 无法安全地用该源进行更新,所以默认禁用该源。
N: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。我是后来挂了梯子。
3. 安装 Docker Engine-Community
- 更新 apt 包索引:
sudo apt-get update- 安装最新版本的 Docker Engine-Community 和 containerd:
sudo apt-get install docker-ce docker-ce-cli containerd.ioTips:安装特定版本
要安装特定版本的 Docker Engine-Community,请先列出仓库中可用的版本:
bashapt-cache madison docker-ce然后使用第二列中的版本字符串安装特定版本:
bashsudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
4. Docker是否安装成功
4.1 运行状态
上面安装完毕后,我们通过下面的命令检查一下docker是否运行:
systemctl status docker
# 或者
service docker status【例】
sumu@sumu-vm:~$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset>
Active: active (running) since Sun 2025-11-02 19:03:03 CST; 8s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 13413 (dockerd)
Tasks: 10
Memory: 21.2M
CGroup: /system.slice/docker.service
└─13413 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/con>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.150663010+08>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.191768734+08>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.264944358+08>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.769443060+08>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.813157059+08>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.813288296+08>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.850087749+08>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.859045992+08>
11月 02 19:03:03 sumu-vm systemd[1]: Started Docker Application Container Engin>
11月 02 19:03:03 sumu-vm dockerd[13413]: time="2025-11-02T19:03:03.859174326+08>
lines 1-21/21 (END)这里显示active (running)表示已经在运行了。
4.2 版本号
docker -v【例】
sumu@sumu-vm:~$ docker -v
Docker version 28.1.1, build 4eba3775. 配置镜像源
Docker 默认从官方的 registry 下载镜像,但国内访问速度慢,甚至可能拉取镜像失败。可以使用阿里云、华为云、清华等提供的加速器。
- 创建 Docker 配置文件
sudo mkdir -p /etc/docker- 获取镜像源 registry-mirrors 地址后,编辑配置文件,添加以下内容:
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.imgdb.de",
"https://docker-0.unsee.tech",
"https://docker.hlmirror.com"
]
}
EOF- 重启 Docker 服务
sudo systemctl daemon-reload
sudo systemctl restart docker6. Docker服务启停命令
systemctl start docker # 启动 docker
systemctl stop docker # 关闭 docker
systemctl restart docker # 重启 docker
systemctl enable docker # 自启 docker7. 拉取镜像
7.1 用户权限
我们执行docker run相关的命令的时候肯呢个会报错:
docker: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Head "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied
Run 'docker run --help' for more information这个是说我们没有权限,在 Linux 系统中,Docker 默认需要 root 权限才能运行。如果希望普通用户也能使用 Docker,可以通过以下步骤赋予普通用户 Docker 权限。这个时候我们加上sudo或者配置一下用户组:
sudo cat /etc/group | grep docker # 检查是否存在 Docker 用户组
sudo groupadd docker # 如果不存在,则创建 Docker 用户组
sudo usermod -aG docker your_username # 将 your_username 用户添加到 Docker 用户组
newgrp docker # 使更改生效,使用以下命令更新用户组【例】
sumu@sumu-vm:~$ sudo cat /etc/group | grep docker
docker:x:998:
sumu@sumu-vm:~$ sudo usermod -aG docker sumu
sumu@sumu-vm:~$ newgrp docker
sumu@sumu-vm:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally出现最后一行表示在本地查找镜像了,说明用户的docker权限添加成功。
7.2 hello-world
输入以下指令测试 Docker 是否安装成功,可能会失败(失败的话一般是网络问题,我们可以配置一些国内的镜像源):
sumu@sumu-vm:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete
Digest: sha256:6dc565aa630927052111f823c303948cf83670a3903ffa3849f1488ab517f891
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/如果看到 "Hello from Docker!" 的欢迎信息,说明安装成功。
三、一个偷懒的办法
温馨提示:请记得先确认设置好Ubuntu的镜像源
懒惰的人可以直接通过以下命令安装+改源
bash <(curl -sSL https://linuxmirrors.cn/docker.sh)这个其实就是一个安装的shell脚本,把中间的一些步骤写在了脚本中。
参考资料:
Ubuntu 安装 Docker - Ubuntu 系统 Docker 安装教程 | Docker 中文文档
解决docker报Error response from daemon Get httpsregistry-1.docker.iov2错误-CSDN博客