Skip to content

Instantly share code, notes, and snippets.

@jaqque
Created January 9, 2023 20:19
Show Gist options
  • Save jaqque/95c786b542664b750d35582dcb732351 to your computer and use it in GitHub Desktop.
Save jaqque/95c786b542664b750d35582dcb732351 to your computer and use it in GitHub Desktop.
Start a dock container of the named image
#!/usr/bin/env zsh
local docker_args docker_interactive exit_status highlight hostname image name shell
local status_color=cyan
local success_color=cyan
local failure_color=red
typeset -a docker_args docker_interactive
image="${0##*/}"
hostname="${image/:/}"
docker_interactive=(--interactive --tty)
case $image in
alpine) shell=/bin/sh ;;
*) shell=/bin/bash ;;
esac
while [[ "$1" ]]; do
case $1 in
--) shift; break ;;
:*) image=$image$1; shift ;;
--name) name=$2; shift 2 ;;
--hostname) hostname=$2; shift 2 ;;
-d|--detach) unset docker_interactive; docker_args+=($1); shift ;;
--help) echo "Use \"docker container --help\" for help. Don't ask me.";
exit 1 ;;
-i|--interactive|-t|--tty) shift ;;
--init \
|--no-healthcheck \
|--oom-kill-disable \
|--privileged \
|--read-only \
|--rm \
|--sig-proxy) docker_args+=($1); shift ;;
-*) docker_args+=($1 $2); shift 2 ;;
*) break ;;
esac
done
if [[ -z $name ]]; then
name="$hostname-${1:+${1##*/}-}$(date +%Y-%m-%d-%H.%M.%S)"
fi
docker_args+=($docker_interactive)
print -P "%F{$status_color}==>%f $name"
docker run --name $name --hostname $hostname $docker_args $image ${@-$shell};
exit_status=$?
(( exit_status )) && highlight=$failure_color || highlight=$success_color
print -P "%F{$highlight}<==%f $name"
return $exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment