Files
251123-biz-ml-px/hook/nsenter.sh
Wang Sen Di e20ded8c11 -
2025-11-25 16:46:02 +08:00

21 lines
1007 B
Bash

#!/usr/bin/env bash
# 在要使用宿主机执行命令时引入该脚本
# 示例
# # shellcheck disable=SC1091
# if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/nsenter.sh "$0"; fi
_script_path_this=$(realpath "$(ps -p $$ -o args= 2>/dev/null | awk '{print $2}')")
_latter="${_script_path_this#/apps/data}"
if [[ "$CONTAINER_NAME" == "" ]]; then CONTAINER_NAME=$(grep devices </proc/self/cgroup | grep -Po '[0-9a-z]{64}' | cut -c1-12); fi
if [[ "${APPS_DATA}" == "" ]]; then APPS_DATA=$(docker inspect "$CONTAINER_NAME" -f '{{range .Mounts}}{{if eq .Destination "/apps/data"}}{{.Source}}{{end}}{{end}}'); fi
if [[ "${APPS_DATA}" == "" ]]; then
# echo "nsenter.sh 未挂载 /apps/data 到外部路径"
_data_upper_dir=$(docker inspect "$CONTAINER_NAME" -f '{{.GraphDriver.Data.UpperDir}}')
_absolute_path="${_data_upper_dir}$_script_path_this"
else
_absolute_path="${APPS_DATA}$_latter"
fi
nsenter --mount=/host/proc/1/ns/mnt --net=/host/proc/1/ns/net /usr/bin/bash "$_absolute_path"
exit