惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

云野开源志

OpenClaw npm 升级踩坑记:从版本冲突到模块丢失 免责声明 openclaw(moltbot、Clawdbot)常见异常 大模型配置-千问免费版 初始化OpenClaw 安装openclaw记录 大年初一桃花谷 CheckSSL.sh - SSL证书到期时间监控脚本 g.sh - Go语言环境管理工具安装脚本 install-cri-docker.sh - cri-dockerd安装脚本 install-docker2.sh - Docker 交互式安装脚本 install-nginx.sh - Nginx官方源快速安装脚本 install-zerotier.sh - ZeroTier 虚拟网络快速安装脚本 mng.sh - Nginx配置文件合并脚本 OpenSSL.sh - 自签名 SSL 证书生成脚本 SystemInfoMonitor.sh - 系统资源监控告警脚本 UpdateImages.sh - Docker 镜像批量更新脚本 为Hugo Next主题添加Umami统计支持 解忧杂货铺 - 栏目声明 Certd - 解决多平台SSL证书管理难题的神器 轻量级网络端口监控工具 GeoLite2 自动搬运仓库使用手册 MaxMind 免费账号注册 CloudFlare Accel Docker 搭建私有 DNS 服务器 hugo-theme-stack集成Umami openresty编译添加geoip2模块支持 GPU加速云原生应用,开启高性能计算新纪元 5. K8S Service与Ingress 4. K8S 控制器 Rancher搭建 3. K8S Pod 资源管理 2. K8S 基础资源管理 1. K8S 简介和认证介绍 Rancher集成AzureAD认证 Harbor 部署搭建 站点留言板 关于 友情链接
install-docker.sh - Docker Engine快速安装脚本
云野开源志 · 2026-01-26 · via 云野开源志

总结摘要

install-docker.sh 是一个用于快速安装 Docker Engine的Shell脚本,支持主流 Linux发行版,自动配置镜像加速器,简化 Docker安装和部署流程。

脚本功能

install-docker.sh 是一个一键式 Docker Engine 安装脚本,支持 Debian、Ubuntu、RHEL、CentOS、Alibaba Cloud Linux、Anolis OS等主流Linux发行版。脚本自动添加官方软件源、安装最新稳定版 Docker Engine及Docker Compose插件,并配置国内镜像加速器,极大简化 Docker的安装和部署流程。

核心特性

  • 一键安装,无需手动配置
  • 智能识别操作系统类型
  • 自动添加官方或镜像软件源
  • 集成 Docker Compose插件
  • 预配置多个国内镜像加速器
  • 自动启动并设置开机自启
  • 支持多种 Linux发行版

脚本依赖

系统工具依赖

根据操作系统不同,需要以下基础工具:

Debian/Ubuntu系列

  • apt-get:包管理工具
  • ca-certificates:CA证书包
  • curl:下载工具
  • gnupg:GPG签名验证

RHEL/CentOS/Alibaba Cloud Linux系列

  • yumdnf:包管理工具
  • yum-utils:YUM工具集

外部服务依赖

  • 互联网连接:用于下载 Docker安装包和配置软件源
  • Docker 官方软件源:通过阿里云镜像加速访问

环境变量依赖

本脚本无需预设环境变量,所有配置已内置在脚本中。

内置配置说明

脚本内部定义了以下配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Docker 软件源配置(使用阿里云镜像)
# Ubuntu/Debian:
deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable

# CentOS/RHEL:
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# Docker 镜像加速器(daemon.json 配置)
"registry-mirrors": [
    "https://nd1.eli1.top",
    "https://nd2.eli1.top",
    "https://docker.fnnas.com",
    "https://docker.m.daocloud.io",
    "https://d2.eli1.top",
    "https://do3.eli1.top"
]

参数用法

本脚本无命令行参数,直接执行完整安装流程:

  • 无参数模式:自动完成所有安装步骤
  • 自定义配置:需编辑脚本修改镜像地址或其他参数

使用方法

快速安装与使用

  1. 下载脚本

    1
    2
    3
    4
    5
    
    # 使用主下载地址
    bash <(curl -sL sc.eli1.top) install-docker download
    
    # 或使用备用地址
    bash <(curl -sL download.elisky.cn) install-docker download
  2. 赋予执行权限

    1
    
    chmod +x install-docker.sh
  3. 执行安装

    1
    2
    3
    4
    5
    
    # 使用 root用户运行(推荐)
    sudo ./install-docker.sh
    
    # 或直接以 root身份运行
    sudo bash install-docker.sh
  4. 验证安装

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    # 查看 Docker版本
    docker --version
    docker compose version
    
    # 查看 Docker 服务状态
    systemctl status docker
    
    # 查看镜像加速配置
    docker info | grep -A 10 "Registry Mirrors"
    
    # 运行测试容器
    docker run hello-world

典型使用场景示例

场景 1:全新服务器安装 Docker

1
2
3
4
5
6
7
# 在新购买的云服务器上快速安装 Docker
sudo curl -fsSL https://raw.githubusercontent.com/.../install-docker.sh | sudo bash

# 或先下载再执行
wget https://.../install-docker.sh
chmod +x install-docker.sh
sudo ./install-docker.sh

场景 2:批量部署 Docker环境

创建批量部署脚本:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
# deploy_docker.sh

# 服务器列表
SERVERS=("192.168.1.10" "192.168.1.11" "192.168.1.12")
SSH_KEY="~/.ssh/id_rsa"

for server in "${SERVERS[@]}"; do
    echo "=== Deploying to $server ==="
    
    # 复制脚本到远程服务器
    scp -i $SSH_KEY install-docker.sh root@$server:/tmp/
    
    # 远程执行安装
    ssh -i $SSH_KEY root@$server "bash /tmp/install-docker.sh"
    
    # 验证安装
    ssh -i $SSH_KEY root@$server "docker --version"
done

场景 3:指定 Docker版本安装

脚本默认安装最新稳定版,如需指定版本,编辑脚本:

1
2
3
4
5
# 修改 apt-install或 yum-install函数
# 添加版本号:
sudo apt-get install docker-ce=5:20.10.24~3-0~ubuntu-focal \
                     docker-ce-cli=5:20.10.24~3-0~ubuntu-focal \
                     containerd.io docker-buildx-plugin docker-compose-plugin -y

场景 4:自定义镜像加速器

编辑脚本中的add-daemonfile()函数(约第 44-56 行):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
add-daemonfile() {
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": [
        "https://your-custom-mirror.com",
        "https://another-mirror.com"
    ]
}
EOF
}

场景 5:离线环境安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 1. 在有网络的机器上下载 Docker离线包
# Ubuntu/Debian:
apt-get download $(apt-cache depends docker-ce | grep Depends | sed "s/.*//;\s")

# CentOS/RHEL:
yumdownloader --resolve docker-ce docker-ce-cli containerd.io docker-compose-plugin

# 2. 传输到离线服务器
scp docker-packages.tar.gz user@offline-server:/tmp/

# 3. 离线安装
cd /tmp
tar xf docker-packages.tar.gz
sudo dpkg -i *.deb  # Debian/Ubuntu
# 或
sudo yum install *.rpm  # RHEL/CentOS

场景 6:配置开机自启和防火墙

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 脚本已自动配置开机自启,额外配置防火墙:

# Ubuntu/Debian (ufw)
sudo ufw allow 2375/tcp  # Docker API(不推荐开放)
sudo ufw allow 2376/tcp  # Docker TLS API

# CentOS/RHEL (firewalld)
sudo firewall-cmd --permanent --add-port=2375/tcp
sudo firewall-cmd --reload

# 验证服务状态
systemctl is-enabled docker
systemctl is-active docker

安装流程详解

脚本执行以下步骤:

  1. 检查包管理工具(第 2-4 行):

    1
    2
    3
    
    command_exists() {
        command -v "$@" > /dev/null 2>&1
    }
  2. Debian/Ubuntu系列安装

    • 添加 GPG密钥(第 13-17 行):

      1
      2
      3
      
      sudo install -m 0755 -d /etc/apt/keyrings
      curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
        sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    • 添加软件源(第 18-20 行):

      1
      2
      
      echo "deb [arch=... signed-by=...] https://mirrors.aliyun.com/docker-ce/..." | \
        sudo tee /etc/apt/sources.list.d/docker.list
    • 安装 Docker(第 23-24 行):

      1
      2
      3
      
      sudo apt-get update
      sudo apt-get install docker-ce docker-ce-cli containerd.io \
        docker-buildx-plugin docker-compose-plugin -y
  3. RHEL/CentOS系列安装

    • 添加 YUM 源(第 26-28 行):

      1
      2
      
      sudo yum install -y yum-utils
      yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/...
    • 安装 Docker(第 30-31 行):

      1
      2
      
      yum install docker-ce docker-ce-cli containerd.io \
        docker-buildx-plugin docker-compose-plugin -y
  4. 配置 daemon.json(第 44-56 行):

    1
    2
    3
    4
    5
    6
    
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json <<EOF
    {
        "registry-mirrors": [...]
    }
    EOF
  5. 启用并启动服务(第 58-60 行):

    1
    
    systemctl enable --now docker
  6. 验证安装(第 61-63 行):

实际应用场景

  • 新服务器初始化:快速在新购云服务器上部署 Docker环境
  • 开发环境搭建:为开发人员快速创建 Docker开发环境
  • CI/CD流水线:在 CI服务器上安装 Docker用于构建镜像
  • 生产环境部署:标准化生产环境的 Docker安装流程
  • 培训演示环境:快速搭建 Docker培训或演示环境
  • 多节点集群:批量在 Kubernetes节点上安装 Docker
  • 边缘计算节点:在边缘设备上部署轻量级 Docker环境

注意事项

操作系统兼容性

  • 支持的发行版

    • Debian系列:Debian 10/11/12, Ubuntu 18.04/20.04/22.04
    • RHEL 系列:RHEL 7/8/9, CentOS 7/8/9, AlmaLinux, Rocky Linux
    • 国产系统:Alibaba Cloud Linux 2/3, Anolis OS
  • 版本要求

    • Ubuntu:建议 18.04 LTS及以上
    • CentOS:建议 7.0及以上(CentOS 8已停止维护)
    • Debian:建议 10 (Buster)及以上

网络要求

  • 软件源访问:需要能访问https://mirrors.aliyun.com/
  • 镜像加速器:脚本配置的加速器在国内访问速度更快
  • 代理配置:如使用 HTTP代理,需额外配置:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    # 创建 systemd 覆盖配置
    mkdir -p /etc/systemd/system/docker.service.d
    cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
    [Service]
    Environment="HTTP_PROXY=http://proxy.example.com:8080"
    Environment="HTTPS_PROXY=https://proxy.example.com:8080"
    EOF
    
    systemctl daemon-reload
    systemctl restart docker

存储配置

  • 默认存储驱动:overlay2(推荐)

  • 数据目录/var/lib/docker

  • 磁盘空间:建议至少预留 20GB可用空间

  • 自定义数据目录

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    # 编辑 daemon.json
    cat > /etc/docker/daemon.json <<EOF
    {
        "data-root": "/mnt/docker-data"
    }
    EOF
    
    # 迁移现有数据
    systemctl stop docker
    rsync -av /var/lib/docker/ /mnt/docker-data/
    systemctl start docker

安全配置

  • Root权限:Docker守护进程需要root权限运行

  • Socket权限:将用户加入 docker组可免sudo使用:

    1
    2
    
    sudo usermod -aG docker $USER
    newgrp docker
  • TLS加密:生产环境建议启用 TLS:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    # 生成 CA和服务器证书
    # 配置 daemon.json 启用 TLS
    cat > /etc/docker/daemon.json <<EOF
    {
        "tls": true,
        "tlsverify": true,
        "tlscacert": "/etc/docker/certs/ca.pem",
        "tlscert": "/etc/docker/certs/server-cert.pem",
        "tlskey": "/etc/docker/certs/server-key.pem"
    }
    EOF
  • 防火墙配置:不要随意开放 2375端口(未加密的 Docker API)

性能优化

  • 日志驱动:建议使用 json-file并限制大小:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    cat > /etc/docker/daemon.json <<EOF
    {
        "log-driver": "json-file",
        "log-opts": {
            "max-size": "100m",
            "max-file": "3"
        }
    }
    EOF
  • 存储优化:定期清理悬空镜像和停止的容器:

    1
    2
    3
    4
    
    # 添加定时任务
    crontab -e
    # 每周日凌晨清理
    0 3 * * 0 docker system prune -f

常见问题排查

问题 1:软件源添加失败

1
2
3
4
5
6
# 检查网络连接
ping mirrors.aliyun.com

# 手动添加 GPG密钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
  sudo apt-key add -

问题 2:Docker服务无法启动

1
2
3
4
5
6
7
# 查看详细错误
journalctl -u docker.service -n 50 --no-pager

# 常见原因:
# - 配置文件错误:检查/etc/docker/daemon.json语法
# - 端口冲突:确保 2375端口未被占用
# - 存储驱动问题:尝试更换存储驱动

问题 3:镜像拉取速度慢

1
2
3
4
5
6
7
8
# 检查镜像加速器配置
docker info | grep -A 10 "Registry Mirrors"

# 手动测试镜像速度
curl -I https://nd1.eli1.top/v2/

# 更换其他加速器
# 编辑/etc/docker/daemon.json

问题 4:权限拒绝错误

1
2
3
4
5
6
7
8
# 普通用户使用 Docker
sudo usermod -aG docker $USER

# 重新登录或执行
newgrp docker

# 验证
groups $USER

最佳实践

  1. 版本固定:生产环境建议固定 Docker版本,避免自动升级
  2. 定期更新:每月检查安全更新并及时应用
  3. 监控告警:监控 Docker服务状态和容器健康
  4. 日志管理:配置日志轮转防止磁盘占满
  5. 备份配置:定期备份 daemon.json和证书文件
  6. 资源限制:为容器配置 CPU和内存限制
  7. 网络规划:合理规划容器网段,避免冲突
  8. 存储分离:将/var/lib/docker挂载到独立分区

升级策略

  • 小版本升级:通常无兼容性问题,可直接升级
  • 大版本升级:先在测试环境验证
  • 降级方法
    1
    2
    3
    4
    5
    
    # 卸载当前版本
    apt-get remove docker-ce docker-ce-cli containerd.io
    
    # 安装指定版本
    apt-get install docker-ce=5:20.10.24~3-0~ubuntu-focal ...

相关资源

  • Docker 官方文档:https://docs.docker.com/
  • Docker Hub:https://hub.docker.com/
  • 阿里云镜像加速器:https://cr.console.aliyun.com/