Docker相关

安装Docker

脚本安装

来源
境内服务器

bash <(curl -sSL https://gitee.com/SuperManito/LinuxMirrors/raw/main/DockerInstallation.sh)

境外服务器

bash <(curl -sSL https://raw.githubusercontent.com/SuperManito/LinuxMirrors/main/DockerInstallation.sh)

源码安装

适合境外服务器

apt install apt-transport-https ca-certificates curl software-properties-common -y

拉取docker证书

curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

apt源

echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

安装docker

apt update
apt install docker-ce docker-ce-cli containerd.io docker-compose -y

启动docker

systemctl enable docker
systemctl start docker

镜像源

默认镜像源国内拉取速度慢

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.xuanyuan.me",
    "https://docker.1ms.run",
    "https://docker.m.daocloud.io",
    "https://atomhub.openatom.cn"
  ]
}
EOF

管理

管理脚本
nano docker.sh

#!/bin/bash

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # 重置颜色

while true; do
    echo -e "\n${YELLOW}----当前容器----${NC}"
    
    # 获取所有容器列表并编号输出
    # 格式:容器名 状态
    mapfile -t containers < <(docker ps -a --format "{{.Names}} {{.Status}}")
    
    if [ ${#containers[@]} -eq 0 ]; then
        echo "暂无容器"
    else
        for i in "${!containers[@]}"; do
            name=$(echo "${containers[$i]}" | awk '{print $1}')
            status=$(echo "${containers[$i]}" | grep -q "Up" && echo -e "${GREEN}运行中${NC}" || echo -e "${RED}未运行${NC}")
            echo "$((i+1)). $name $status"
        done
    fi
    
    echo -e "${YELLOW}-------------${NC}"
    echo "请选择操作:"
    echo "1. 运行容器"
    echo "2. 停止容器"
    echo "3. 重启容器"
    echo "4. 删除容器及其所有数据"
    echo "q. 退出脚本"
    read -p "请输入选项: " main_opt

    case $main_opt in
        1)
            read -p "请输入序号: " idx
            target=$(echo "${containers[$((idx-1))]}" | awk '{print $1}')
            if [ -z "$target" ]; then echo "序号无效"; continue; fi
            
            if docker ps --format '{{.Names}}' | grep -q "^$target$"; then
                echo -e "${YELLOW}容器正在运行${NC}"
            else
                docker start "$target" > /dev/null
                echo -e "${GREEN}容器运行成功${NC}"
            fi
            ;;
            
        2)
            read -p "请输入序号: " idx
            target=$(echo "${containers[$((idx-1))]}" | awk '{print $1}')
            if [ -z "$target" ]; then echo "序号无效"; continue; fi

            if ! docker ps --format '{{.Names}}' | grep -q "^$target$"; then
                echo -e "${YELLOW}容器未运行${NC}"
            else
                docker stop "$target" > /dev/null
                echo -e "${GREEN}容器停止成功${NC}"
            fi
            ;;

        3)
            read -p "请输入序号: " idx
            target=$(echo "${containers[$((idx-1))]}" | awk '{print $1}')
            if [ -z "$target" ]; then echo "序号无效"; continue; fi
            
            docker restart "$target" > /dev/null
            echo -e "${GREEN}重启成功${NC}"
            ;;

        4)
            read -p "请输入序号: " idx
            target=$(echo "${containers[$((idx-1))]}" | awk '{print $1}')
            if [ -z "$target" ]; then echo "序号无效"; continue; fi

            read -p "请再次确认是否清空 (需要输入 y): " confirm
            if [ "$confirm" == "y" ]; then
                # 停止并删除容器,同时删除关联的匿名卷
                docker rm -f -v "$target" > /dev/null
                echo -e "${RED}容器及其数据已彻底删除${NC}"
            else
                echo "操作已取消"
            fi
            ;;

        q)
            echo "退出程序"
            exit 0
            ;;
        *)
            echo "无效选项,请重新选择"
            ;;
    esac
done

脚本说明
动态获取:脚本每次循环都会通过 docker ps -a 实时获取最新的容器列表和状态。
状态判定
运行:如果容器已在运行,会提示“容器正在运行”;否则执行 start。
停止:如果容器本身就没开,会提示“容器未运行”;否则执行 stop。
重启:无论当前什么状态,执行 restart 都会确保它最终处于运行态。
数据清理:在删除操作中,我加入了 -v 参数,这会尝试删除该容器挂载的匿名卷。如果是具名卷或宿主机路径挂载,Docker 默认不会删除物理文件以保安全。
交互友好:使用了 mapfile 将容器名存入数组,你只需要输入 1, 2, 3 这样的数字序号即可,不需要手打复杂的容器 ID

Docker 恢复到刚安装完的“白纸”状态

docker stop $(docker ps -aq) && docker system prune -a --volumes -f
最后修改:2026 年 04 月 23 日 09 : 29 AM