CentOS 7 配置国内清华源并安装 Docker-CE 以及配置 Docker 加速

在 CentOS 7 上配置国内清华源并安装 Docker-CE 及其加速器配置,可以显著提高软件包下载速度和 Docker 镜像拉取速度。以下是详细步骤。

步骤一:配置国内清华源

首先,备份现有的YUM源配置文件:

sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

然后,替换为清华源:

sudo tee /etc/yum.repos.d/CentOS-Base.repo <<-'EOF'
[base]
name=CentOS-$releasever - Base - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7
EOF

更新YUM缓存:

sudo yum clean all
sudo yum makecache

步骤二:安装 Docker-CE

添加 Docker 仓库:

sudo tee /etc/yum.repos.d/docker-ce.repo <<-'EOF'
[docker-ce-stable]
name=Docker CE Stable - mirrors.tuna.tsinghua.edu.cn
baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/gpg
EOF

安装 Docker-CE:

sudo yum install -y docker-ce

启动并设置 Docker 开机自启动:

sudo systemctl start docker
sudo systemctl enable docker

验证 Docker 是否安装成功:

sudo docker run hello-world

步骤三:配置 Docker 加速器

为了加速 Docker 镜像的拉取,可以配置 Docker 的镜像加速器。以阿里云的加速器为例:

创建或编辑 /etc/docker/daemon.json 文件:

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://<your-mirror>.mirror.aliyuncs.com"]
}
EOF

请将 <your-mirror> 替换为你从阿里云控制台获取的专属加速器地址。

重新启动 Docker 服务使配置生效:

sudo systemctl daemon-reload
sudo systemctl restart docker

分析说明表

步骤 说明 示例命令或配置
备份现有YUM源配置 备份原始的YUM源配置文件 sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
配置清华源 替换YUM源为清华源 sudo tee /etc/yum.repos.d/CentOS-Base.repo
更新YUM缓存 清理并重新生成YUM缓存 sudo yum clean all && sudo yum makecache
添加Docker仓库 添加Docker CE的YUM仓库 sudo tee /etc/yum.repos.d/docker-ce.repo
安装Docker-CE 安装Docker CE sudo yum install -y docker-ce
启动并设置Docker开机自启动 启动Docker服务并设置开机自启动 sudo systemctl start docker && sudo systemctl enable docker
验证Docker安装 运行测试镜像验证Docker安装是否成功 sudo docker run hello-world
配置Docker加速器 配置Docker镜像加速器以加速镜像拉取 sudo tee /etc/docker/daemon.json
重新启动Docker服务使配置生效 重新加载并重启Docker服务 sudo systemctl daemon-reload && sudo systemctl restart docker

结论

通过配置国内清华源,可以显著提高软件包的下载速度;安装 Docker-CE 并配置镜像加速器,可以提升 Docker 镜像的拉取速度。这一系列配置不仅提高了系统和应用的安装效率,还优化了服务器的运行环境。如果你需要更多关于服务器管理和优化的内容,请参考相关资源