Update docker-env to be smarter

This commit is contained in:
Daniel_I_Am 2022-05-11 20:28:48 +02:00
parent a2e19a4728
commit 047235b341

View File

@ -162,14 +162,75 @@ function see() {
}
function docker-env() {
if [ $# -lt 1 ]; then
echo "Usage: docker-env <image_name> [command]"
return 1
else
name=$(echo -n $1| grep -Eo '^[^:]+' | tr '/' '-')
index=$(docker ps -a --filter "name=docker-env-$name-*" --format '{% raw %}{{.Names}}{% endraw %}' | perl -e 'my $max = 0; while (<>) {my ($n) = $_ =~ /(\d+)$/; if ($n > $max) { $max = $n };}; print $max+1;')
docker run --rm --name "docker-env-$name-$index" -it -v "$(pwd):/work_dir" -w "/work_dir" "$1" "${@:2}"
function usage() {
cat >&2 <<EOS
Usage: docker-env [OPTIONS] [--] IMAGE [COMMAND] [ARG...]
Options:
-p <port> Publish a container's port(s) to the host
-v <volume> Bind mount a volume
EOS
exit 1
}
port=
workdir=/work_dir
volumes=("$(pwd):/work_dir")
while
do
case "$1" in
--)
shift
break
;;
-p)
port=$2
shift
;;
-v)
volumes+=("$2")
shift
;;
*)
break
;;
esac
if [ $# -eq 0 ]
then
usage
fi
shift
done
if [ $# -eq 0 ]
then
usage
fi
image="$1"
shift
cmd="$@"
image_repository=$(echo -n "$image"| grep -Eo '^[^:]+' | tr '/' '-')
index=$(docker ps -a --filter "name=docker-env-$image_repository-*" --format '{% raw %}{{.Names}}{% endraw %}' | perl -e 'my $max = 0; while (<>) {my ($n) = $_ =~ /(\d+)$/; if ($n > $max) { $max = $n };}; print $max+1;')
name="docker-env-$image_repository-$index"
run_command="docker run --rm -it --name \"$name\""
if [ ! -z "$port" ]
then
run_command+=" -p $port"
fi
for volume in $volumes
do
run_command+=" -v $volume"
done
run_command+=" $image $cmd"
eval $run_command
}
function limit_video_size() {