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
相关推荐
idolao2 小时前
傲梅分区助手 使用教程:免安装硬盘分区管理工具
linux·运维·服务器
云边云科技_云网融合2 小时前
网关接入异常监测预警:从固定阈值到 AI 动态感知的技术革新
运维·服务器·网络·人工智能
焦糖玛奇朵婷2 小时前
盲盒小程序开发,盲盒小程序怎么做
java·大数据·服务器·前端·小程序
ayt0073 小时前
Netty NioEventLoopGroup源码深度剖析:高性能网络编程的核心引擎
服务器·前端·数据库
草莓熊Lotso3 小时前
【Linux系统加餐】 mmap 文件映射全解:从底层原理、API 到实战开发(含 malloc 模拟实现)
android·linux·运维·服务器·c语言·c++
那山川3 小时前
canbus操作记录
linux·服务器·网络
wanhengidc3 小时前
云手机的工作原理
运维·服务器·网络·网络协议·智能手机
SPC的存折3 小时前
3、MySQL数据库主从复制
linux·运维·服务器·数据库·mysql
一只小bit3 小时前
Docker 实用命令手册:包含Docker简介与常用命令
运维·docker·容器