Server - 服务器 CentOS 安装与配置 Docker

欢迎关注我的CSDN:https://spike.blog.csdn.net/

本文地址:https://spike.blog.csdn.net/article/details/159988565

免责声明:本文来源于个人知识与公开资料,仅用于学术交流,欢迎讨论,不支持转载。


在 CentOS 上安装与配置 Docker 的核心流程主要包括环境修复、组件安装和网络优化。由于 CentOS 7 已停更,首先需将系统基础 yum 源替换为阿里云历史存档源,以解决依赖下载报错问题。接着,添加 Docker 的国内仓库并安装最新版核心组件,配置服务开机自启。最后,为避免拉取官方镜像超时,必须在配置文件中添加国内镜像加速器并重启服务,即可完成稳定 Docker 环境的搭建。

1. 修改 Docker 配置

安装依赖:

bash 复制代码
# 1. 备份原有的 yum 源配置文件(以防万一)
sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
sudo mkdir -p /etc/yum.repos.d/bak
sudo mv /etc/yum.repos.d/CentOS-*.repo /etc/yum.repos.d/bak/

# 2. 下载阿里云的 CentOS 7 基础源
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# 3. 将源地址替换为 Vault 存档源(因为普通镜像节点也已清理了 CentOS 7 的数据)
sudo sed -i 's/mirrors.aliyun.com\/centos/mirrors.aliyun.com\/centos-vault/g' /etc/yum.repos.d/CentOS-Base.repo
sudo sed -i 's/$releasever/7.9.2009/g' /etc/yum.repos.d/CentOS-Base.repo

清空缓存:

bash 复制代码
sudo yum clean all
sudo yum makecache

2. 安装 Docker

执行安装 Docker 命令:

bash 复制代码
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

配置 Docker:

bash 复制代码
sudo systemctl start docker
sudo systemctl enable docker

3. 配置 Docker 源

添加镜像加速器:

bash 复制代码
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://docker.1panel.live",
    "https://hub.rat.dev",
    "https://dockerpull.com",
    "https://docker.anyhub.us.kg"
  ]
}
EOF

重启配置:

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl restart docker

验证是否安装成功:

bash 复制代码
sudo docker run hello-world

输出:

bash 复制代码
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/

Docker 版本:

bash 复制代码
# docker --version
Docker version 26.1.4, build 5650f9b
相关推荐
gc_22991 小时前
学习在Windows中基于Docker部署Dify的步骤
windows·docker·dify
写代码的学渣1 小时前
docker部署开源实时观测系统hertzbeat
docker·容器·开源
ai产品老杨1 小时前
架构师视点:基于 Docker 与边缘计算的百路异构视频中台,如何实现 GB28181/RTSP 统一接入与源码交付?
docker·音视频·边缘计算
TimberWill1 小时前
Docker使用总结
运维·docker·容器
闪电悠米1 小时前
黑马点评-Redis ZSet-实现关注 Feed 流
服务器·网络·数据库·redis·缓存·junit·lua
m0_547486661 小时前
《KVM Docker OpenStack实战—虚拟化与云计算配置 管理与运维》全套课件PPT
运维·docker·openstack
yyuuuzz1 小时前
2026游戏云服务器推荐的技术判断思路
运维·服务器·开发语言·网络·人工智能·游戏·php
江华森2 小时前
Linux 运维新手入门课
linux·运维·服务器
正经教主2 小时前
【docker基础】第十周:CI/CD集成
运维·docker·容器
Clang's Blog2 小时前
Ubuntu(20.04/22.04/24.04)国内环境一键安装 Docker、JDK17 和 Maven
ubuntu·docker·maven