-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdps
More file actions
executable file
·48 lines (41 loc) · 1.22 KB
/
dps
File metadata and controls
executable file
·48 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
#
# Docker/Container Process Status
#
# Display container status with enhanced formatting for both docker and nerdctl.
# Supports filtering by container name and automatically handles sudo if needed.
set -euo pipefail
TAB=$'\t'
# Detect container runtime
if command -v docker &>/dev/null; then
CLI=docker
elif command -v nerdctl &>/dev/null; then
CLI=nerdctl
else
echo "Error: neither docker nor nerdctl found in PATH" >&2
exit 1
fi
# Test if we need sudo
if $CLI ps &>/dev/null; then
CONTAINER_CLI="$CLI"
elif sudo -n $CLI ps &>/dev/null; then
CONTAINER_CLI="sudo $CLI"
else
echo "Error: cannot run $CLI, even with sudo" >&2
exit 1
fi
# Choose format strings
if [[ "$CLI" == "docker" ]]; then
BASE_FORMAT="table {{.Names}}${TAB}{{.Status}}${TAB}{{.Size}}"
FILTER_FORMAT="table {{.Names}}${TAB}{{.Image}}${TAB}{{.CreatedAt}}${TAB}{{.Size}}"
else
BASE_FORMAT="{{.Names}}${TAB}{{.Status}}${TAB}{{.Size}}"
FILTER_FORMAT="{{.Names}}${TAB}{{.Image}}${TAB}{{.CreatedAt}}${TAB}{{.Size}}"
fi
# Main logic
if [[ $# -eq 0 ]]; then
$CONTAINER_CLI ps --format "$BASE_FORMAT" | tail -n +2 | sort
else
FILTER="$1"
$CONTAINER_CLI ps --format "$FILTER_FORMAT" | grep -i -- "$FILTER" | tail -n +2 | sort
fi