-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.docker_functions
214 lines (177 loc) · 6.57 KB
/
.docker_functions
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#! .docker_functions
# shellcheck source=.shell_control
# shellcheck disable=SC1091
source "$HOME/.shell_control" || echo "$(tput bold)error: ~/.shell_control not installed!$(tput sgr0)" >&2
# Build images with BuildKit
export DOCKER_BUILDKIT=1
# Docker Aliases
alias dc="docker-compose"
# Docker Clean
dcl() {
run "docker ps -aq | xargs docker kill"
run "docker ps -aq | xargs docker rm"
if [[ $1 == "images" ]]; then # optionally remove old images
run "docker images -aq -f dangling=true | xargs docker rmi --force"
DOCKER_VERSION="$(docker --version | sed 's/Docker version //g;s/,.*//g')"
MAJOR="$(echo "$DOCKER_VERSION" | cut -d. -f1)"
MINOR="$(echo "$DOCKER_VERSION" | cut -d. -f2)"
if [[ $MAJOR -gt 1 ]] || [[ $MAJOR -eq 1 && $MINOR -ge 9 ]]; then
run "docker volume ls -qf dangling=true | xargs docker volume rm"
run "docker system prune --volumes -f"
else
run "docker system prune -f"
show -i "docker version less than 1.9, can not clean up dangling any dangling volumes"
fi
fi
}
ds() {
run "docker ps -aq | xargs docker update --restart=no"
run "docker ps -aq | xargs docker stop"
}
dcli() {
dcl images
}
dps() {
docker ps | while read -r line; do
if [[ $line =~ "CONTAINER ID" ]]; then
echo -e "IP ADDRESS\t$line"
else
CID=$(echo $line | awk '{print $1}')
IP=$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" "$CID")
printf "%s\t%s\n" "${IP}" "${line}"
fi
done
}
running_in_docker() {
awk -F/ '$2 == "docker"' /proc/self/cgroup 2>/dev/null | read -r
}
# add variable DOCKER_ID with UID of most recent container
alias docker-id='export DOCKER_ID=`docker ps -ql` && echo $DOCKER_ID'
# remove all containers
alias docker-rmall='docker stop $(docker ps -qa) && docker rm $(docker ps -qa) && docker rmi $(docker images -q)'
# kill all containers
alias docker-killall='docker kill $(docker ps -q)'
# inspect last container created
alias docker-inspect='docker inspect $(docker ps -ql) | less'
# get ip of container
alias docker-ip='docker inspect -f "{{ .NetworkSettings.IPAddress }}"'
# enter last container
alias docker-ns='nsenter --target $(docker inspect --format {{.State.Pid}} $(docker ps -ql)) --mount --uts --ipc --net --pid'
# docker run -it
alias docker-run='docker run -it '
# following based on https://github.com/rcmorano/docker-baids/blob/master/00-docker-baids
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=$*
# shellcheck disable=SC2016
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 [ -n "$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 [ -n "$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 [ -n "$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 [ -n "$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() {
# shellcheck disable=SC2016
docker ps -a | grep -v ^CONTAINER | awk '{print $1}' | xargs -rI % bash -c 'echo $(docker kill %; echo "killed!"); echo $(docker rm %; echo "removed!")'
}
function docker-container-remove-all-non-running() {
# shellcheck disable=SC2016
docker ps -a | grep -v ^CONTAINER | grep Exit | awk '{print $1}' | xargs -rI % bash -c 'echo $(docker kill %; echo "killed!"); echo $(docker rm %; echo "removed!")'
}
function docker-container-remove-pattern() {
PATTERN=$*
docker-container-kill-pattern "$PATTERN"
# shellcheck disable=SC2016
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
# shellcheck disable=SC2016
docker images -a | grep -v ^REPOSITORY | awk '{print $3}' | xargs -rI % bash -c 'echo $(docker rmi %; echo "removed!");'
}
function docker-image-remove-orphan() {
# shellcheck disable=SC2016
docker images | grep "<none>" | awk '{print $3}' | xargs -rI % bash -c 'echo $(docker rmi %; echo "removed!");'
}
function docker-image-remove-pattern() {
PATTERN=$*
# shellcheck disable=SC2016
docker-image-grep "$PATTERN" | awk '{print $1}' | xargs -rI % bash -c 'echo $(docker rmi %; echo "removed!");'
}