From a11347b7ebca7b572d0318295f23ee6ff60ea1a4 Mon Sep 17 00:00:00 2001 From: yanhao98 Date: Mon, 24 Aug 2026 04:45:09 +0000 Subject: [PATCH] =?UTF-8?q?docker-userland-proxy-off.sh:=20=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E4=BC=98=E5=8C=96=E5=89=8D=E5=90=8E=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=AF=B9=E6=AF=94=EF=BC=88=E5=9F=BA=E7=BA=BF+=E5=87=80?= =?UTF-8?q?=E9=87=8A=E6=94=BE=EF=BC=8C=E5=90=AB=20used=20=E5=8F=A3?= =?UTF-8?q?=E5=BE=84=E4=B8=8B=E9=99=8D=E6=8F=90=E7=A4=BA=E4=B8=8E=20availa?= =?UTF-8?q?ble=20=E5=8F=82=E8=80=83=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-userland-proxy-off.sh | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/docker-userland-proxy-off.sh b/docker-userland-proxy-off.sh index da49d0c..edeb9fb 100644 --- a/docker-userland-proxy-off.sh +++ b/docker-userland-proxy-off.sh @@ -69,7 +69,12 @@ if [ "$CFG_VAL" = "false" ]; then fi echo -e "${YELLOW}[状态] 当前 daemon.json 未配置 userland-proxy: false(值: ${CFG_VAL})${NC}" -# --- 3. 备份现有 daemon.json --- +# --- 3. 记录优化前内存基线 --- +MEM_BEFORE_USED=$(free -m | awk '/^Mem:/{print $3}') +MEM_BEFORE_AVAIL=$(free -m | awk '/^Mem:/{print $7}') +echo -e "${GREEN}[基线] 优化前内存: used ${MEM_BEFORE_USED}MB / available ${MEM_BEFORE_AVAIL}MB${NC}" + +# --- 4. 备份现有 daemon.json --- mkdir -p /etc/docker if [ -f /etc/docker/daemon.json ]; then BAK="/etc/docker/daemon.json.bak-$(date +%Y%m%d%H%M%S)" @@ -77,7 +82,7 @@ if [ -f /etc/docker/daemon.json ]; then echo -e "${GREEN}[备份] $BAK${NC}" fi -# --- 4. 合并写入 userland-proxy: false(保留原有其他配置)--- +# --- 5. 合并写入 userland-proxy: false(保留原有其他配置)--- if [ -f /etc/docker/daemon.json ]; then if command -v jq >/dev/null 2>&1; then TMP="/etc/docker/daemon.json.tmp.$$" @@ -103,7 +108,7 @@ fi echo -e "${GREEN}[配置] /etc/docker/daemon.json -> userland-proxy: false${NC}" cat /etc/docker/daemon.json -# --- 5. 重启 docker(容器会短暂中断几秒)--- +# --- 6. 重启 docker(容器会短暂中断几秒)--- # 注: systemctl restart 在个别环境下会因时序返回非零,实际已生效; # 因此不在此处退出,改由下面的验证步骤给出真实结果。 echo -e "${YELLOW}[重启] $RESTART_CMD${NC}" @@ -117,7 +122,7 @@ for _ in $(seq 1 15); do sleep 2 done -# --- 6. 验证 --- +# --- 7. 验证 --- PROXY_COUNT=$(pgrep -f '[d]ocker-proxy' 2>/dev/null | wc -l || true) if [ "$PROXY_COUNT" = "0" ]; then echo -e "${GREEN}[验证] docker-proxy 进程数: 0${NC}" @@ -126,8 +131,16 @@ else fi CONF=$(docker info 2>/dev/null | awk -F': ' '/EnableUserlandProxy/{gsub(/ /,"",$2); print $2}' | head -1) echo -e "${GREEN}[验证] daemon.json 配置: userland-proxy: false(docker info 字段: ${CONF:-不显示=false 时正常})${NC}" -echo "--- 内存 ---" -free -m | head -2 +MEM_AFTER_USED=$(free -m | awk '/^Mem:/{print $3}') +MEM_AFTER_AVAIL=$(free -m | awk '/^Mem:/{print $7}') +SAVED=$((MEM_BEFORE_USED - MEM_AFTER_USED)) +echo -e "${GREEN}[内存] 优化前: used ${MEM_BEFORE_USED}MB / available ${MEM_BEFORE_AVAIL}MB${NC}" +echo -e "${GREEN}[内存] 优化后: used ${MEM_AFTER_USED}MB / available ${MEM_AFTER_AVAIL}MB${NC}" +if [ "$SAVED" -gt 0 ]; then + echo -e "${GREEN}[内存] 净释放: ${SAVED}MB(used 口径;available 变化: $((MEM_AFTER_AVAIL - MEM_BEFORE_AVAIL))MB)${NC}" +else + echo -e "${YELLOW}[内存] used 未下降(${SAVED}MB): 可能受页缓存波动影响,以 available 为准(变化: $((MEM_AFTER_AVAIL - MEM_BEFORE_AVAIL))MB)${NC}" +fi echo "--- 容器状态 ---" docker ps --format '{{.Names}}\t{{.Status}}' 2>/dev/null || true echo ""