[Linux服务器] Linux安装Docker教程(详解)

70 0
Honkers 昨天 17:56 | 显示全部楼层 |阅读模式

如果想要系统学习docker,建议进入官方文档中学习:docker官方文档

Ubuntu系统的同学请看这篇 Ubuntu24.04无脑安装docker(含图例)

一. 基本概念

  • Docker Desktop 和 Docker Engine 有什么区别?

Docker Desktop for Linux 提供用户友好的图形界面,可简化容器和服务的管理。它包括 Docker Engine,因为这是为 Docker 容器提供支持的核心技术。适用于 Linux 的 Docker Desktop 还附带了 Docker Scout 和 Docker Extensions 等附加功能。

二. 删除旧版本(可选)

  1. yum remove docker \
  2. docker-client \
  3. docker-client-latest \
  4. docker-common \
  5. docker-latest \
  6. docker-latest-logrotate \
  7. docker-logrotate \
  8. docker-selinux \
  9. docker-engine-selinux \
  10. docker-engine \
  11. docker-ce
复制代码

三. 开始安装

  1. 安装所需的软件包 yum-utils,device-mapper-persistent-data和lvm2
  1. yum install -y yum-utils device-mapper-persistent-data lvm2
复制代码

yum-utils:
yum-utils 是一组为 YUM(Yellowdog Updater Modified)包管理器提供额外功能的工具。它包括了多个实用程序,如 yum-config-manager,这个工具可以用来方便地添加和管理 YUM 软件源。需要使用 yum-config-manager 来设置 Docker 的官方仓库。
device-mapper-persistent-data:
这个软件包提供了支持设备映射器(device mapper)驱动的工具,特别是用于持久化数据的逻辑卷管理。Docker 使用 device mapper 作为存储驱动之一,用于管理容器的文件系统层。device-mapper-persistent-data 包含了必要的工具来处理这些底层的文件系统操作,确保容器的数据能够被正确且高效地管理。
lvm2:
LVM (Logical Volume Manager) 是 Linux 下的一种磁盘分区管理技术,允许创建、调整大小和管理磁盘分区,即逻辑卷。LVM 提供了灵活性,例如可以在不丢失数据的情况下调整分区大小。lvm2 包含了管理和配置 LVM 所需的命令行工具。Docker 可以利用 LVM 和 device mapper 驱动来更有效地管理容器的存储空间,特别是在需要频繁创建和删除容器的环境中。

报错请点击这里查看
2. 更换docker镜像仓库 (三个任选其一)

  1. # 官方源 (慢 不推荐)
  2. sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  3. # 阿里云 (推荐)
  4. sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  5. # 清华大学源 (推荐)
  6. sudo yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
复制代码
  1. 安装docker
  1. # 安装docker,docker-cli
  2. sudo yum -y install docker-ce docker-ce-cli containerd.io
复制代码
  1. 配置docker加速 (现在配置阿里云容器镜像加速需要是阿里云设备,我是本地虚拟机,所以此处不使用阿里的镜像加速了)
  1. vi /etc/docker/daemon.json
  2. # 直接复制进去并保存 (2025.3.7最新可用源,看到这里的同学请评论一下是否还有效,无效博主会来更新)
  3. {
  4. "registry-mirrors": [
  5. "http://docker.1ms.run",
  6. "http://docker.mybacc.com"
  7. ]
  8. }
  9. # 启动/重启docker
  10. sudo systemctl daemon-reload
  11. #如果还没启动
  12. sudo systemctl start docker
  13. #如果已经启动
  14. sudo systemctl restart docker
  15. # 验证镜像加速是否修改 查看Registry Mirrors部分
  16. sudo docker info
复制代码

四. 启动和验证

  1. # 启动docker 上一步启动过了就不用再重复执行
  2. sudo systemctl start docker
  3. # docker开机自启
  4. systemctl enable docker
  5. # 拉取测试镜像 验证
  6. sudo docker pull hello-world
  7. // 执行hello-world
  8. sudo docker run hello-world
复制代码

五. 问题总结

yum安装软件包时报错

[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=vag error was
14: curl#6 - “Could not resolve host: mirrorlist.centos.org; Unknown error”
One of the configured repositories failed (Unknown),
and yum doesn’t have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work “fix” this:
1. Contact the upstream for the repository and get them to fix the problem.
2. Reconfigure the baseurl/etc. for the repository, to point to a working
upstream. This is most often useful if you are using a newer
distribution release than is supported by the repository (and the
packages for the previous distribution release still work).
3. Run the command with the repository temporarily disabled
yum --disablerepo= …
4. Disable the repository permanently, so yum won’t use it by default. Yum
will then just ignore the repository until you permanently enable it
again or use --enablerepo for temporary usage:
yum-config-manager --disable
or
subscription-manager repos --disable=
5. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
compromise:
yum-config-manager --save --setopt=.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7/x86_64

错误原因:

centos7已停止运营,已被归档,需要更换yum源

解决方案:更换为阿里源或其他国内源

  1. cd /etc/yum.repos.d
  2. # 备份原有的源
  3. cp CentOS-Base.repo CentOS-Base.repo.back
  4. #下载国内源的 YUM 配置文件 (本文采用阿里源,需要其他可自行百度)
  5. sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  6. # 如果没有wget 可以使用curl命令
  7. sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  8. # 清理 YUM 缓存
  9. sudo yum clean all
  10. sudo yum makecache
  11. # 验证新源是否可用 会看到都是阿里的源
  12. sudo yum repolist
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行