-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00-docker-baids
executable file
·169 lines (119 loc) · 3.83 KB
/
00-docker-baids
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
function docker-container-grep() {
PATTERN=$@
docker ps -a | grep $PATTERN | grep -v ^CONTAINER
}
function docker-container-grep-running() {
PATTERN=$@
docker ps | grep $PATTERN | grep -v ^CONTAINER
}
function docker-container-kill-pattern() {
PATTERN=$@
docker-container-grep-running $PATTERN | awk '{print $1}' | xargs -rI % bash -c 'echo $(docker kill %; echo "killed!");'
}
function docker-container-most-recent() {
docker ps | grep -v ^CONTAINER | head -n1 | awk '{print $1}'
}
function docker-container-most-recent-killed() {
docker ps -a | grep Exited | head -n1 | awk '{print $1}'
}
function docker-container-diff-most-recent() {
LAST_CONTAINER=$(docker-container-most-recent)
if [ ! -z "$LAST_CONTAINER" ]
then
docker diff $LAST_CONTAINER
else
echo "There are no running containers!"
fi
}
function docker-container-get-ip() {
CONTAINER_ID=$1
if [ -z "$CONTAINER_ID" ]
then
CONTAINER_ID=$(docker-container-most-recent)
fi
if [ ! -z "$CONTAINER_ID" ]
then
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CONTAINER_ID
else
echo "There are no running containers!"
fi
}
function docker-container-inspect-most-recent() {
LAST_CONTAINER=$(docker-container-most-recent)
if [ ! -z "$LAST_CONTAINER" ]
then
docker inspect $LAST_CONTAINER
else
echo "There are no running containers!"
fi
}
function docker-container-get-root() {
CONTAINER_ID=$1
if [ -z "$CONTAINER_ID" ]
then
CONTAINER_ID=$(docker-container-most-recent)
fi
if [ ! -z "$CONTAINER_ID" ]
then
docker inspect --format '{{ .HostsPath }}' $CONTAINER_ID
else
echo "There are no running containers!"
fi
}
function docker-container-map-port() {
CONTAINER_ID=$1
SRC_PORT=$2
DST_PORT=$3
CONTAINER_IP=$(docker-container-get-ip $CONTAINER_ID)
iptables -t nat -A DOCKER -p tcp --dport $SRC_PORT -j DNAT --to-destination $CONTAINER_IP:$DST_PORT
}
function docker-container-remove-all() {
docker ps -a | grep -v ^CONTAINER | awk '{print $1}' | xargs -rI % bash -c 'docker kill %; docker rm %'
}
function docker-container-remove-all-non-running() {
docker ps -a | grep -v ^CONTAINER | grep Exit | awk '{print $1}' | xargs -rI % bash -c 'docker rm %'
}
function docker-container-remove-pattern() {
PATTERN=$@
docker-container-kill-pattern $PATTERN
docker-container-grep $PATTERN | awk '{print $1}' | xargs -rI % bash -c 'echo $(docker rm %; echo "removed!")'
}
function docker-enter() {
NSENTER=/usr/local/bin/nsenter
if [ ! -e $NSENTER ]
then
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
fi
if [ -z "$1" ]; then
echo "usage: docker-enter <container id/name> <command to run default:sh>"
else
CONTAINER_ID=$(docker inspect --format {{.State.Pid}} "$1")
if [ -z "$CONTAINER_ID" ]; then
exit 1
fi
shift
"$NSENTER" --target $CONTAINER_ID --mount --uts --ipc --net --pid -- "$@"
fi
}
function docker-image-grep() {
PATTERN=$@
docker images | grep $PATTERN | grep -v ^REPOSITORY
}
function docker-image-most-recent() {
docker images | grep -v ^REPOSITORY | head -n1 | awk '{print $3}'
}
function docker-image-remove-all() {
docker-container-remove-all
docker images -a | grep -v ^REPOSITORY | awk '{print $3}' | xargs -rI % bash -c 'docker rmi %'
}
function docker-image-remove-orphan() {
docker images | grep "<none>" | awk '{print $3}' | xargs -rI % bash -c 'docker rmi %'
}
function docker-image-remove-pattern() {
PATTERN=$@
docker-image-grep $PATTERN | awk '{print $1":"$2}' | xargs -rI % bash -c 'docker rmi %'
}
function docker-volumes-remove-orphan() {
docker volume rm $(docker volume ls -qf dangling=true)
}