-
Notifications
You must be signed in to change notification settings - Fork 5
/
.dockerfunctions
589 lines (529 loc) · 17.5 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#!/usr/bin/env bash
# Bash wrappers for docker run commands
export DOCKER_REPO_PREFIX=jess
#
# Helper Functions
#
alias dpsa="docker ps --all"
alias ddown='docker rm --force'
dlog() {
docker logs -f --tail 100 "$@"
}
# cleans all docker resources. run it cautiously
dcleanup() {
docker container prune --force 2>/dev/null
docker volume prune --force 2>/dev/null
docker image prune --all 2>/dev/null
# docker network prune --force 2>/dev/null
docker builder prune --force 2>/dev/null
docker buildx prune --force 2>/dev/null
}
# deletes stopped containers
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
}
# bring up local container registry
ctr-registry-up() {
local state=$(docker inspect --format "{{.State.Running}}" registry 2>/dev/null)
if [[ "$state" == "true" ]] || [[ "$state" != "" ]]; then
docker rm -f registry
fi
docker-compose -f $DEV_ZONE_CONFIG_PATH/kubernetes/clusters/docker-compose.yml up -d
echo 'registry started successfully...'
}
# down local container registry
ctr-rgistry-down() {
docker-compose -f $DEV_ZONE_CONFIG_PATH/kubernetes/clusters/docker-compose.yml down
echo 'registry is down successfully'
}
# checks if target container relies on other and ensure to run dependent
# containers before running the target container
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
}
nginx-proxy() {
del_stopped nginx-proxy
local state=$(docker inspect --format "{{.State.Running}}" nginx-proxy 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
docker run -it -d \
--net ronsvpn \
--publish 80:80 \
--publish 443:443 \
--name nginx-proxy \
--volume /var/run/docker.sock:/tmp/docker.sock:ro \
--volume $DEV_ZONE_CONFIG_PATH/nginx/certs:/etc/nginx/certs:ro \
--volume $DEV_ZONE_CONFIG_PATH/nginx/vhost.d:/etc/nginx/vhost.d:ro \
--volume $DEV_ZONE_CONFIG_PATH/nginx/conf.d/custom_settings.conf:/etc/nginx/conf.d/custom_settings.conf:ro \
nginxproxy/nginx-proxy:alpine
else
echo 'proxy is already running'
fi
}
portainer() {
relies_on nginx-proxy
del_stopped portainer
local state=$(docker inspect --format "{{.State.Running}}" portainer 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "portainer server is not running, starting it for you."
docker run -it -d \
--volume /var/run/docker.sock:/var/run/docker.sock:ro \
--volume $DEV_ZONE_CONFIG_PATH/portainer:/data \
--env VIRTUAL_HOST=portainer.nurrony.localhost \
--expose 9000 \
--net ronsvpn \
--name portainer \
portainer/portainer
else
echo 'portainer is already running'
fi
}
# creates an nginx config for a local route
nginx_config() {
server=$1
route=$2
cat >$DEV_ZONE_CONFIG_PATH/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
hostess add $server 127.0.0.1
# open browser
open "http://${server}"
}
dhtop() {
docker run --rm -it \
--pid host \
--net none \
--name htop \
${DOCKER_REPO_PREFIX}/htop
}
mysqlserver() {
local VERSION=${1:-8}
local PORT=${2:-3306}
del_stopped mysqlserver${VERSION}
local state=$(docker inspect --format "{{.State.Running}}" mysqlserver$VERSION 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "mysql $VERSION server is not running, starting it for you."
mkdir -p $DEV_ZONE_CONFIG_PATH/databases/mysql/${VERSION} && \
docker run -it -d \
--volume $DEV_ZONE_CONFIG_PATH/databases/mysql/${VERSION}:/var/lib/mysql \
--env MYSQL_ROOT_PASSWORD=nurrony \
--publish $PORT:3306 \
--net ronsvpn \
--name mysqlserver${VERSION} \
mysql:${VERSION}
else
echo "mysql server $VERSION is already running"
fi
}
mysql() {
relies_on mysqlserver
local RUNNING_DBSERVER_NAME=$(docker ps --filter "name=mysqlserver" --format "{{.Names}}")
docker exec -it $RUNNING_DBSERVER_NAME mysql "$@"
}
mysql5() {
docker exec -it mysqlserver5 mysql "$@"
}
dynamodb() {
del_stopped dynamodb
relies_on nginx-proxy
sleep 2
local state=$(docker inspect --format "{{.State.Running}}" dynamodb 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "local dynamodb server is not running, starting it for you."
docker run -dit \
--volume $DEV_ZONE_CONFIG_PATH/databases/dynamodb:/home/dynamodblocal/data \
--workdir /home/dynamodblocal \
--publish "8000:8000" \
--expose 8000 \
--env VIRTUAL_HOST=dynamodb.nurrony.localhost \
--net ronsvpn \
--name dynamodb \
amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data
if [ "$1" != "" ]; then
docker network connect $1 dynamodb
fi
else
echo 'local dynamodb server is already running'
fi
}
pma() {
del_stopped pma
relies_on nginx-proxy
sleep 2
local state=$(docker inspect --format "{{.State.Running}}" pma 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "phpMyAdmin is not running, starting it for you."
docker run -it -d \
--env UPLOAD_LIMIT=1024M \
--env VIRTUAL_HOST=pma.nurrony.localhost \
--env PMA_ARBITRARY=1 \
--net ronsvpn \
--expose 80 \
--name pma \
phpmyadmin/phpmyadmin
else
echo 'phpMyAdmin is already running'
fi
}
myblog() {
relies_on nginx-proxy
relies_on mysqlserver
sleep 5
del_stopped personal-blog
local state=$(docker inspect --format "{{.State.Running}}" personal-blog 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "blog server is not running, starting it for you."
docker run -it -d \
--volume ${DEV_ZONE}/projects/open-sources/nurrony.info/ghost-content:/var/lib/ghost/content \
--env VIRTUAL_HOST=blog.nurrony.localhost \
--env url=http://blog.nurrony.localhost \
--env database__client=mysql \
--env database__connection__host=mysqlserver \
--env database__connection__user=root \
--env database__connection__password=nurrony \
--env database__connection__database=rons_blog \
--env DEV_DOMAIN=http://blog.nurrony.localhost \
--net ronsvpn \
--name personal-blog \
ghost:2-alpine
else
echo 'blog is already running'
fi
}
mongoserver() {
local VERSION=${1:-7}
local PORT=${2:-27017}
local DBNAME=${3:-experiments}
local state=$(docker inspect --format "{{.State.Running}}" mongoserver$VERSION 2>/dev/null)
del_stopped mongoserver${VERSION}
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "mongoserver ${VERSION}.x.x is not running, starting it for you."
docker run -dit \
--user $(id -u):$(id -g) \
--volume $DEV_ZONE_CONFIG_PATH/databases/mongodb/$VERSION:/data/db \
--env MONGODB_INITDB_ROOT_USERNAME=root \
--env MONGODB_INITDB_ROOT_PASSWORD=nurrony \
--publish $PORT:27017 \
--net ronsvpn \
--name mongoserver$VERSION \
mongo:${VERSION}
else
echo "mongoserver $VERSION.x.x is already running"
fi
}
mongo() {
docker exec -t mongoserver5 mongo "$@"
}
kafka() {
local CONFLUENT_PLATFORM_VERSION=${1:-7.7.1}
del_stopped kafka
local state=$(docker inspect --format "{{.State.Running}}" kafka 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "kafka broker ${VERSION} is not running, starting it for you."
docker run --detach -it \
--name kafka \
--net ronsvpn \
--publish 9092:9092 \
--publish 9102:9102 \
--env KAFKA_NODE_ID=1 \
--env KAFKA_JMX_PORT=9102 \
--env KAFKA_JMX_HOSTNAME=localhost \
--env CLUSTER_ID=MkU3OEVBNTcwNTJENDM3Qk \
--env KAFKA_TRANSACTION_STATE_LOG_MIN_ISR=1 \
--env KAFKA_PROCESS_ROLES=broker,controller \
--env KAFKA_LOG_DIRS=/tmp/kraft-combined-logs \
--env KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS=0 \
--env KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
--env KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT \
--env KAFKA_CONTROLLER_LISTENER_NAMES=CONTROLLER \
--env KAFKA_CONTROLLER_QUORUM_VOTERS=1@kafka:29094 \
--env KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=1 \
--env KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092 \
--env KAFKA_LISTENERS=PLAINTEXT://kafka:29092,CONTROLLER://kafka:29094,PLAINTEXT_HOST://0.0.0.0:9092 \
--env KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT \
confluentinc/cp-kafka:${CONFLUENT_PLATFORM_VERSION:-latest}
else
echo "kafka broker $CONFLUENT_PLATFORM_VERSION is already running"
fi
}
mailserver() {
del_stopped mailserver
relies_on nginx-proxy
sleep 2
local state=$(docker inspect --format "{{.State.Running}}" mailserver 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "mailserver is not running. starting is for you"
docker run -d \
--net ronsvpn \
--expose 8025 \
--name mailserver \
--publish 1025:1025 \
--publish 1110:1110 \
--env TZ=Asia/Dhaka \
--env VIRTUAL_PORT=8025 \
--env MP_DATABASE=/data/mailpit.db \
--env MP_SMTP_AUTH_ACCEPT_ANY=true \
--env MP_SMTP_AUTH_ALLOW_INSECURE=true \
--env VIRTUAL_HOST=mail.nurrony.localhost \
--volume $DEV_ZONE_CONFIG_PATH/mailserver:/data \
axllent/mailpit:latest
else
echo "mailserver is already running"
fi
}
zipkin() {
del_stopped zipkin
relies_on nginx-proxy
sleep 2
local state=$(docker inspect --format "{{.State.Running}}" zipkin 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "zipkin 3.x.x server is not running, starting it for you."
docker run --detach -it \
--name zipkin \
--net ronsvpn \
--publish 9411:9411 \
--env VIRTUAL_PORT=9411 \
--env VIRTUAL_HOST=zipkin.nurrony.localhost \
openzipkin/zipkin:3
else
echo "zipkin 3.x.x is already running"
fi
}
# spawn up postgres server. takes version as params. defaults to 16.x.x
pgserver() {
local VERSION=${1:-17}
local PORT=${2:-5432}
local DBNAME=${3:-experiments}
del_stopped pgserver${VERSION}
local state=$(docker inspect --format "{{.State.Running}}" pgserver$VERSION 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
mkdir -p $DEV_ZONE_CONFIG_PATH/databases/postgres/${VERSION}
echo "postgres $VERSION.x.x server is not running, starting it for you."
docker run -it -d \
--net ronsvpn \
--ulimit memlock=-1:-1 \
--memory-swappiness=0 \
--volume $DEV_ZONE_CONFIG_PATH/databases/postgres/${VERSION}:/var/lib/postgresql/data \
--env POSTGRES_USER=postgres \
--env POSTGRES_DB=$DBNAME \
--env POSTGRES_PASSWORD=nurrony \
--publish $PORT:5432 \
--name pgserver$VERSION \
postgres:$VERSION
else
echo "postgres $VERSION.x.x is already running"
fi
}
psql() {
relies_on pgserver
local RUNNING_PGSERVER_NAME=$(docker ps --filter "name=pgserver" --format "{{.Names}}")
docker exec -it $RUNNING_PGSERVER_NAME psql "$@"
}
pga() {
del_stopped pga
relies_on nginx-proxy
sleep 2
local state=$(docker inspect --format "{{.State.Running}}" pga 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "pgAdmin4 is not running, starting it for you."
docker run -dit \
--env VIRTUAL_HOST=pga.nurrony.localhost \
--env [email protected] \
--env PGADMIN_DEFAULT_PASSWORD=nurrony \
--env PGADMIN_DISABLE_POSTFIX=yes \
--net ronsvpn \
--expose 80 \
--name pga \
dpage/pgadmin4
else
echo 'pgAdmin4 is already running'
fi
}
elasticsearch() {
# Add these extra environment variables if needed
# --env "bootstrap.memory_lock=true"
# --env "ES_JAVA_OPTS=-Xms512m -Xmx512m"
del_stopped elasticsearch
local state=$(docker inspect --format "{{.State.Running}}" elasticsearch 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "elasticsearch server is not running, starting it for you."
docker run -it -d \
--volume "$DEV_ZONE_CONFIG_PATH/databases/elasticsearch/data:/usr/share/elasticsearch/data" \
--publish 9200:9200 \
--publish 9300:9300 \
--net ronsvpn \
--name elasticsearch \
--ulimit memlock=-1:-1 \
--env VIRTUAL_PORT=9200 \
--env "http.cors.enabled=true" \
--env "ELASTIC_PASSWORD=nurrony" \
--env "discovery.type=single-node" \
--env "xpack.security.enabled=true" \
--env "http.cors.allow-origin=/.*/" \
--env "http.max_content_length=200mb" \
--env "node.name=local-elasticsearch" \
--env "cluster.name=local-elasticsearch" \
--env VIRTUAL_HOST=elasticserver.nurrony.localhost \
--env "http.cors.allow-headers: X-Requested-With,Content-Type,Content-Length,Authorization" \
docker.elastic.co/elasticsearch/elasticsearch:7.17.24 "$@"
else
echo 'elasticsearch server is already running'
fi
}
elasticview() {
del_stopped elasticvue
relies_on elasticsearch
relies_on nginx-proxy
sleep 2
local state=$(docker inspect --format "{{.State.Running}}" elasticview 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
docker run -dit \
--expose 8080 \
--net ronsvpn \
--name elasticview \
--env VIRTUAL_PORT=8080 \
--env VIRTUAL_HOST=elasticsearch.nurrony.localhost \
--volume "$DEV_ZONE_CONFIG_PATH/databases/elasticsearch/default_clusters.json:/usr/share/nginx/html/api/default_clusters.json:ro" \
cars10/elasticvue
else
echo 'elasticsearch gui is already running'
fi
}
containerssh() {
docker exec -it "$@"
}
composer() {
docker run --rm --interactive --tty \
--volume $PWD:/app \
--user $(id -u):$(id -g) \
composer:lts "$@"
}
apache-php() {
docker run --rm --interactive --tty \
--volume $PWD:/var/www/html \
--user $(id -u):$(id -g) \
--net ronsvpn \
nmrony/php:7-apache "$@"
}
start-lamp-stack() {
nginx-proxy && pma && mysqlserver && echo 'LAMP stack started successfully.'
}
stop-lamp-stack() {
docker rm -f nginx-proxy pma mysqlserver && echo 'LAMP stack stopped successfully.'
}
sysdig() {
del_stopped sysdig-container
docker run -it \
--name sysdig-container \
--privileged \
--volume /var/run/docker.sock:/host/var/run/docker.sock \
--volume /dev:/host/dev \
--volume /proc:/host/proc:ro \
--volume /boot:/host/boot:ro \
--volume /lib/modules:/host/lib/modules:ro \
--volume /usr:/host/usr:ro \
sysdig/sysdig
}
bees() {
docker run -it --rm \
--env NOTARY_TOKEN \
--volume $HOME/.bees:/root/.bees \
--volume $HOME/.boto:/root/.boto \
--volume $HOME/.dev:/root/.ssh:ro \
--log-driver none \
--name bees \
${DOCKER_REPO_PREFIX}/beeswithmachineguns "$@"
}
###
### Awesome sauce by @jpetazzo
###
command_not_found_handle() {
# Check if there is a container image with that name
if ! docker inspect --format '{{ .Author }}' "$1" >&/dev/null; then
echo "$0: $1: command not found"
return
fi
# Check that it's really the name of the image, not a prefix
if docker inspect --format '{{ .Id }}' "$1" | grep -q "^$1"; then
echo "$0: $1: command not found"
return
fi
# docker run -ti -u $(whoami) -w "$HOME" \
# $(env | cut -d= -f1 | awk '{print "-e", $1}') \
# --device /dev/snd \
# --volume /etc/passwd:/etc/passwd:ro \
# --volume /etc/group:/etc/group:ro \
# --volume /etc/localtime:/etc/localtime:ro \
# --volume /home:/home \
# --volume /tmp/.X11-unix:/tmp/.X11-unix \
# "${DOCKER_REPO_PREFIX}/$@"
}
dive() {
del_stopped dive
docker run --rm -it \
--net ronsvpn \
--name dive \
--volume /var/run/docker.sock:/var/run/docker.sock:ro \
wagoodman/dive:latest "$@"
}
function lazyvim() {
docker run --interactive --tty --rm \
--workdir /root alpine:edge sh -uelic '
apk add git lazygit neovim ripgrep alpine-sdk --update
git clone https://github.com/LazyVim/starter ~/.config/nvim
cd ~/.config/nvim
nvim
'
}
keycloak() {
del_stopped keycloak
relies_on nginx-proxy
sleep 2
local state=$(docker inspect --format "{{.State.Running}}" pga 2>/dev/null)
if [[ "$state" == "false" ]] || [[ "$state" == "" ]]; then
echo "starting keycloak server for you"
docker run -dit\
--expose 8080 \
--expose 9000 \
--expose 8443 \
--name keycloak \
--publish 8080:8080 \
--publish 9000:9000 \
--env VIRTUAL_PORT=8080 \
--env KC_HEALTH_ENABLED=true \
--env KC_BOOTSTRAP_ADMIN_USERNAME="root" \
--env KC_BOOTSTRAP_ADMIN_PASSWORD="nurrony" \
--env VIRTUAL_HOST=keycloak.nurrony.localhost \
quay.io/keycloak/keycloak:latest start-dev
else
echo "keycloak server is already running"
fi
}