-
Notifications
You must be signed in to change notification settings - Fork 0
/
dk
executable file
·104 lines (85 loc) · 2.82 KB
/
dk
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
function help() {
echo -e " _ _
__| | |__
/ _\` | / /
\__,_|_\_\\
${GREEN}Docker aliases${NC}
${YELLOW}Usage:${NC}
dk <command> [<args>]
${YELLOW}Some useful dk commands are:${NC}
${GREEN}ls${NC} list images
${GREEN}ps${NC} list active containers
${GREEN}sh${NC} shell into <img>
${GREEN}bind${NC} create volume with current dir and sh <img>
${GREEN}gz${NC} export <image> to *.tar.gz
${GREEN}add${NC} import <image.tar.gz>
${GREEN}mon${NC} monitoring containers with ctop
${GREEN}clean${NC} cleanup unused resources"
exit 1
}
function err() { echo -e "${RED}$* ${NC}"; }
function yes() { echo -e "${GREEN}$* ${NC}"; }
function sh() {
[[ -z "$3" ]] && term='/bin/sh' || term="${*:3}"
docker run -it --rm "$2" "$term"
}
function bind() {
folder=$(basename "$PWD")
[[ -z "$3" ]] && term='/bin/sh' || term="${*:3}"
docker run -it --rm -v "$(pwd):/mnt/$folder" -w "/mnt/$folder" "$2" "$term"
}
function gz() {
filename=$(echo "$1" | sed 's/:/_/' | sed 's/\//_/')
yes "=> Exporting '$1' to $filename.tar.gz"
docker save "$1" | gzip > "$filename.tar.gz"
yes "=> Finish"
}
function clean() {
yes '-> removing stopped containers'
stopped=$(docker ps --filter "status=exited" -q)
[[ -n "$stopped" ]] && docker rm $stopped
yes '=> removing <none> images'
noneimgs=$(docker images | grep "^<none>" | awk '{ print $3 }')
[[ -n "$noneimgs" ]] && docker rmi $noneimgs
yes '-> removing dangling images'
dangling=$(docker images -f "dangling=true" -q)
[[ -n "$dangling" ]] && docker rmi $dangling
yes '-> removing unused networks'
docker network prune -f
yes '-> removing unused local volumes'
docker volume prune -f
}
function mon() {
if ! [ -x "$(command -v ctop)" ]; then
yes '=> Downloading ctop'
version="$(curl --silent https://api.github.com/repos/bcicen/ctop/releases/latest | jq .tag_name -r | cut -c2-)"
arch="darwin-amd64"
[ -f "/etc/os-release" ] && arch="linux-amd64"
curl -L "https://github.com/bcicen/ctop/releases/download/v${version}/ctop-${version}-${arch}" -o "$HOME/bin/ctop"
chmod +x "$HOME/bin/ctop"
fi
ctop
}
function main() {
! [ -x "$(command -v docker)" ] && err 'docker not installed' && exit 1
[[ -z "$1" ]] && help
case "$1" in
ls) docker images --format "$lsfmt" "${@:2}" | tail -n +2 | sort -d ;;
ps) docker ps --format "$psfmt" "${@:2}" | tail -n +2 | sort -d ;;
sh) sh "$@" ;;
bind) bind "$@" ;;
clean) clean ;;
gz) gz "$2" ;;
add) docker load < "$2" ;;
mon) mon ;;
*) docker "$@" ;;
esac
}
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
lsfmt="table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.Size}}"
psfmt="table {{.Names}}\t{{.Image}}:{{.ID}}\t{{.Status}}"
main "$@"