-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdps.sh
60 lines (51 loc) · 1.74 KB
/
dps.sh
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
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# Determine Docker options based on the first argument
case "$1" in
-a)
PS_OPTS="-a"
;;
-s)
PS_OPTS="--filter status=exited"
;;
*)
PS_OPTS=""
;;
esac
# Fetch container IDs and basic info from docker ps
docker_ps_output=$(docker ps $PS_OPTS --format '{{.ID}}|{{.Names}}|{{.Image}}|{{.RunningFor}}')
# Define the header
HEADER="CONTAINER ID|NAMES|IP ADDRESS|IMAGE|RUNNING FOR"
# Check if there are any containers
if [ -z "$docker_ps_output" ]; then
echo "No containers to display."
exit 0
fi
# Initialize output with header
output="$HEADER"$'\n'
# Process each container
while IFS='|' read -r id name image running_for; do
# Get IP addresses via docker inspect and format them properly
ip_address=$(docker inspect -f \
'{{range $k, $v := .NetworkSettings.Networks}}{{if $v.IPAddress}}{{if ne $v.IPAddress ""}}{{$v.IPAddress}} {{end}}{{end}}{{end}}' \
"$id" | sed 's/ $//' | tr ' ' ',')
if [ -z "$ip_address" ]; then
ip_address="N/A"
fi
# Remove 'ago' from running_for
running_for=$(echo "$running_for" | sed 's/ ago//')
# Append the data line to the output
output+="$id|$name|$ip_address|$image|$running_for"$'\n'
done <<< "$docker_ps_output"
# Use column to format the output
formatted_output=$(echo "$output" | column -t -s '|')
# Add separator line after header
# Extract the header line from the formatted output
header_line=$(echo "$formatted_output" | head -n 1)
# Generate a separator line with dashes matching the length of the header line
separator_line=$(echo "$header_line" | sed 's/./-/g')
# Combine header, separator, and the rest of the output
final_output=$(echo "$formatted_output" | sed "1 a\\
$separator_line
")
# Display the final output
echo "$final_output"