Skip to content

Instantly share code, notes, and snippets.

@rimelek
Last active October 13, 2024 18:26
Show Gist options
  • Save rimelek/05241c26a3b10ff8c9cfe1035b787996 to your computer and use it in GitHub Desktop.
Save rimelek/05241c26a3b10ff8c9cfe1035b787996 to your computer and use it in GitHub Desktop.
Compare Docker container runtimes and check the available resources, the kernel version and the visibility of the kernel files (made for a youtube video))
#!/usr/bin/env bash
set -eu -o pipefail
runtimes=(Host runc runsc kata)
YELLOW_START='\033[1;33m'
YELLOW_END='\033[0m'
declare -A COMMANDS=(
[cpus]='nproc'
[memory]='free | grep "Mem" | awk "{print \$2}"'
[kernel]='uname -nrv'
[filesystem]='ls /boot | awk "/vmlinuz-/" | sort -r | head -n1'
)
function runtime_run() {
local mode="$1"
local runtime="$2"
local command="$3"
if [[ "$runtime" != "Host" ]]; then
command="docker run --rm --runtime $runtime ubuntu $command"
fi
case "$mode" in
echo) echo "$command" ;;
yellow) echo -e "${YELLOW_START}${command}${YELLOW_END}" ;;
exec) eval "$command" ;;
*) >&2 echo "Invalid mode: $mode. Valid modes: echo, exec"; return 1
esac
}
function showresult() {
local runtime="$1"
local label="$2"
local command="$3"
runtime_run yellow "$runtime" "$command"
echo -n "$label: "
runtime_run exec "$runtime" "$command"
}
labels=("$@")
if (( "${#labels[@]}" == 0 )); then
for label in "${!COMMANDS[@]}"; do
labels+=("$label")
done
fi
for label in "${labels[@]}"; do
for runtime in "${runtimes[@]}"; do
showresult "$runtime" "$label" "${COMMANDS[$label]}"
echo
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment