CentOS 7/8/Stream 安装 Docker 通用详细步骤 ~
以下是 CentOS 7 / 8 / Stream 通用的 Docker 安装步骤,包含每一步的详细注释说明和排版优化,适用于国内环境,一条龙服务
🚀 Docker 安装步骤(CentOS 7/8/Stream 通用)
🔧 第 0 步:更换 YUM 源为阿里云镜像,加速安装速度
# 备份原有的 CentOS YUM 源配置文件
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 下载阿里云的 CentOS 7 镜像源配置文件(CentOS 8 使用 Centos-8.repo)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 清理旧的缓存
yum clean all
# 重新生成缓存
yum makecache
🔧 第 1 步:安装 Docker 所需的依赖工具包
# 安装设备管理工具和 LVM 支持组件
yum install -y yum-utils device-mapper-persistent-data lvm2
🌐 第 2 步:添加 Docker 官方仓库(推荐使用阿里云镜像仓库)
# 添加 Docker 的阿里云镜像仓库配置
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
📦 第 3 步:安装 Docker 引擎和相关插件
# 快速更新缓存,加速安装
yum makecache fast
# 安装 Docker 主程序及其命令行工具、容器运行时和插件
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
▶️ 第 4 步:启动 Docker 并设置为开机自启动
# 启动 Docker 服务
systemctl start docker
# 设置 Docker 服务在系统启动时自动运行
systemctl enable docker
🧪 第 5 步:验证 Docker 安装是否成功
# 查看 Docker 版本信息,验证是否正确安装
docker version
如果出现如下错误:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
说明 Docker 服务未启动,执行以下命令重新启动并设为开机自启:
systemctl start docker
systemctl enable docker
🚀 第 6 步:配置 Docker 镜像加速器(使用多个国内加速源)
# 创建 Docker 配置目录
sudo mkdir -p /etc/docker
# 写入加速镜像配置文件
sudo tee /etc/docker/daemon.json <<EOF
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.mybacc.com",
"https://dytt.online",
"https://lispy.org",
"https://docker.xiaogenban1993.com",
"https://docker.yomansunter.com",
"https://aicarbon.xyz",
"https://666860.xyz",
"https://docker.zhai.cm",
"https://a.ussh.net",
"https://hub.littlediary.cn",
"https://hub.rat.dev",
"https://docker.m.daocloud.io"
]
}
EOF
# 重新加载 Docker 配置
sudo systemctl daemon-reload
# 重启 Docker 服务以应用新配置
sudo systemctl restart docker
🐳 第 7 步:测试 Docker 是否能正常拉取镜像运行容器
# 拉取并运行官方的 Hello World 镜像进行测试
docker run hello-world
如果能成功输出 Hello from Docker! 说明安装无误。
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:0b6a027b5cf322f09f6706c754e086a232ec1ddba835c8a15c6cb74ef0d43c29
Status: Downloaded newer image for hello-world:latest
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/
[root@localhost ~]#
文章作者: 无念log
文章链接: https://silys.nianlink.top/index.php/archives/121/
版权声明: 本网站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 伤极无念log-科技、爱好、工具!
文章链接: https://silys.nianlink.top/index.php/archives/121/
版权声明: 本网站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 伤极无念log-科技、爱好、工具!
评论