买了阿里云服务器后如何正确安装和配置Docker?

在阿里云 ECS 服务器上正确安装和配置 Docker,需结合阿里云环境特点(如 CentOS/Alibaba Cloud Linux/Ubuntu 系统、内核兼容性、防火墙、安全组等)进行规范操作。以下是详细、安全、生产就绪的完整指南(以主流系统 Alibaba Cloud Linux 3 / CentOS 7+/Ubuntu 22.04 为例):


✅ 一、前期准备与检查

  1. 确认系统信息

    # 查看系统版本(关键!不同系统安装方式略有差异)
    cat /etc/os-release
    uname -r  # 确保内核 ≥ 3.10(Docker 最低要求),阿里云新实例通常满足
  2. 更新系统并安装基础工具

    # Alibaba Cloud Linux 3 / CentOS 7+/8+
    sudo yum update -y
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2 curl wget vim
    
    # Ubuntu 22.04/20.04
    sudo apt update && sudo apt upgrade -y
    sudo apt install -y curl wget vim gnupg lsb-release
  3. 检查并关闭冲突服务(可选但推荐)

    # 确保未运行旧版 Docker(如通过 snap 或第三方包安装)
    sudo systemctl is-active docker 2>/dev/null || echo "Docker 未运行"
    sudo yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine 2>/dev/null
    # Ubuntu: sudo apt remove -y docker docker.io containerd runc

✅ 二、安装 Docker(推荐官方方式,非 yum install docker

⚠️ 阿里云镜像源提速 + 官方稳定版(避免 EPEL 的老旧版本)

▶ 方式 1:使用官方 Docker CE 仓库(最推荐

✅ Alibaba Cloud Linux 3 / CentOS 7+

# 1. 添加 Docker 官方 YUM 源(使用阿里云镜像提速下载)
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 替换 baseurl 为阿里云镜像(提升速度和稳定性)
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo

# 2. 安装最新稳定版 Docker CE
sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 3. 启动并设置开机自启
sudo systemctl enable docker
sudo systemctl start docker

# 4. 验证
sudo docker --version
sudo docker run hello-world  # 首次运行会拉取镜像,需网络通畅

✅ Ubuntu 22.04/20.04

# 1. 添加 GPG 密钥和阿里云镜像源
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 2. 安装
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 3. 启动
sudo systemctl enable docker
sudo systemctl start docker
sudo docker --version

▶ 方式 2:一键脚本(仅用于测试,不推荐生产)

# 官方脚本(自动适配系统,但走国际源较慢,建议配合X_X或改源)
curl -fsSL https://get.docker.com | sh
# ❗ 安装后务必执行:
sudo systemctl enable docker && sudo systemctl start docker

✅ 三、关键配置(生产环境必备)

1️⃣ 配置国内镜像提速器(大幅提升 pull 速度)

# 创建 daemon.json(阿里云容器镜像服务免费提供提速器)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://<your-registry-mirror>.mirror.aliyuncs.com",  # 🔑 替换为你自己的提速器地址!
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  },
  "live-restore": true,
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 65536,
      "Soft": 65536
    }
  }
}
EOF

📌 如何获取你的专属提速器?
登录 阿里云容器镜像服务控制台 → 左侧「镜像工具」→ 「镜像提速器」→ 复制 https://xxxxxx.mirror.aliyuncs.com

2️⃣ 重载 Docker 配置并重启

sudo systemctl daemon-reload
sudo systemctl restart docker
# 验证配置是否生效
sudo docker info | grep "Registry Mirrors" -A 3

3️⃣ 非 root 用户免 sudo 运行 Docker(安全增强)

# 创建 docker 组(若不存在)
sudo groupadd docker 2>/dev/null
# 将当前用户加入 docker 组
sudo usermod -aG docker $USER
# ⚠️ 重要:退出当前 SSH 会话,重新登录,或执行:
newgrp docker  # 临时生效(当前 shell)
# 验证
docker run hello-world  # 不再需要 sudo

4️⃣ 防火墙配置(阿里云 ECS 特别注意!)

  • 系统防火墙(firewalld/ufw):Docker 默认绕过 firewalld,但建议显式放行端口(如需暴露服务):
    # CentOS/Alibaba Cloud Linux(firewalld)
    sudo firewall-cmd --permanent --add-port=80/tcp
    sudo firewall-cmd --permanent --add-port=443/tcp
    sudo firewall-cmd --reload
  • 阿里云安全组(❗ 必做!):
    登录 ECS 控制台 → 找到实例 → 「安全组」→ 「配置规则」→ 添加入方向规则(如 80/TCP, 443/TCP, 22/TCP),不要开放 2375/2376(Docker Remote API)除非绝对必要且已加固!

5️⃣ 内核参数优化(可选,提升稳定性)

# 阿里云 ECS 推荐(添加至 /etc/sysctl.conf)
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
echo 'vm.swappiness = 0' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

✅ 四、验证与基础测试

# 1. 检查服务状态
sudo systemctl status docker

# 2. 查看基本信息
sudo docker info | grep -E "(Server Version|Storage Driver|Cgroup Driver|Registry Mirrors)"

# 3. 运行测试容器(带日志和端口映射)
sudo docker run -d -p 8080:80 --name nginx-test nginx:alpine
curl http://localhost:8080  # 应返回 Nginx 欢迎页

# 4. 清理测试
sudo docker stop nginx-test && sudo docker rm nginx-test

✅ 五、后续建议(生产就绪)

项目 建议
Docker Compose 已随 docker-compose-plugin 自动安装,用 docker compose up(非旧版 docker-compose
日志管理 配置 journald 或对接阿里云 SLS 日志服务
数据持久化 使用 docker volume 或挂载阿里云 NAS/EBS,禁止直接写容器内路径
安全加固 禁用 --privileged;使用 --read-only;定期 docker scan;启用 docker trust
监控 部署 cAdvisor + Prometheus,或使用阿里云 ARMS 容器监控
备份 定期 docker commit 镜像 + docker save,或使用阿里云镜像仓库自动构建

❌ 常见错误排查

现象 解决方案
Cannot connect to the Docker daemon 检查 sudo systemctl status docker,确认服务运行;用户是否加入 docker 组并重登
Permission denied while trying to connect to ... sudo chmod 666 /var/run/docker.sock ❌(不安全!)→ 正确做法是加组 + 重登
Error response from daemon: Get "https://registry-1.docker.io/v2/...": net/http: request canceled while waiting for connection 镜像提速器未生效 → 检查 /etc/docker/daemon.json 格式 & 重启 docker
Failed to start docker.service: Unit not found 确认安装的是 docker-ce(非 docker 包),检查 repo 是否启用:yum repolist | grep docker

总结一句话

用阿里云镜像源安装官方 Docker CE → 配置专属提速器 → 加入 docker 用户组 → 开放安全组 → 定期更新,即可获得稳定、高速、安全的 Docker 运行环境。

如需我帮你:

  • 自动生成适配你 ECS 系统版本的安装脚本
  • 配置 Nginx + SSL 反向X_X
  • 部署 Portainer 可视化管理
  • 集成阿里云镜像仓库(ACR)自动构建
    欢迎随时告诉我你的具体系统版本和需求 👇

祝你上云顺利,容器飞驰!🚀

未经允许不得转载:CCLOUD博客 » 买了阿里云服务器后如何正确安装和配置Docker?