-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_aliases.sh
More file actions
246 lines (187 loc) · 5.88 KB
/
Copy pathdocker_aliases.sh
File metadata and controls
246 lines (187 loc) · 5.88 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#----------------------------------------------
# Docker alias and functions
# https://github.com/thombashi/docker-alias
#----------------------------------------------
if command -v fzf > /dev/null 2>&1 ; then
SELECTOR=\fzf
elif command -v peco > /dev/null 2>&1 ; then
SELECTOR=\peco
else
SELECTOR=
fi
select-dimg-tag() {
if ! command -v "$SELECTOR" > /dev/null 2>&1; then
echo "${FUNCNAME[0]}: require fzf|peco" 1>&2
return 1
fi
docker images | $SELECTOR | awk '{print $1,$2}' OFS=:
}
select-dimg-id() {
if ! command -v "$SELECTOR" > /dev/null 2>&1; then
echo "${FUNCNAME[0]}: require fzf|peco" 1>&2
return 1
fi
docker images | $SELECTOR | awk '{print $3}'
}
# Get latest container ID
alias dl="docker ps -l --quiet"
# Get container logs
alias dlogs="docker logs"
# Get container process
alias dps="docker ps"
# Get process included stop container
alias dpsa="docker ps --all"
# List container images
alias dimgs="docker images"
# Get container IP address
alias dip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
# Get container process ID
alias dpid="docker inspect --format '{{ .State.Pid }}'"
# ping to a container
dping() {
local container=$1
local return_code
container_id=$(did "$1" 2> /dev/null)
return_code=$?
if [ "$return_code" != "0" ]; then
echo "Usage: ${FUNCNAME[0]} CONTAINER_ID_OR_NAME " 1>&2
return $return_code
fi
ping "$(dip $container_id)"
}
alias drun="docker run"
# Run deamonized container. e.g. $ drund base /bin/echo hello
alias drund="docker run --detach --tty"
# Run interactive container. e.g. $dki base /bin/bash
#alias druni="docker run --interactive --tty -P"
drunit() {
docker run -it --rm "$(select-dimg-tag)" ${1:-/bin/bash}
}
# Execute interactive container, e.g., $dex base /bin/bash
alias dexec="docker exec --interactive --tty"
# Get container ID from name/ID/image
did() {
local container=$1
if [ "$container" = "" ]; then
echo "Usage: ${FUNCNAME[0]} CONTAINER_ID_OR_NAME " 1>&2
return 22
fi
# try to convert from container name to id
container_id=$(dps --quiet --filter name="$container")
if [ "$container_id" != "" ]; then
echo "$container_id"
return 0
fi
# try to convert from container image to id
container_id=$(dps --quiet --filter ancestor="$container")
if [ "$container_id" != "" ]; then
echo "$container_id"
return 0
fi
# check weather exist id
container_id=$(dps --quiet --filter id="$container")
if [ "$container_id" != "" ]; then
echo "$container_id"
return 0
fi
echo "$container not found " 1>&2
return 22
}
# Get container ID from name/ID/image
dida() {
local container=$1
if [ "$container" = "" ]; then
echo "Usage: ${FUNCNAME[0]} CONTAINER_ID_OR_NAME " 1>&2
return 22
fi
# try to convert from container name to id
container_id=$(dpsa --quiet --filter name="$container")
if [ "$container_id" != "" ]; then
echo "$container_id"
return 0
fi
# try to convert from container image to id
container_id=$(dpsa --quiet --filter ancestor="$container")
if [ "$container_id" != "" ]; then
echo "$container_id"
return 0
fi
# check weather exist id
container_id=$(dpsa --quiet --filter id="$container")
if [ "$container_id" != "" ]; then
echo "$container_id"
return 0
fi
echo "$container not found " 1>&2
return 22
}
# Convert container id to container name
didtoname() {
local container_id
container_id=$(dida "$1") && dpsa --filter id="$container_id" --format "{{ .Names }}"
}
# Stop containers
dstop() {
local container=$1
if [ "$container" = "" ] && [ "$SELECTOR" != "" ]; then
container=$(dps | $SELECTOR | awk '{print $1}')
fi
if [ "$container" = "" ]; then
return 0
fi
docker stop $(did "$container" | tr '\n' ' ')
}
# Stop and remove container(s)
drmi() {
local image_id=$1
local container_id
if [ "$image_id" = "" ] && [ "$SELECTOR" != "" ]; then
image_id=$(select-dimg-id)
fi
if [ "$image_id" = "" ]; then
return 0
fi
# stop running containers which created by the selected docker image
container_id=$(dps --quiet --filter ancestor="$image_id" | tr '\n' ' ')
if [ "$container_id" != "" ]; then
# shellcheck disable=SC2086
docker stop $container_id
fi
container_id=$(dpsa --quiet --filter "status=exited" --filter ancestor="$image_id")
if [ "$container_id" != "" ]; then
# shellcheck disable=SC2086
docker rm $container_id
fi
# shellcheck disable=SC2086
docker rmi $image_id
}
# Stop and remove all of the containers
alias drmall='docker stop $(dpsa --quiet) --time 5 && docker rm $(dpsa --quiet)'
# Remove all of the containers which Exited/Created status
dclean() {
local container
# find not executed docker containers
container=$(docker ps --all --filter "status=exited" --filter "status=created" --quiet)
if [ "$container" != "" ]; then
# shellcheck disable=SC2086
docker rm $container
fi
dprecated_images=$(docker images | \grep '<none>' | awk '{print $3}')
if [ "$dprecated_images" != "" ]; then
# shellcheck disable=SC2086
docker rmi $dprecated_images
fi
}
# Remove all images
#dri() { docker rmi $(docker images --quiet); }
# Dockerfile build, e.g., $dbu tcnksm/test
dbuild() { docker build --tag="$1" .; }
# Show all alias related docker
dalias() { alias | \grep 'docker' | sed "s/^\([^=]*\)=\(.*\)/\1 => \2/"| sed "s/['|\']//g" | sort; }
# Bash into running container
dbash() {
local container_id
container_id=$(did "$1") && docker exec -it $(dpsa -q --filter id="$container_id") bash
}
# Print Docker service journal
alias dockerdlog='journalctl --unit docker.service'