欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > 如何在Debian操作系统上安装Docker

如何在Debian操作系统上安装Docker

2024/10/25 21:13:51 来源:https://blog.csdn.net/qq_19309473/article/details/143197426  浏览:    关键词:如何在Debian操作系统上安装Docker

本章教程,主要介绍如何在Debian 11 系统上安装Docker。主要使用一键安装Docker脚本和一键卸载脚本来完成。

在这里插入图片描述

一、安装Docker

#!/bin/bashRED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'CURRENT_DIR=$(cd "$(dirname "$0")" || exitpwd
)function log() {message="[1Panel Log]: $1 "case "$1" in*"失败"*|*"错误"*|*"请使用 root 或 sudo 权限运行此脚本"*)echo -e "${RED}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log;;*"成功"*)echo -e "${GREEN}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log;;*"忽略"*|*"跳过"*)echo -e "${YELLOW}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log;;*)echo -e "${BLUE}${message}${NC}" 2>&1 | tee -a "${CURRENT_DIR}"/install.log;;esac
}
log "======================= 开始安装Docker ======================="function Check_Root() {if [[ $EUID -ne 0 ]]; thenlog "请使用 root 或 sudo 权限运行此脚本"exit 1fi
}ACCELERATOR_URL="https://docker.1panelproxy.com"
DAEMON_JSON="/etc/docker/daemon.json"
BACKUP_FILE="/etc/docker/daemon.json.1panel_bak"function create_daemon_json() {log "创建新的配置文件 ${DAEMON_JSON}..."mkdir -p /etc/dockerecho '{"registry-mirrors": ["'"$ACCELERATOR_URL"'"]}' | tee "$DAEMON_JSON" > /dev/nulllog "镜像加速配置已添加。"
}function configure_accelerator() {read -p "是否配置镜像加速?(y/n): " configure_acceleratorif [[ "$configure_accelerator" == "y" ]]; thenif [ -f "$DAEMON_JSON" ]; thenlog "配置文件已存在,我们将备份现有配置文件为 ${BACKUP_FILE} 并创建新的配置文件。"cp "$DAEMON_JSON" "$BACKUP_FILE"create_daemon_jsonelsecreate_daemon_jsonfilog "正在重启 Docker 服务..."systemctl daemon-reloadsystemctl restart dockerlog "Docker 服务已成功重启。"elselog "未配置镜像加速。"fi
}function Install_Docker(){if which docker >/dev/null 2>&1; thenlog "检测到 Docker 已安装,跳过安装步骤"configure_acceleratorelselog "... 在线安装 docker"if [[ $(curl -s ipinfo.io/country) == "CN" ]]; thensources=("https://mirrors.aliyun.com/docker-ce""https://mirrors.tencent.com/docker-ce""https://mirrors.163.com/docker-ce""https://mirrors.cernet.edu.cn/docker-ce")docker_install_scripts=("https://get.docker.com""https://testingcf.jsdelivr.net/gh/docker/docker-install@master/install.sh""https://cdn.jsdelivr.net/gh/docker/docker-install@master/install.sh""https://fastly.jsdelivr.net/gh/docker/docker-install@master/install.sh""https://gcore.jsdelivr.net/gh/docker/docker-install@master/install.sh""https://raw.githubusercontent.com/docker/docker-install/master/install.sh")get_average_delay() {local source=$1local total_delay=0local iterations=2local timeout=2for ((i = 0; i < iterations; i++)); dodelay=$(curl -o /dev/null -s -m $timeout -w "%{time_total}\n" "$source")if [ $? -ne 0 ]; thendelay=$timeoutfitotal_delay=$(awk "BEGIN {print $total_delay + $delay}")doneaverage_delay=$(awk "BEGIN {print $total_delay / $iterations}")echo "$average_delay"}min_delay=99999999selected_source=""for source in "${sources[@]}"; doaverage_delay=$(get_average_delay "$source" &)if (( $(awk 'BEGIN { print '"$average_delay"' < '"$min_delay"' }') )); thenmin_delay=$average_delayselected_source=$sourcefidonewaitif [ -n "$selected_source" ]; thenlog "选择延迟最低的源 $selected_source,延迟为 $min_delay 秒"export DOWNLOAD_URL="$selected_source"for alt_source in "${docker_install_scripts[@]}"; dolog "尝试从备选链接 $alt_source 下载 Docker 安装脚本..."if curl -fsSL --retry 2 --retry-delay 3 --connect-timeout 5 --max-time 10 "$alt_source" -o get-docker.sh; thenlog "成功从 $alt_source 下载安装脚本"breakelselog "从 $alt_source 下载安装脚本失败,尝试下一个备选链接"fidoneif [ ! -f "get-docker.sh" ]; thenlog "所有下载尝试都失败了。您可以尝试手动安装 Docker,运行以下命令:"log "bash <(curl -sSL https://linuxmirrors.cn/docker.sh)"exit 1fish get-docker.sh 2>&1 | tee -a ${CURRENT_DIR}/install.logdocker_config_folder="/etc/docker"if [[ ! -d "$docker_config_folder" ]];thenmkdir -p "$docker_config_folder"fidocker version >/dev/null 2>&1if [[ $? -ne 0 ]]; thenlog "docker 安装失败\n您可以尝试使用离线包进行安装,具体安装步骤请参考以下链接:https://1panel.cn/docs/installation/package_installation/"exit 1elselog "docker 安装成功"systemctl enable docker 2>&1 | tee -a "${CURRENT_DIR}"/install.logconfigure_acceleratorfielselog "无法选择源进行安装"exit 1fielselog "非中国大陆地区,无需更改源"export DOWNLOAD_URL="https://download.docker.com"curl -fsSL "https://get.docker.com" -o get-docker.shsh get-docker.sh 2>&1 | tee -a "${CURRENT_DIR}"/install.loglog "... 启动 docker"systemctl enable docker; systemctl daemon-reload; systemctl start docker 2>&1 | tee -a "${CURRENT_DIR}"/install.logdocker_config_folder="/etc/docker"if [[ ! -d "$docker_config_folder" ]];thenmkdir -p "$docker_config_folder"fidocker version >/dev/null 2>&1if [[ $? -ne 0 ]]; thenlog "docker 安装失败\n您可以尝试使用安装包进行安装,具体安装步骤请参考以下链接:https://1panel.cn/docs/installation/package_installation/"exit 1elselog "docker 安装成功"fififi
}function Install_Compose(){docker-compose version >/dev/null 2>&1if [[ $? -ne 0 ]]; thenlog "... 在线安装 docker-compose"arch=$(uname -m)if [ "$arch" == 'armv7l' ]; thenarch='armv7'ficurl -L https://resource.fit2cloud.com/docker/compose/releases/download/v2.26.1/docker-compose-$(uname -s | tr A-Z a-z)-"$arch" -o /usr/local/bin/docker-compose 2>&1 | tee -a "${CURRENT_DIR}"/install.logif [[ ! -f /usr/local/bin/docker-compose ]];thenlog "docker-compose 下载失败,请稍候重试"exit 1fichmod +x /usr/local/bin/docker-composeln -s /usr/local/bin/docker-compose /usr/bin/docker-composedocker-compose version >/dev/null 2>&1if [[ $? -ne 0 ]]; thenlog "docker-compose 安装失败"exit 1elselog "docker-compose 安装成功"fielsecompose_v=$(docker-compose -v)if [[ $compose_v =~ 'docker-compose' ]];thenread -p "检测到已安装 Docker Compose 版本较低(需大于等于 v2.0.0 版本),是否升级 [y/n] : " UPGRADE_DOCKER_COMPOSEif [[ "$UPGRADE_DOCKER_COMPOSE" == "Y" ]] || [[ "$UPGRADE_DOCKER_COMPOSE" == "y" ]]; thenrm -rf /usr/local/bin/docker-compose /usr/bin/docker-composeInstall_Composeelselog "Docker Compose 版本为 $compose_v,可能会影响应用商店的正常使用"fielselog "检测到 Docker Compose 已安装,跳过安装步骤"fifi
}function Get_Ip(){active_interface=$(ip route get 8.8.8.8 | awk 'NR==1 {print $5}')if [[ -z $active_interface ]]; thenLOCAL_IP="127.0.0.1"elseLOCAL_IP=$(ip -4 addr show dev "$active_interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')fiPUBLIC_IP=$(curl -s https://api64.ipify.org)if [[ -z "$PUBLIC_IP" ]]; thenPUBLIC_IP="N/A"fiif echo "$PUBLIC_IP" | grep -q ":"; thenPUBLIC_IP=[${PUBLIC_IP}]1pctl listen-ip ipv6fi
}function main(){Check_RootInstall_DockerInstall_Compose
}
main

当然,你也可以用下面这个在线脚本进行安装。

curl -sSL https://file.ewbang.com/docker/debian/install_docker.sh -o install_docker.sh && bash install_docker.sh

二、卸载Docker

#!/bin/bash# 停止 Docker 服务
echo "Stopping Docker service..."
sudo systemctl stop docker# 卸载 Docker 相关软件包
echo "Removing Docker packages..."
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin# 删除 Docker 数据
echo "Deleting Docker data..."
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd# 删除 Docker 配置文件
echo "Deleting Docker configuration..."
sudo rm -rf /etc/docker# 自动清理不需要的软件包
echo "Cleaning up..."
sudo apt-get autoremove -y
sudo apt-get updateecho "Docker uninstalled successfully."

同样,下面也提供在线脚本。

curl -sSL https://file.ewbang.com/docker/debian/uninstall_docker.sh -o uninstall_docker.sh && bash uninstall_docker.sh

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com