Last active
October 28, 2025 06:53
-
-
Save image72/112c7968f2cd88ca8d1312d9054aed4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # ========================================================== | |
| # All-in-One: Docker + docker-compose-plugin + 16 GB swap | |
| # Tested on Ubuntu 20.04/22.04/24.04 (x86_64 & arm64) | |
| # 1) 安装官方最新 Docker & Compose | |
| # 2) 创建 16 GB /swapfile 并开机自启 | |
| # 全程无交互,失败即退出 | |
| # ========================================================== | |
| set -euo pipefail | |
| # ---- 0. prepeare root check -------------------------------- | |
| [ "$EUID" -ne 0 ] && exec sudo bash "$0" "$@" | |
| # ---- 1. env ------------------------------------------ | |
| SWAP_SIZE_MB=16384 # 16 GB | |
| SWAPFILE="/swapfile" | |
| DOCKER_CHANNEL="stable" | |
| # ---- 2. clean old Docker / swap ------------------------------ | |
| echo "==> clean old Docker & swap" | |
| systemctl stop docker docker.socket containerd 2>/dev/null || true | |
| apt-get remove -y docker docker-engine docker.io containerd runc 2>/dev/null || true | |
| swapoff "$SWAPFILE" 2>/dev/null || true | |
| rm -f "$SWAPFILE" | |
| # ---- 3. upgrade system ------------------------------------------ | |
| apt-get update -qq && apt-get upgrade -y | |
| # ---- 4. install depends ------------------------------------------ | |
| apt-get install -y \ | |
| ca-certificates curl gnupg lsb-release coreutils | |
| # ---- 5. add Docker offical repo ------------------------------ | |
| install -m 0755 -d /etc/apt/keyrings | |
| curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \ | |
| gpg --dearmor -o /etc/apt/keyrings/docker.gpg | |
| echo \ | |
| "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \ | |
| https://download.docker.com/linux/ubuntu \ | |
| $(lsb_release -cs) $DOCKER_CHANNEL" \ | |
| > /etc/apt/sources.list.d/docker.list | |
| # ---- 6. 安装 Docker + Compose 插件 ------------------------ | |
| apt-get update -qq | |
| apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin | |
| # ---- 7. 把当前用户加入 docker 组 --------------------------- | |
| # (如以 root 运行脚本,$SUDO_USER 为空则跳过) | |
| if [ -n "${SUDO_USER:-}" ]; then | |
| usermod -aG docker "$SUDO_USER" | |
| fi | |
| # ---- 8. 创建 16 GB swap-file ------------------------------ | |
| echo "==> 创建 $((SWAP_SIZE_MB/1024)) GB swap-file" | |
| dd if=/dev/zero of="$SWAPFILE" bs=1M count=$SWAP_SIZE_MB status=progress | |
| chmod 600 "$SWAPFILE" | |
| mkswap "$SWAPFILE" | |
| swapon "$SWAPFILE" | |
| # ---- 9. 开机自动挂载 --------------------------------------- | |
| # 先删除旧行(如果有) | |
| sed -i "\|^$SWAPFILE|d" /etc/fstab | |
| echo "$SWAPFILE none swap sw 0 0" >> /etc/fstab | |
| # ---- 10. 验证结果 ------------------------------------------ | |
| echo "==> check Docker" | |
| docker --version | |
| docker compose version | |
| echo "==> check swap" | |
| swapon --show | |
| free -h | grep -i swap | |
| echo | |
| echo "✅ All complete!Docker & 16 GB swap ready to use." | |
| echo " 如以普通用户身份运行脚本,请重新登录或执行 newgrp docker 免 sudo 使用 docker。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment