在CentOS上安装Docker需要注意的事项

文章目录

 写在前面:大家好!我是晴空๓。如果博客中有不足或者的错误的地方欢迎在评论区或者私信我指正,感谢大家的不吝赐教。我的唯一博客更新地址是:https://ac-fun.blog.csdn.net/。非常感谢大家的支持。一起加油,冲鸭!

用知识改变命运,用知识成就未来!加油 (ง •̀o•́)ง (ง •̀o•́)ง

前言

 今天在 CentOS 7.9 上安装 Docker 时遇到网络连接超时问题,一个是未设置仓库导致从仓库中下载Docker引擎 的时候网络连接超时;另一个是因为没有配置镜像加速器导致镜像拉取失败。以下是相关问题的解决方案:

Docker Engine

 官网对于容器的解释原文是:

Docker Engine is an open source containerization technology for building and containerizing your applications. Docker Engine acts as a client-server application with:

  • A server with a long-running daemon process dockerd.
  • APIs which specify interfaces that programs can use to talk to and instruct the Docker daemon.
  • A command line interface (CLI) client docker.

Docker Engine 是一种开源容器化技术,用于构建和容器化应用程序。使用 Docker 我们可以将软件打包成标准化单元,以用于开发、交付和部署。

如何设置仓库

 Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.
Set up the repository

复制代码
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

 在安装Docker 时有一个步骤是设置Docker 存储库,这里需要注意最好不要用官方提供的 Docker CE 仓库地址(https://download.docker.com/linux/centos/docker-ce.repo),而是使用国内的仓库地址(例如阿里云的仓库地址)。

shell 复制代码
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

 执行完以上命令之后即可快速的通过阿里云仓库下载Docker了。官网给出的命令用的是 dnfdnfyum 的改进版,从 CentOS 8 开始成为默认包管理工具。但在旧系统(如 CentOS 7 )中仍需使用 yum 作为包管理工具。我用的CentOS 版本是 7.9 所以仍使用 yum ,使用 yum install -y yum-utils 命令安装辅助工具,帮助管理软件包和仓库(yum-config-manager用于管理仓库)。

设置镜像加速器

 在下载安装完Docker CE 并启动(具体操作查看Docker官网 https://docs.docker.com/engine/install/centos/)之后就可以通过运行hello-world镜像来测试是否安装成功,但是我在测试的时候出现了拉取镜像超时的问题。具体报错信息如下:

shell 复制代码
[root@qingkong ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

Docker 无法从 Docker Hub 拉取 hello-world 镜像,原因是因为网络连接超时了。由于 Docker Hub 在国内访问可能较慢或不稳定,可以通过配置镜像加速器访问国内的镜像仓库来解决。

获取镜像加速器地址

 我们可以登录 阿里云容器镜像服务控制台 获取专属镜像加速器地址(例如 https://xxx.mirror.aliyuncs.com)。

获取到加速器地址之后按照提示进行配置即可。

shell 复制代码
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

 设置并重启Docker 之后重新执行docker run hello-world 成功拉取了hello-world 镜像并运行

相关推荐
Yana.nice3 小时前
Linux 只保留 30 天内日志(find命令删除日志文件)
linux·运维·chrome
DFT计算杂谈7 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
Zhang~Ling8 小时前
从 fopen 到 struct file:从零开始拆解 Linux 文件 I/O
linux·运维·服务器
DeeplyMind8 小时前
Linux 深入 per-VMA lock:Linux 缺页路径如何摆脱 mmap_lock
linux·per-vma lock
爱写代码的森8 小时前
蒙三方库 | harmony-utils之FileUtil文件重命名与属性查询详解
linux·运维·服务器·华为·harmonyos·鸿蒙·huawei
XMAIPC_Robot9 小时前
软硬协同实时控制|RK3588业务调度+FPGA硬件时序,ethercat实现半导体设备微秒级响应(125us)
linux·arm开发·人工智能·fpga开发
重生的黑客10 小时前
Linux 进程优先级、切换与调度:从孤儿进程到 O(1) 调度模型
linux·运维·服务器·进程优先级·nice
风曦Kisaki11 小时前
#企业级docker私有仓库构建:harbor仓库与阿里云镜像仓库
阿里云·docker·容器
骑上单车去旅行11 小时前
MD5校验对比脚本
linux·服务器·windows
平生幻11 小时前
Linux 常用命令
linux