摘要: 本文面向旧版 CentOS / Alibaba Cloud Linux 用户,提供从零安装 Docker 及 Compose 插件的完整步骤。涵盖官方脚本快速安装、国内镜像源替代方案、CentOS 7 与 Alibaba Cloud Linux 两种环境的详细命令,以及镜像加速器配置与安装验证方法,帮助读者一次性解决国内环境下的 Docker 安装与镜像拉取问题。
整体安装流程: 先尝试官方脚本,报错则根据系统版本选择替代方案,最后配置镜像加速并验证。
#mermaid-svg-vCs8Fa4ixFp37ofP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vCs8Fa4ixFp37ofP .error-icon{fill:#552222;}#mermaid-svg-vCs8Fa4ixFp37ofP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vCs8Fa4ixFp37ofP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vCs8Fa4ixFp37ofP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vCs8Fa4ixFp37ofP .marker.cross{stroke:#333333;}#mermaid-svg-vCs8Fa4ixFp37ofP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vCs8Fa4ixFp37ofP p{margin:0;}#mermaid-svg-vCs8Fa4ixFp37ofP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-vCs8Fa4ixFp37ofP .cluster-label text{fill:#333;}#mermaid-svg-vCs8Fa4ixFp37ofP .cluster-label span{color:#333;}#mermaid-svg-vCs8Fa4ixFp37ofP .cluster-label span p{background-color:transparent;}#mermaid-svg-vCs8Fa4ixFp37ofP .label text,#mermaid-svg-vCs8Fa4ixFp37ofP span{fill:#333;color:#333;}#mermaid-svg-vCs8Fa4ixFp37ofP .node rect,#mermaid-svg-vCs8Fa4ixFp37ofP .node circle,#mermaid-svg-vCs8Fa4ixFp37ofP .node ellipse,#mermaid-svg-vCs8Fa4ixFp37ofP .node polygon,#mermaid-svg-vCs8Fa4ixFp37ofP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vCs8Fa4ixFp37ofP .rough-node .label text,#mermaid-svg-vCs8Fa4ixFp37ofP .node .label text,#mermaid-svg-vCs8Fa4ixFp37ofP .image-shape .label,#mermaid-svg-vCs8Fa4ixFp37ofP .icon-shape .label{text-anchor:middle;}#mermaid-svg-vCs8Fa4ixFp37ofP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-vCs8Fa4ixFp37ofP .rough-node .label,#mermaid-svg-vCs8Fa4ixFp37ofP .node .label,#mermaid-svg-vCs8Fa4ixFp37ofP .image-shape .label,#mermaid-svg-vCs8Fa4ixFp37ofP .icon-shape .label{text-align:center;}#mermaid-svg-vCs8Fa4ixFp37ofP .node.clickable{cursor:pointer;}#mermaid-svg-vCs8Fa4ixFp37ofP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-vCs8Fa4ixFp37ofP .arrowheadPath{fill:#333333;}#mermaid-svg-vCs8Fa4ixFp37ofP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vCs8Fa4ixFp37ofP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vCs8Fa4ixFp37ofP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vCs8Fa4ixFp37ofP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-vCs8Fa4ixFp37ofP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vCs8Fa4ixFp37ofP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-vCs8Fa4ixFp37ofP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vCs8Fa4ixFp37ofP .cluster text{fill:#333;}#mermaid-svg-vCs8Fa4ixFp37ofP .cluster span{color:#333;}#mermaid-svg-vCs8Fa4ixFp37ofP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vCs8Fa4ixFp37ofP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-vCs8Fa4ixFp37ofP rect.text{fill:none;stroke-width:0;}#mermaid-svg-vCs8Fa4ixFp37ofP .icon-shape,#mermaid-svg-vCs8Fa4ixFp37ofP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vCs8Fa4ixFp37ofP .icon-shape p,#mermaid-svg-vCs8Fa4ixFp37ofP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-vCs8Fa4ixFp37ofP .icon-shape .label rect,#mermaid-svg-vCs8Fa4ixFp37ofP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vCs8Fa4ixFp37ofP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-vCs8Fa4ixFp37ofP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-vCs8Fa4ixFp37ofP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 成功
报错 connection reset
CentOS 7 / RHEL 7
Alibaba Cloud Linux 2/3
开始
执行官方脚本 curl -fsSL https://get.docker.com | sh
是否成功?
配置镜像加速
cat /etc/os-release 确认系统版本
系统类型
方案 A:yum 安装 Docker CE
方案 B:yum install docker
docker run hello-world 验证
安装完成
1. 安装 Docker(Linux 示例)
bash
# CentOS / Aliyun Linux 等可参考官方文档,简版:
curl -fsSL https://get.docker.com | sh
sudo systemctl enable docker
sudo systemctl start docker
# Compose 插件(Docker 新版本一般自带)
docker compose version
curl -fsSL https://get.docker.com | sh这条命令会做什么:
- 自动识别系统(CentOS、Ubuntu、Debian、阿里云 Linux 等)
- 安装 Docker Engine、CLI,新版一般自带 Compose 插件(
docker compose)- 需要
root或前面加sudo
如果执行报错如下信息,别急,我们走 B 方案:
bash
-bash-4.2# curl -fsSL https://get.docker.com | sh
curl: (35) TCP connection reset by peer
-bash-4.2#
这是国内服务器访问 get.docker.com 被墙或不稳定时的常见现象(connection reset by peer),不是命令写错了。换国内镜像源安装即可。
2. 先确认系统版本
在服务器上执行:
bash
cat /etc/os-release
下面按常见两种情况写;阿里云 ECS 多数是 CentOS 7 或 Alibaba Cloud Linux。
方案 A:CentOS 7 / RHEL 7(bash-4.2 很常见)
bash
# 1. 卸载旧版(若有,没有会提示找不到,可忽略)
yum remove -y docker docker-client docker-client-latest docker-common \
docker-latest docker-latest-logrotate docker-logrotate docker-engine 2>/dev/null
# 2. 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 3. 阿里云 Docker 源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 4. 安装(CentOS 7 建议固定较新但兼容的版本)
yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 5. 启动
systemctl enable docker
systemctl start docker
# 6. 验证
docker --version
docker compose version
若第 4 步报 containerd.io 版本冲突,可试:
bash
yum install -y docker-ce docker-ce-cli docker-compose-plugin
方案 B:Alibaba Cloud Linux 2 / 3
bash
yum install -y docker
systemctl enable docker
systemctl start docker
docker --version
部分镜像自带 docker,但没有 Compose 插件时再装:
bash
yum install -y docker-compose-plugin
# 或
curl -L https://github.com/docker/compose/releases/download/v2.24.0/docker-compose-linux-x86_64 \
-o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose version
3. 配置镜像加速(国内拉镜像更快)
bash
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://mirror.ccs.tencentyun.com"
]
}
EOF
systemctl daemon-reload
systemctl restart docker
阿里云专属加速(推荐,更稳定):
登录阿里云控制台 → 容器镜像服务 ACR → 镜像工具 → 镜像加速器,复制你的专属地址,类似
https://xxxxxx.mirror.aliyuncs.com。直接看下方的操作文档也可以。
4. 验证
bash
docker run hello-world
能输出 Hello from Docker! 就说明装好了。
常见问题:拉取镜像超时
如果没有配置镜像加速,会出现:
bash
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 Hub 的镜像(国内很常见),需要配置镜像加速。