-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.dockerfunctions
346 lines (305 loc) · 8.7 KB
/
.dockerfunctions
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/bin/bash
# Bash wrappers for docker run commands
export DOCKER_REPO_PREFIX=miel
#
# Helper Functions
#
alias dstopall='docker stop $(docker ps -q)'
dcleanup(){
local containers
mapfile -t containers < <(docker ps -aq 2>/dev/null)
docker rm "${containers[@]}" 2>/dev/null
local volumes
mapfile -t volumes < <(docker ps --filter status=exited -q 2>/dev/null)
docker rm -v "${volumes[@]}" 2>/dev/null
local images
mapfile -t images < <(docker images --filter dangling=true -q 2>/dev/null)
docker rmi "${images[@]}" 2>/dev/null
local dangling_volumes
mapfile -t dangling_volumes < <(docker volume ls -qf dangling=true 2>/dev/null)
docker volume rm "${dangling_volumes[@]}" 2>/dev/null
docker network prune -f 2>/dev/null
docker image prune -a -f --filter "until=1440h" 2>/dev/null
docker system prune -f 2>/dev/null
}
del_stopped(){
local name=$1
local state
state=$(docker inspect --format "{{.State.Running}}" "$name" 2>/dev/null)
if [[ "$state" == "false" ]]; then
docker rm "$name"
fi
}
relies_on(){
for container in "$@"; do
local state
state=$(docker inspect --format "{{.State.Running}}" "$container" 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "$container is not running, starting it for you."
$container
fi
done
}
# creates an nginx config for a local route
nginx_config(){
server=$1
route=$2
cat >"${HOME}/.nginx/conf.d/${server}.conf" <<-EOF
upstream ${server} { server ${route}; }
server {
server_name ${server};
location / {
proxy_pass http://${server};
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$http_host;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header X-Forwarded-Port \$server_port;
proxy_set_header X-Request-Start \$msec;
}
}
EOF
# restart nginx
docker restart nginx
# add host to /etc/hosts
sudo hostess add "$server" 127.0.0.1
# open browser
browser-exec "http://${server}"
}
#
# Container Aliases
#
# nginx(){
# del_stopped nginx
#
# docker run -d \
# --restart always \
# -v "${HOME}/.nginx:/etc/nginx" \
# --net host \
# --name nginx \
# nginx
# }
aws-cli(){
# Generate a random to start the container multiple times under different names
local r=$(( RANDOM % 100 ))
# Do NOT use "-it" because it allocates a pseudo terminal which uses CRLF as linebreak, this malformes output.
# Instead the "-a stdout" with the redirect will make sure output is correctly captured.
docker run --rm \
-a stdout \
-a stderr \
-v "${HOME}/.aws:/root/.aws" \
-v "${HOME}/.kube:/root/.kube" \
-v "$(pwd):/aws" \
-e AWS_PROFILE \
-e AWS_REGION \
-e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY \
-e AWS_SESSION_TOKEN \
--name "aws-cli-${r}" \
amazon/aws-cli "$@"
}
# Export function to be able to use in scripts
export -f aws-cli
# For some usages, its needed to create a separate script named `aws` which will then call this function:
# #!/bin/bash
# aws-cli "$@"
gcloud-cli(){
# Generate a random to start the container multiple times under different names
local r=$(( RANDOM % 100 ))
local boto_config="${HOME}/.boto"
test -f "${boto_config}" || touch -a "${boto_config}"
# Do NOT use "-it" because it allocates a pseudo terminal which uses CRLF as linebreak, this malformes output.
# Instead the "-a stdout" with the redirect will make sure output is correctly captured.
docker run --rm \
-i \
-a stdout \
-a stderr \
-v "${HOME}/.gcloud:/config/.gcloud" \
-v "${boto_config}:/root/.boto" \
-v "${HOME}/certs:/certs" \
-v "${HOME}/.kube:/root/.kube" \
-e CLOUDSDK_CONFIG=/config/.gcloud \
--entrypoint=gcloud \
--name "gcloud-${r}" \
google/cloud-sdk "$@"
}
# Export function to be able to use in scripts
export -f gcloud-cli
# For some usages, its needed to create a separate script named `gcloud` which will then call this function:
# #!/bin/bash
# gcloud-cli "$@"
clickhouse-client(){
local ch_history_file="${HOME}/.clickhouse-client-history"
test -f "${ch_history_file}" || touch -a "${ch_history_file}"
# Determine either to open a TTY or attach to stdin/stdout
if [ ! -t 0 ]; then
local args="-i --attach stdin --attach stdout --attach stderr"
else
local args="-it"
fi
# Generate a random to start the container multiple times under different names
local r=$(( RANDOM % 100 ))
docker run \
${args} \
--rm \
--net=host \
-v "${ch_history_file}:/etc/clickhouse-client/history" \
--entrypoint=clickhouse-client \
--name "clickhouse-client-${r}" \
clickhouse/clickhouse-server --history_file=/etc/clickhouse-client/history "$@"
}
dive(){
docker run --rm -it \
-v /var/run/docker.sock:/var/run/docker.sock \
--name dive \
wagoodman/dive:latest "$@"
}
gimp(){
del_stopped gimp
docker run -d \
-v /etc/localtime:/etc/localtime:ro \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e "DISPLAY=unix${DISPLAY}" \
-v "${HOME}/Documents:/root/Documents" \
-v "${HOME}/Downloads:/root/Downloads" \
-v "${HOME}/.gtkrc:/root/.gtkrc" \
-e GDK_SCALE \
-e GDK_DPI_SCALE \
--name gimp \
jess/gimp
}
hollywood(){
# Exit using "F6" which disconnects from Byobu session
docker run --rm -it \
--name hollywood \
miel/hollywood
}
htop(){
docker run --rm -it \
--pid host \
--net none \
--name htop \
jess/htop
}
lens(){
del_stopped lens
docker run -d \
-v /etc/localtime:/etc/localtime:ro \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e "DISPLAY=unix${DISPLAY}" \
-v "${HOME}/certs:/certs" \
-v "${HOME}/.aws:/home/lens/.aws" \
-v "${HOME}/.kube:/home/lens/.kube" \
-v "${HOME}/.minikube:${HOME}/.minikube" \
-v "${HOME}/.gcloud:/home/lens/.gcloud" \
-e CLOUDSDK_CONFIG=/home/lens/.gcloud \
-v /dev/shm:/dev/shm \
-v /etc/hosts:/etc/hosts \
--net host \
--device /dev/snd \
--device /dev/dri \
--device /dev/video0 \
--group-add audio \
--group-add video \
--security-opt "seccomp=/etc/docker/seccomp/electron.json" \
-u "$(id -u "${USER}")":"$(id -g "${USER}")" \
--name lens \
${DOCKER_REPO_PREFIX}/lens
}
k9s(){
# Generate a random to start the container multiple times under different names
local r=$(( RANDOM % 100 ))
docker run --rm \
-it \
-v "${HOME}/certs:/certs" \
-v "${HOME}/.aws:/home/k9s/.aws" \
-v "${HOME}/.kube:/home/k9s/.kube" \
-v "${HOME}/.minikube:${HOME}/.minikube" \
-v "${HOME}/.gcloud:/home/k9s/.gcloud" \
-e CLOUDSDK_CONFIG=/home/k9s/.gcloud \
-v /etc/hosts:/etc/hosts \
--net host \
-u "$(id -u "${USER}")":"$(id -g "${USER}")" \
--name "k9s-${r}" \
${DOCKER_REPO_PREFIX}/k9s
}
mitmproxy(){
docker run --rm -it \
-p 9090:8080 \
mitmproxy/mitmproxy mitmdump
}
virtualbox(){
del_stopped virtualbox
docker run -d \
-v /etc/localtime:/etc/localtime:ro \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e "DISPLAY=unix${DISPLAY}" \
--privileged \
--name virtualbox \
jess/virtualbox
}
eclipse-mat(){
del_stopped eclipse-mat
docker run -d \
--cpus=4 \
-v /etc/localtime:/etc/localtime:ro \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e "DISPLAY=unix${DISPLAY}" \
-v "${HOME}/Downloads:/root/Downloads" \
--name eclipse-mat \
${DOCKER_REPO_PREFIX}/eclipse-mat
}
shellcheck(){
# Do NOT use "-it" because it allocates a pseudo terminal which uses CRLF as linebreak, this malformes output.
# Instead the "-a stdout" with the redirect will make sure output is correctly captured.
docker run --rm \
-a stdout \
-a stderr \
-v "$(pwd):/usr/src:ro" \
--workdir /usr/src \
--name shellcheck \
jess/shellcheck \
shellcheck "$@"
}
vlc() {
del_stopped vlc
docker run -d \
-v /etc/localtime:/etc/localtime:ro \
-v "$XDG_RUNTIME_DIR/pulse:$XDG_RUNTIME_DIR/pulse" \
-v /dev/shm:/dev/shm \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e "DISPLAY=unix${DISPLAY}" \
-e GDK_SCALE \
-e GDK_DPI_SCALE \
-e QT_DEVICE_PIXEL_RATIO \
-e "PULSE_SERVER=unix:$XDG_RUNTIME_DIR/pulse/native" \
-v "${HOME}/Downloads:/home/vlc/Torrents" \
--device /dev/dri \
--name vlc \
jess/vlc
}
transmission-ui(){
del_stopped transmission-ui
docker run -d \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e "DISPLAY=unix${DISPLAY}" \
-v "${HOME}/Downloads:/root/Downloads" \
--name transmission-ui \
jess/transmission-ui
}
transmission(){
del_stopped transmission
docker run -d \
-v /etc/localtime:/etc/localtime:ro \
-v "${HOME}/Downloads:/transmission/download" \
-v "${HOME}/.transmission:/transmission/config" \
-p 9091:9091 \
-p 51413:51413 \
-p 51413:51413/udp \
--name transmission \
jess/transmission
sudo hostess add transmission "$(docker inspect --format '{{.NetworkSettings.Networks.bridge.IPAddress}}' transmission)"
browser-exec "http://transmission:9091"
}