feat: add on the fly volume creating and mapping to the host including bind mounting

master
Kebler Network System Administrator 2023-01-21 22:47:00 -08:00
parent 9b90e79d4d
commit 4bc6727f20
1 changed files with 63 additions and 39 deletions

View File

@ -9,22 +9,42 @@
try_container () { try_container () {
declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64") declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64")
local mp;local cuser; local hmp; local bind; local vname; local prod; local priv
local dkpath; local hostmp; local evar; local hostmap
[[ $# -lt 1 ]] && echo "image name required to try" && return 1 [[ $# -lt 1 ]] && echo "image name required to try" && return 1
declare OPTION; declare OPTARG; declare OPTIND declare OPTION; declare OPTARG; declare OPTIND
OPTIND=0 OPTIND=0
while getopts 'pt:u:' OPTION; do while getopts 'pr:t:u:m:h:be:s' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in case "$OPTION" in
u) u)
ruser=$OPTARG cuser=$OPTARG
;;
b)
bind=true
;;
s)
save=true
;;
m)
mp=$OPTARG
;;
h)
hmp=$OPTARG
;;
e)
evar="-e $OPTARG"
;; ;;
t) t)
TAG=$OPTARG TAG=$OPTARG
;; ;;
p) p)
PROD=$OPTARG priv=--privileged
;;
r)
prod=$OPTARG
;; ;;
*) echo unknown run option -$OPTARG *) echo unknown run option -$OPTARG
echo "USAGE: try <options>" echo "USAGE: try <options>"
@ -35,12 +55,13 @@ try_container () {
shift $((OPTIND - 1)) shift $((OPTIND - 1))
user=${2:-$ruser} [[ ! $1 ]] && echo must supply an image to try && return 1
image=$([[ $user ]] && echo ${user}/)$1
# :${TAG:-latest} # user=${2:-$ruser}
image=$1
shift 1
if [[ $PROD ]]; then if [[ $prod ]]; then
echo removing any local copy of image $image echo removing any local copy of image $image
docker image rm $image docker image rm $image
host=prod host=prod
@ -53,42 +74,45 @@ try_container () {
name=${image//\//-} name=${image//\//-}
image=$image:${TAG:-latest} image=$image:${TAG:-latest}
bmount () { docker rm try-$name > /dev/null 2>&1
[[ $1 == "-u" ]] && umount=true && shift if [[ $mp ]]; then
declare mp=${1:-opt} if [[ $bind ]];then
declare dir=${2:-${PWD}/_$mp} dir=$(realpath "${hmp:-${PWD}/mnt/$mp}")
vname=try-$name-$mp vname="try-$name${dir//\//-}"
if [[ $umount ]]; then
echo removing volume ....
docker volume rm $vname
echo "volume $vname was removed"
echo "Do you want to also delete the bound volume directory $dir?"
read -n 1 -p "Warning: ALL changes in $mp directory in container will be lost! [y]=>" REPLY
[[ $REPLY != "y" ]] && echo -e "\n" && exit 0
sudo rm -rf $dir
else
mkdir -p $dir mkdir -p $dir
# echo bind $dir to volume $vname
docker volume create --driver local \ docker volume create --driver local \
--opt type=none \ --opt type=none \
--opt device=$dir \ --opt device=$dir \
--opt o=bind \ --opt o=bind "$vname" > /dev/null
$vname else
# echo "type=volume,dst=/$mp,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=$dir" vname="try-$name-${mp//\//-}"
hostmap="-e HOST_MAP=$(id -u):$(id -g)"
dkpath=$(docker info | grep -i "Docker Root Dir" | cut -d':' -f2)/volumes/$vname/_data
hostmp=${hmp:-${PWD}/mnt/$mp}
echo via volume $vname
echo linking $dkpath to $hostmp
ln -s $dkpath -T $hostmp
fi
fi fi
}
echo starting container with image: $image, and name $name echo starting container with image: $image, and name $name
echo at container prompt type \'exit\' to exit from shell and remove trial container echo at container prompt type \'exit\' to exit from shell and remove trial container
docker rm try-$name > /dev/null 2>&1
docker run -i -t --rm --entrypoint /usr/bin/env --name try-$name --hostname try-$host-$name \
-v "$(bmount)":/opt \
$image \
/bin/bash -l
echo "done with session, removing containter try-$name"
bmount -u docker run -i -t --rm $priv $evar $hostmap --entrypoint /opt/scripts/entrypoint.sh \
$([[ $cuser ]] && echo "--user $cuser") \
--name try-$name --hostname try-$host-$name \
$([[ $mp ]] && echo "-v $vname:/$mp") \
"$image" \
"$@"
echo "done with session, removing containter try-$name"
if [[ $mp ]]; then
if [[ ! $save ]]; then
echo removing volume $vname used for mapping
docker volume rm $vname > /dev/null
[[ ! $bind ]] && [[ -h $hostmp ]] && echo "removing link at $hostmp" && rm $hostmp
fi
fi
} }