Skip to content

Instantly share code, notes, and snippets.

@auser
Last active April 9, 2024 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auser/8ea8be4e1a784e16b9002d79d7410563 to your computer and use it in GitHub Desktop.
Save auser/8ea8be4e1a784e16b9002d79d7410563 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Default configuration
IMAGE_NAME="auser/dev"
# Color definitions
declare -A Colors=(
[Color_Off]='\033[0m'
[Black]='\033[0;30m'
[Red]='\033[0;31m'
[Green]='\033[0;32m'
[Yellow]='\033[0;33m'
[Blue]='\033[0;34m'
[Purple]='\033[0;35m'
[Cyan]='\033[0;36m'
[White]='\033[0;37m'
[BBlack]='\033[1;30m'
[BRed]='\033[1;31m'
[BGreen]='\033[1;32m'
[BYellow]='\033[1;33m'
[BBlue]='\033[1;34m'
[BPurple]='\033[1;35m'
[BCyan]='\033[1;36m'
[BWhite]='\033[1;37m'
[UBlack]='\033[4;30m'
[URed]='\033[4;31m'
[UGreen]='\033[4;32m'
[UYellow]='\033[4;33m'
[UBlue]='\033[4;34m'
[UPurple]='\033[4;35m'
[UCyan]='\033[4;36m'
[UWhite]='\033[4;37m'
)
CLEANUP_IMAGE=false
FORCE_REBUILD_IMAGE=false
TAG="latest"
RUN_PRIVILEGED=true
declare -a MOUNTS=("$(pwd)/ansible:/app" "$(pwd)/ansible_config:/etc/ansible")
echo_color() {
local color="$1"
local message="${@:2}"
echo -e "${Colors[$color]}${message}${Colors[Color_Off]}"
}
docker_instance() {
docker ps | grep "$IMAGE_NAME" | awk '{print $1}'
}
build_image() {
local image_id=$(docker images --filter=reference="$IMAGE_NAME" --format "{{.ID}}")
if [[ "$FORCE_REBUILD_IMAGE" == "true" && -n "$image_id" ]]; then
docker rmi "$image_id"
fi
local cmd=(docker build -f ./docker/Dockerfile -t "$IMAGE_NAME:$TAG")
[[ "$FORCE_REBUILD_IMAGE" == "true" ]] && cmd+=(--no-cache)
cmd+=(".")
"${cmd[@]}"
}
start_container() {
local docker_instance=$(docker_instance)
if [[ -z "$docker_instance" ]]; then
local cmd=(docker run --rm -it)
[[ "$RUN_PRIVILEGED" == "true" ]] && cmd+=(--privileged)
# Add volume mounts to the command
for mount in "${MOUNTS[@]}"; do
cmd+=(-v "$mount")
done
cmd+=(-d "$IMAGE_NAME" /sbin/init)
# Execute the command
"${cmd[@]}"
sleep 2
fi
}
run_container() {
[[ -z $(docker_instance) ]] && docker run --privileged "$IMAGE_NAME" /sbin/init
}
exec_container() {
local instance=$(docker_instance)
[[ -n "$instance" ]] && docker container exec -it "$instance" /bin/bash
}
stop_container() {
local instance=$(docker_instance)
[[ -n "$instance" ]] && docker kill "$instance"
}
rm_container() {
local instance=$(docker_instance)
[[ -n "$instance" ]] && docker rm "$instance"
}
image_id() {
local image_id=$(docker images --filter=reference="$IMAGE_NAME" --format "{{.ID}}")
echo "$image_id"
}
container_info() {
local instance=$(docker_instance)
if [[ -n "$instance" ]]; then
local ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$instance")
local name=$(docker inspect -f '{{.Name}}' "$instance")
local mounts=$(docker inspect -f "{{ range .Mounts }}${Colors[Cyan]}{{ .Source }}${Colors[Color_Off]} => ${Colors[Red]}{{.Destination}}${Colors[Color_Off]}\n{{ end }}" "$instance")
echo_color "BBlack" "-------- Container info --------"
echo_color "Cyan" "Container ID: $instance"
echo_color "Green" "IP Address: $ip"
echo_color "Yellow" "Image ID: $IMAGE_NAME"
echo_color "Purple" "Container Name: $name"
echo -e "Mounts: \n$mounts"
fi
}
cleanup() {
local instance=$(docker_instance)
if [[ -n "$instance" ]]; then
stop_container
rm_container
fi
if [[ "$CLEANUP_IMAGE" == "true" ]]; then
local image_id=image_id
[[ -n "$image_id" ]] && docker rmi "$image_id"
fi
}
start_and_exec() {
start_container
exec_container
}
parse_opts() {
local opt
while getopts ":cfi:t:m:p" opt; do
case ${opt} in
c ) CLEANUP_IMAGE=true ;;
f ) FORCE_REBUILD_IMAGE=true ;;
i ) IMAGE_NAME=$OPTARG ;;
t ) TAG=$OPTARG ;;
m ) MOUNTS+=("$OPTARG") ;;
p ) RUN_PRIVILEGED=false ;;
\? ) echo "Invalid option: $OPTARG" 1>&2; exit 1 ;;
esac
done
}
help() {
echo -e "Usage: $(basename "$0") [options] <command>
Options:
-i Set custom image name (default: $IMAGE_NAME)
-c Cleanup the docker container after stopping
-f Force rebuild the docker image
-t Set the tag for the docker image (default: $TAG)
-m Add a volume mount to the container (can be used multiple times)
-p Do not run in privileged mode
Commands:
${Colors[Green]}build${Colors[Color_Off]} Build the docker image
${Colors[Green]}start${Colors[Color_Off]} Start and exec into the docker container
${Colors[Green]}stop${Colors[Color_Off]} Stop the docker container
${Colors[Green]}run${Colors[Color_Off]} Run the docker container
${Colors[Green]}info${Colors[Color_Off]} Display information about the running container
${Colors[Green]}cleanup${Colors[Color_Off]} Remove the docker container
"
exit 1
}
main() {
parse_opts "$@"
shift $((OPTIND - 1))
case "$1" in
build) build_image ;;
start) start_and_exec ;;
stop) stop_container ;;
run) run_container ;;
info) container_info ;;
cleanup) cleanup ;;
*) help ;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment