0


CentOS 上安装 Docker 的详细指南

CentOS 上安装 Docker 的详细指南

一、前置条件

在开始安装 Docker 之前,确保以下几点:

  1. 操作系统版本:本文以 CentOS 7 和 CentOS 8 为例。如果你使用的是其他版本,请参考官方文档。
  2. 权限要求:需要使用 root 用户或具有 sudo 权限的用户。
二、卸载旧版本

首先检查并卸载系统中可能存在的旧版本 Docker,旧版本的 Docker 称为

docker

docker-engine

sudo yum remove docker\
                docker-client \
                docker-client-latest \
                docker-common \
                docker-latest \
                docker-latest-logrotate \
                docker-logrotate \
                docker-engine
三、设置 Docker 仓库

为了能够安装 Docker,首先需要设置 Docker 的官方仓库。

  1. 安装必备软件包:这些软件包允许 yum 使用 HTTPS 进行传输。
sudo yum install-y yum-utils \
  device-mapper-persistent-data \
  lvm2
  1. 添加 Docker 仓库
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
四、安装 Docker

使用

yum

安装 Docker CE(社区版):

sudo yum install docker-ce docker-ce-cli containerd.io

系统会自动下载并安装所有的依赖包。

常见错误及解决方法:
  • 错误 1:GPG Key 验证失败- 描述:在安装过程中,可能会遇到 GPG Key 验证失败的情况。- 解决方案sudo yum install-y--nogpgcheck docker-ce
  • 错误 2:依赖冲突- 描述:有时可能会遇到依赖包之间的冲突。- 解决方案: - 使用 --skip-broken 参数:sudo yum install docker-ce --skip-broken- 如果仍然失败,可以手动解决冲突或更新相关软件包。
五、启动 Docker

安装完成后,需要启动 Docker 并设置开机自启:

sudo systemctl start dockersudo systemctl enabledocker

可以使用以下命令检查 Docker 是否正确启动:

sudo systemctl status docker
六、运行 Hello World 容器

为了验证 Docker 安装是否成功,可以运行一个简单的

hello-world

容器:

sudodocker run hello-world

如果一切正常,系统将输出类似以下的消息:

Hello from Docker!
This message shows that your installation appears to be working correctly.
常见错误及解决方法:
  • 错误 1:Cannot connect to the Docker daemon- 描述:可能是 Docker 服务未启动或用户没有权限。- 解决方案: - 确保 Docker 服务已启动:sudo systemctl start docker- 使用 sudo 运行 Docker 命令或将用户添加到 docker 组:sudousermod-aGdocker$USER
  • 错误 2:Could not resolve host: mirrorlist.centos.org- 描述:无法解析CentOS镜像域名。- 解决方案: 这个错误表明系统无法解析 mirrorlist.centos.org 的域名,可能是网络连接问题或 DNS 配置问题。以下是几种不同情况的解决方法:- 检查网络连接确保服务器的网络连接正常:ping-c41.0.0.1如果没有回应,检查网络配置和连接。- 更改 DNS 配置编辑 /etc/resolv.conf 文件,添加或更换 DNS 服务器:sudovi /etc/resolv.conf添加以下内容:nameserver 223.5.5.5nameserver 1.0.0.1保存并退出文件后,再试一次。- 手动配置 CentOS 仓库如果无法使用默认的镜像列表,可以直接指定可用的仓库 URL。1. 备份并编辑仓库配置文件sudocp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.baksudovi /etc/yum.repos.d/CentOS-Base.repo1. ****注释掉 mirrorlist 行,并添加 baseurl****。以下示例使用阿里云镜像,您可以根据需要替换为其他镜像站点:# CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and# geographically close to the client. You should use this for CentOS updates# unless you are manually picking other mirrors.## If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead.##[base]name=CentOS-$releasever - Base#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#released updates [updates]name=CentOS-$releasever - Updates#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that may be useful[extras]name=CentOS-$releasever - Extras#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that extend functionality of existing packages[centosplus]name=CentOS-$releasever - Plus#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-71. 保存并退出文件。2. 更新 yum 缓存清除 yum 缓存并更新:sudo yum clean allsudorm-rf /var/cache/yumsudo yum makecache1. 测试仓库连接尝试安装 yum-utils 以确认问题是否解决:sudo yum install-y yum-utils
七、配置 Docker 镜像加速

在中国大陆,直接访问 Docker 官方镜像库的速度较慢。可以使用国内的镜像加速服务。

  1. 修改 Docker 配置文件
sudomkdir-p /etc/docker
  1. 编辑 /etc/docker/daemon.json 文件,并添加如下内容:
{"registry-mirrors":["https://<your-mirror-url>"]}

以下是一些常用的 Docker registry mirrors:

  1. DaoCloud 镜像加速器: https://docker.m.daocloud.io
  2. DockerProxy: https://dockerproxy.com
  3. USTC (中国科学技术大学) 镜像: https://docker.mirrors.ustc.edu.cn
  4. 南京大学镜像: https://docker.nju.edu.cn
  5. 阿里云加速器: https://registry.cn-hangzhou.aliyuncs.com
  6. 腾讯云加速器: https://mirror.ccs.tencentyun.com
  7. 华为云加速器: https://repo.huaweicloud.com
  8. 七牛云加速器: https://reg-mirror.qiniu.com
  9. 网易云加速器: https://hub-mirror.c.163.com
  10. 中科大镜像: https://docker.mirrors.ustc.edu.cn

你可以在 Docker 的配置文件

/etc/docker/daemon.json

中添加这些镜像地址:

{"registry-mirrors":["https://docker.m.daocloud.io","https://dockerproxy.com","https://docker.mirrors.ustc.edu.cn","https://docker.nju.edu.cn","https://registry.cn-hangzhou.aliyuncs.com","https://mirror.ccs.tencentyun.com","https://repo.huaweicloud.com","https://reg-mirror.qiniu.com","https://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"]}

配置完成后,重启 Docker 服务即可生效。

  1. 重新启动 Docker 服务
sudo systemctl daemon-reload
sudo systemctl restart docker
进一步阅读
  • Docker 官方文档
  • CentOS 官方文档
标签: centos docker 容器

本文转载自: https://blog.csdn.net/Li_WenZhang/article/details/141051155
版权归原作者 li.wz 所有, 如有侵权,请联系我们删除。

“CentOS 上安装 Docker 的详细指南”的评论:

还没有评论