-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerc
executable file
·1335 lines (1153 loc) · 33.3 KB
/
dockerc
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# DockerC (v2.0.0)
# Wrapper for docker compose commands in your project.
# https://github.com/matiboux/dockerc
# MIT License
# Copyright (c) 2023 Matiboux
# This project is not affiliated with Docker, Inc.
VERSION='2.0.0'
ERROR_CODE=''
# Parse arguments
# Dummy while loop to allow breaking
while true; do
# Parse options arguments
while [ "$#" -gt 0 ]; do
case "$1" in
'--help' | '-h' )
# Print general help
DOCKERC_PRINT_HELP='true'
shift
;;
'--version' | '-v' )
# Print version
DOCKERC_PRINT_VERSION='true'
shift
;;
'--force' | '-f' )
# Force, for example ignores abstract contexts
DOCKERC_FORCE='true'
shift
;;
'--dry-run' | '-n' )
# Dry run, print docker compose command without running it
DOCKERC_DRY_RUN='true'
shift
;;
'--quiet' | '-q' )
# Quiet, do not print docker compose command
DOCKERC_QUIET='true'
shift
;;
'--update' )
# Update DockerC
DOCKERC_UPDATE='true'
shift
;;
'--install-dir' | '-i' )
shift
# Check for installation directory argument
if [ -z "$1" ]; then
echo 'Error: Missing installation directory.' >&2
DOCKERC_PRINT_HELP='true'
DOCKERC_UPDATE='true'
ERROR_CODE=1
break
fi
# Set installation directory
DOCKERC_INSTALL_DIR="$1"
shift
;;
* )
break
;;
esac
done
if
[ "$DOCKERC_PRINT_HELP" = 'true' ] ||
[ "$DOCKERC_PRINT_VERSION" = 'true' ] ||
[ "$DOCKERC_UPDATE" = 'true' ]
then
# Stop parsing arguments
break
fi
# Check for optional positional arguments
if [ "$#" -le 0 ]; then
# No more arguments
break
fi
# Parse first optional positional argument
if [ "$1" = '?' ]; then
# Print context help
DOCKERC_PRINT_HELP_CONTEXT='true'
# Stop parsing arguments
break
fi
# Parse docker command preset
if [ "$(echo "$1" | cut -c1)" = '@' ]; then
if [ "$1" = '@' ]; then
# Print docker preset help
DOCKERC_PRINT_HELP_DOCKER_PRESET='true'
# Stop parsing arguments
break
fi
DOCKERC_DOCKER_PRESET="$1"
shift
# Parse update install tag argument
elif [ "$DOCKERC_UPDATE" = 'true' ]; then
DOCKERC_INSTALL_TAG="$1"
shift
# Parse context argument
else
DOCKERC_CONTEXT="$1"
shift
fi
# Check for optional positional arguments
if [ "$#" -le 0 ]; then
# No more arguments
break
fi
# Parse second optional positional argument
# Parse docker command preset
if
[ -z "$DOCKERC_DOCKER_PRESET" ] &&
(
[ "$1" = '$' ] ||
[ "$(echo "$1" | cut -c1)" = '@' ]
)
then
if [ "$1" = '@' ]; then
# Print docker command preset help
DOCKERC_PRINT_HELP_COMPOSE_PRESET='true'
# Stop parsing arguments
break
fi
DOCKERC_COMPOSE_PRESET="$1"
shift
# Check for optional positional arguments
if [ "$#" -le 0 ]; then
# No more arguments
break
fi
# Else, this is a command argument
fi
# Parse trailing positional arguments
# Parse command args
DOCKERC_COMMAND_ARGS=''
for ARG in "$@"; do
if [ -z "$ARG" ]; then
# Skip empty arguments
continue
fi
if [ "$(echo "$ARG" | grep "[ '\"\$\`;*&#<>|]")" ]; then
# Escape argument with spaces or special shell characters
# Replace single quotes with escaped single quotes
ARG="'$(echo "$ARG" | sed "s/'/'\"'\"'/g")'"
fi
DOCKERC_COMMAND_ARGS="$DOCKERC_COMMAND_ARGS $ARG"
done
# Remove leading space
DOCKERC_COMMAND_ARGS="${DOCKERC_COMMAND_ARGS# }"
# Stop parsing arguments
break
done
if [ "$DOCKERC_DEBUG_ARGS" = 'true' ]; then
# Print parsed arguments
echo "DOCKERC_PRINT_HELP: $DOCKERC_PRINT_HELP"
echo "DOCKERC_PRINT_HELP_CONTEXT: $DOCKERC_PRINT_HELP_CONTEXT"
echo "DOCKERC_PRINT_HELP_DOCKER_PRESET: $DOCKERC_PRINT_HELP_DOCKER_PRESET"
echo "DOCKERC_PRINT_HELP_COMPOSE_PRESET: $DOCKERC_PRINT_HELP_COMPOSE_PRESET"
echo "DOCKERC_PRINT_VERSION: $DOCKERC_PRINT_VERSION"
echo "DOCKERC_FORCE: $DOCKERC_FORCE"
echo "DOCKERC_DRY_RUN: $DOCKERC_DRY_RUN"
echo "DOCKERC_QUIET: $DOCKERC_QUIET"
echo "DOCKERC_UPDATE: $DOCKERC_UPDATE"
echo "DOCKERC_INSTALL_DIR: $DOCKERC_INSTALL_DIR"
echo "DOCKERC_CONTEXT: $DOCKERC_CONTEXT"
echo "DOCKERC_COMPOSE_PRESET: $DOCKERC_COMPOSE_PRESET"
echo "DOCKERC_COMMAND_ARGS: $DOCKERC_COMMAND_ARGS"
exit ${ERROR_CODE:-0}
fi
FORCE='false'
if [ -n "$DOCKERC_FORCE" ]; then
# Use from argument or environment variable
FORCE="$DOCKERC_FORCE"
fi
DRY_RUN='false'
if [ -n "$DOCKERC_DRY_RUN" ]; then
# Use from argument or environment variable
DRY_RUN="$DOCKERC_DRY_RUN"
fi
QUIET='false'
if [ -n "$DOCKERC_QUIET" ]; then
# Use from argument or environment variable
QUIET="$DOCKERC_QUIET"
fi
UPDATE='false'
if [ -n "$DOCKERC_UPDATE" ]; then
# Use from argument or environment variable
UPDATE="$DOCKERC_UPDATE"
fi
INSTALL_DIR="$(dirname "$0")"
if [ -n "$DOCKERC_INSTALL_DIR" ]; then
# Use from argument or environment variable
INSTALL_DIR="$DOCKERC_INSTALL_DIR"
fi
INSTALL_TAG=''
if [ -n "$DOCKERC_INSTALL_TAG" ]; then
# Use from argument or environment variable
INSTALL_TAG="$DOCKERC_INSTALL_TAG"
fi
DOCKER_PRESET=''
if [ -n "$DOCKERC_DOCKER_PRESET" ]; then
# Use from argument or environment variable
DOCKER_PRESET="$DOCKERC_DOCKER_PRESET"
fi
CONTEXT=''
if [ -n "$DOCKERC_CONTEXT" ]; then
# Use from argument or environment variable
CONTEXT="$DOCKERC_CONTEXT"
fi
COMPOSE_PRESET=''
if [ -n "$DOCKERC_COMPOSE_PRESET" ]; then
# Use from argument or environment variable
COMPOSE_PRESET="$DOCKERC_COMPOSE_PRESET"
fi
COMMAND_ARGS=''
if [ -n "$DOCKERC_COMMAND_ARGS" ]; then
# Use from argument or environment variable
COMMAND_ARGS="$DOCKERC_COMMAND_ARGS"
fi
if [ "$DOCKERC_PRINT_HELP" = 'true' ]; then
if [ "$UPDATE" = 'true' ]; then
# Print update help & exit
echo "Usage: $0 --update [options] [tag]"
echo ''
echo 'Options:'
echo ' -h, --help Print this help message'
echo ' -i, --install-dir Set installation directory'
echo " (defaults to directory '$(dirname "$0")')"
echo ''
echo 'Arguments:'
echo ' tag DockerC tag/version to install (defaults to HEAD)'
exit ${ERROR_CODE:-0}
fi
# Print help & exit
echo 'Usage:'
echo " $0 [options] [ <context> [@compose_preset] [...args] ]"
echo " $0 [options] <@docker_preset> [...args]"
echo ''
echo 'Options:'
echo ' -h, --help Print this help message'
echo ' -v, --version Print version'
echo ' --update Update DockerC'
echo ' -f, --force Force, for example ignores abstract contexts'
echo ' -n, --dry-run Dry run, print docker compose command without running it'
echo ' -q, --quiet Quiet, do not print docker compose command'
echo ''
echo 'Arguments:'
echo ' options DockerC options'
echo ' context Docker compose files context'
echo ' @compose_preset Docker compose command preset (shorthand)'
echo ' @docker_preset Docker command preset (shorthand)'
echo ' args Arguments appended to the docker compose command'
echo ''
echo 'Help commands:'
echo " $0 ? See context argument help"
echo " $0 - @ See @compose_preset argument help"
echo " $0 @ See @docker_preset argument help"
exit ${ERROR_CODE:-0}
fi
if [ "$DOCKERC_PRINT_HELP_CONTEXT" = 'true' ]; then
# Print context help & exit
echo 'DockerC context argument values:'
echo ''
echo ' (empty), -'
echo ' Look for default docker compose files'
echo ' Usage:'
echo " $0 [options]"
echo " $0 [options] - [@compose_preset] [...args]"
echo ' Used default docker compose files are:'
echo ' - docker-compose.yml'
echo ' - docker-compose.override.yml (if it exists)'
echo ''
echo ' --'
echo ' Run docker compose without file arguments'
echo ' Usage:'
echo " $0 [options] -- [@compose_preset] [...args]"
echo ''
echo ' level1[.level2[...]]]'
echo ' Look for docker compose files matching context levels'
echo ' Usage:'
echo " $0 [options] level1[.level2[...] [@compose_preset] [...args]"
echo ' Example:'
echo " '$0 prod.boosted' will look for:"
echo ' - docker-compose.yml'
echo ' - docker-compose.prod.yml'
echo ' - docker-compose.prod.boosted.yml'
echo ''
echo 'Use dry run option (--dry-run or -n) to see the docker compose command'
echo 'and files that will be used without running it.'
echo ''
echo "Use '$0 --help' for more help."
exit ${ERROR_CODE:-0}
fi
if [ "$DOCKERC_PRINT_HELP_DOCKER_PRESET" = 'true' ]; then
# Print docker preset help & exit
echo "Usage: $0 [options] <@docker_preset> [...args]"
echo ''
echo 'Docker command presets:'
echo ' prune:'
echo ' @rfc Remove unused containers'
echo ' @rfi Remove dangling images'
echo ' @rfia Remove all unused images'
echo ' @rf Remove unused containers, networks and images'
echo ' @rfa Remove all unused containers, networks and images'
echo ' @rfav Remove all unused containers, networks, images and volumes'
echo ' stop:'
echo ' @sa Stop all running containers'
echo ' rm:'
echo ' @rmf Remove running containers, force'
echo ' @rms Remove stopped containers'
echo ' @rmsf Remove stopped containers, force'
echo ' (aliases: @rmfs)'
echo ' @rma Remove all containers'
echo ' @rmaf Remove all containers, force'
echo ' (aliases: @rmfa)'
echo ''
echo 'Arguments will be appended to the docker command that will be run.'
echo ''
echo "Use '$0 --help' for more help."
exit ${ERROR_CODE:-0}
fi
if [ "$DOCKERC_PRINT_HELP_COMPOSE_PRESET" = 'true' ]; then
# Print compose preset help & exit
echo "Usage: $0 [options] <context> [@compose_preset] [...args]"
echo ''
echo 'Compose command presets:'
echo ' up:'
echo ' @u up -d'
echo ' @ua up'
echo ' @ub up --build -d'
echo ' @uba up --build'
echo ' @ubf up --build -d --force-recreate'
echo ' @ubfa up --build --force-recreate'
echo ' @ubfp up --build -d --force-recreate --pull always'
echo ' @ubfap up --build --force-recreate --pull always'
echo ' @ubr up --build -d --remove-orphans'
echo ' @ubar up --build --remove-orphans'
echo ' @ubfr up --build -d --force-recreate --remove-orphans'
echo ' @ubfar up --build --force-recreate --remove-orphans'
echo ' @uf up -d --force-recreate'
echo ' @ufa up --force-recreate'
echo ' @ur up -d --remove-orphans'
echo ' @uar up --remove-orphans'
echo ' @ufr up -d --force-recreate --remove-orphans'
echo ' @ufar up --force-recreate --remove-orphans'
echo ' build:'
echo ' @b build'
echo ' @bp build --pull'
echo ' @bf build --pull --no-cache'
echo ' @bn build --dry-run --pull --no-cache'
echo ' watch:'
echo ' @w watch'
echo ' @wq watch --quiet'
echo ' @ww watch --no-up'
echo ' @wqw watch --quiet --no-up'
echo ' @wn watch --dry-run'
echo ' down:'
echo ' @d down'
echo ' @da down --remove-orphans'
echo ' @dr down --remove-orphans --rmi local'
echo ' @dra down --remove-orphans --rmi all'
echo ' @drav down --remove-orphans --rmi all -v'
echo ' @drv down --remove-orphans -v'
echo ' run:'
echo ' @r run --rm'
echo ' @rw run -w /pwd -v $(pwd):/pwd --rm'
echo ' @rd run -d'
echo ' @rk run'
echo ' @rb run --build --rm'
echo ' @rbw run --build -w /pwd -v $(pwd):/pwd --rm'
echo ' @rbd run --build -d'
echo ' @rbk run --build'
echo ' @ri run -i --rm'
echo ' @rwi run -w /pwd -v $(pwd):/pwd -i --rm'
echo ' @rki run -i'
echo ' @rbi run --build -i --rm'
echo ' @rbwi run --build -w /pwd -v $(pwd):/pwd -i --rm'
echo ' @rbki run --build -i'
echo ' run shells:'
echo ' @sh run -i --rm --entrypoint /bin/sh'
echo ' @shw run -w /pwd -v $(pwd):/pwd -i --rm --entrypoint /bin/sh'
echo ' (aliases: $, @$)'
echo ' @shb run --build -i --rm --entrypoint /bin/sh'
echo ' @shbw run --build -w /pwd -v $(pwd):/pwd -i --rm --entrypoint /bin/sh'
echo ' @bash run -i --rm --entrypoint /bin/bash'
echo ' @bashw run -w /pwd -v $(pwd):/pwd -i --rm --entrypoint /bin/bash'
echo ' @bashb run --build -i --rm --entrypoint /bin/bash'
echo ' @bashbw run --build -w /pwd -v $(pwd):/pwd -i --rm --entrypoint /bin/bash'
echo ' logs:'
echo ' @l logs -n 100'
echo ' @lf logs -n 100 -f'
echo ' @lt logs -n 100 -t'
echo ' @ls logs -n 100 --no-log-prefix'
echo ' @lft logs -n 100 -f -t'
echo ' @lfs logs -n 100 -f --no-log-prefix'
echo ' @lfts logs -n 100 -f -t --no-log-prefix'
echo ' @lts logs -n 100 -t --no-log-prefix'
echo ' @ll logs'
echo ' @llf logs -f'
echo ' @llt logs -t'
echo ' @lls logs --no-log-prefix'
echo ' @llft logs -f -t'
echo ' @llfs logs -f --no-log-prefix'
echo ' @llfts logs -f -t --no-log-prefix'
echo ' @llts logs -t --no-log-prefix'
echo ' ps:'
echo ' @p ps'
echo ' @pa ps -a'
echo ' push:'
echo ' @pu push'
echo ' pull:'
echo ' @pl pull'
echo ' rm:'
echo ' @rs rm --stop'
echo ' @rf rm -f'
echo ' @rfv rm -f -v'
echo ''
echo 'Arguments will be appended to the docker command that will be run.'
echo 'If the preset is empty, the arguments should include the command.'
echo ''
echo "Use '$0 --help' for more help."
exit ${ERROR_CODE:-0}
fi
get_latest_version() {
# Syntax: '"tag_name": "v1.0.0",'
LATEST_VERSION_JSON=$(
curl -fsSL "https://api.github.com/repos/matiboux/dockerc/releases/latest" 2>/dev/null \
| grep -Eo '"tag_name": "(.+?)",'
)
if [ -n "$LATEST_VERSION_JSON" ]; then
# Extract version from matched string
# Remove `"tag_name": "` (13 characters) and `",` (2 characters)
if [ "$(uname -s)" = 'Darwin' ]; then
# MacOS
LATEST_VERSION="${LATEST_VERSION_JSON:13:$((${#LATEST_VERSION_JSON}-13-2))}"
else
# Linux
LATEST_VERSION=$(expr substr "$LATEST_VERSION_JSON" $(expr 1 + 13) $(expr length "$LATEST_VERSION_JSON" - 13 - 2))
fi
echo "$LATEST_VERSION"
fi
}
if [ "$DOCKERC_PRINT_VERSION" = 'true' ]; then
# Print version & exit
if [ "$QUIET" = 'false' ]; then
echo "DockerC (v$VERSION) - https://github.com/matiboux/dockerc"
if [ "$DRY_RUN" = 'false' ]; then
LATEST_VERSION=$(get_latest_version)
if [ -z "$LATEST_VERSION" ]; then
echo 'Notice: Failed to get DockerC latest version.' >&2
elif [ "$VERSION" != "${LATEST_VERSION#v}" ]; then
echo "Notice: DockerC is not up to date, latest version is $LATEST_VERSION!" >&2
fi
fi
else
echo "v$VERSION"
fi
exit ${ERROR_CODE:-0}
fi
if [ "$UPDATE" = 'true' ]; then
# Update DockerC & exit
INSTALL_TAG_ARG=''
if [ -n "$INSTALL_TAG" ]; then
INSTALL_TAG_ARG=" \"$INSTALL_TAG\""
fi
if [ "$QUIET" = 'false' ]; then
# Print install script command
if [ "$DOCKERC_FIRST_NEWLINE" != 'false' ]; then
echo ''
fi
echo "> curl -fsSL \"https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh\" | /bin/sh -s -- --install-dir \"${INSTALL_DIR}\"${INSTALL_TAG_ARG}"
echo ''
fi
if [ "$DRY_RUN" = 'false' ]; then
# Run install script command
exec sh -c "curl -fsSL \"https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh\" | /bin/sh -s -- --install-dir \"${INSTALL_DIR}\"${INSTALL_TAG_ARG}"
fi
exit ${ERROR_CODE:-0}
fi
# Handle docker presets
if [ -n "$DOCKER_PRESET" ]; then
DOCKER_ARGS=''
case "$DOCKER_PRESET" in
# Prune presets
'@rfc' )
DOCKER_ARGS='container prune -f'
;;
'@rfi' )
DOCKER_ARGS='image prune -f'
;;
'@rfia' )
DOCKER_ARGS='image prune -f -a'
;;
'@rf' )
DOCKER_ARGS='system prune -f'
;;
'@rfa' )
DOCKER_ARGS='system prune -f -a'
;;
'@rfav' )
DOCKER_ARGS='system prune -f -a --volumes'
;;
# Stop presets
'@sa' )
# Stop all containers
DOCKER_ARGS='stop $(docker ps -q)'
;;
# Remove presets
'@rmf' )
# Remove running containers, force
DOCKER_ARGS='rm -f $(docker ps -a -q)'
;;
'@rms' )
# Remove stopped containers
DOCKER_ARGS='rm $(docker ps -a -q -f status=exited -f status=dead)'
;;
'@rmsf' | '@rmfs' )
# Remove stopped containers, force
DOCKER_ARGS='rm -f $(docker ps -a -q -f status=exited -f status=dead)'
;;
'@rma' )
# Remove all containers
DOCKER_ARGS='rm $(docker ps -a -q)'
;;
'@rmaf' | '@rmfa' )
# Remove all containers, force
DOCKER_ARGS='rm -f $(docker ps -q)'
;;
esac
if [ -z "$DOCKER_ARGS" ]; then
echo "Error: Unknown docker preset '$DOCKER_PRESET'" >&2
exit 1
fi
if [ "$QUIET" = 'false' ]; then
# Print docker command
if [ "$DOCKERC_FIRST_NEWLINE" != 'false' ]; then
echo ''
fi
echo "> docker ${DOCKER_ARGS}${ARGS}"
echo ''
fi
if [ "$DRY_RUN" = 'false' ]; then
# Run docker command
exec sh -c "docker ${DOCKER_ARGS}${ARGS}"
fi
exit ${ERROR_CODE:-0}
fi
look_env_files() {
# Usage: look_env_files <dir> <base> <part>
# Extract arguments
ARG_DIR="${1:-.}" # eg. '.' or './docker'
ARG_BASE="${2:-}"
ARG_PART="${3:-}" # e.g. '' or 'prod' or 'dev'
# Reformat base argument: ensure leading dot if not empty
if [ -n "$ARG_PART" ]; then
ARG_PART=".${ARG_PART#.}"
fi
ENV_FILE_FOUND='false'
ENV_FILE_PATH="$ARG_DIR/.env$ARG_BASE$ARG_PART"
if [ -f "$ENV_FILE_PATH" ]; then
# Env file found
echo "$ENV_FILE_PATH"
ENV_FILE_FOUND='true'
fi
ENV_FILE_LOCAL_PATH="$ENV_FILE_PATH.local"
if [ -f "$ENV_FILE_LOCAL_PATH" ]; then
# Local env file found
echo "$ENV_FILE_LOCAL_PATH"
ENV_FILE_FOUND='true'
fi
if [ "$ENV_FILE_FOUND" = 'false' ]; then
# No env file found
return 1
fi
return 0
}
look_compose_file() {
# Usage: look_compose_file <dir> <compose_prefix> <base> <part> <extension>
# Extract arguments
ARG_DIR="${1:-.}" # eg. '.' or './docker'
ARG_COMPOSE_PREFIX="${2:-docker-compose}" # eg. 'docker-compose' or 'compose'
ARG_BASE="${3:-}"
ARG_PART="${4:-}" # e.g. '' or 'override' or 'prod'
ARG_EXTENSION="${5:-yml}" # e.g. 'yml' or 'yaml'
# Reformat base argument: ensure leading dot if not empty
if [ -n "$ARG_PART" ]; then
ARG_PART=".${ARG_PART#.}"
fi
COMPOSE_FILE_PATH="${ARG_DIR}/${ARG_COMPOSE_PREFIX}${ARG_BASE}${ARG_PART}.${ARG_EXTENSION}"
if [ ! -f "$COMPOSE_FILE_PATH" ]; then
# Compose file not found
return 1
fi
# Return found compose file path
echo "$COMPOSE_FILE_PATH"
if cat "$COMPOSE_FILE_PATH" | grep -q "^#:dockerc.abstract$"; then
# Compose file is abstract
return 2
fi
return 0
}
COMPOSE_FILE_ARGS=''
ENV_FILE_ARGS=''
ABSTRACT_CONTEXT='false'
# Handle context
# Unless context is '--': in this case, use no compose file argument
if [ "$CONTEXT" != '--' ]; then
CONTEXT_SKIP_ROOT_STANDARD='false'
if [ "$CONTEXT" = '-' ] || [ "$CONTEXT" = '.' ]; then
# Use default context
CONTEXT=''
elif [ "$(echo "$CONTEXT" | cut -c1)" = '-' ]; then
# Only look for standard root level compose files
CONTEXT_SKIP_ROOT_STANDARD='true'
# Remove leading dash
CONTEXT="${CONTEXT#-}"
fi
# Iterate over directories:
# - Current directory '.'
# - Directory './docker'
while read -r COMPOSE_DIR; do
# Iterate over compose file naming schemes:
# - 'docker-compose.yml'
# - 'docker-compose.yaml'
# - 'compose.yml'
# - 'compose.yaml'
while read -r COMPOSE_SCHEME; do
# If $COMPOSE_SCHEME <= 2, use 'docker-compose' compose file prefix name
# Else, use 'compose' compose file prefix name
COMPOSE_PREFIX=$([ "${COMPOSE_SCHEME:-1}" -le 2 ] && echo 'docker-compose' || echo 'compose')
# If $COMPOSE_SCHEME is odd, use 'yml' compose file extension
# Else, use 'yaml' compose file extension
COMPOSE_EXTENSION=$([ "$((${COMPOSE_SCHEME:-1} % 2))" -eq 1 ] && echo 'yml' || echo 'yaml')
LVL1_COMPOSE_FILE_ARGS=''
LVL1_ENV_FILE_ARGS=''
LVL1_ABSTRACT_CONTEXT='false'
# Look for global env files
ENV_LOOK_FILES=$(look_env_files "$COMPOSE_DIR" '' '')
ENV_LOOK_CODE=$?
if [ "$ENV_LOOK_CODE" -eq 0 ]; then
while read -r ENV_LOOK_FILE; do
LVL1_ENV_FILE_ARGS="$LVL1_ENV_FILE_ARGS --env-file $ENV_LOOK_FILE"
done <<-EOF
$(echo "$ENV_LOOK_FILES")
EOF
fi
if [ "$CONTEXT_SKIP_ROOT_STANDARD" != 'true' ]; then
# Check for standard root level compose file
# Look for root compose file
COMPOSE_LOOK_FILE=$(look_compose_file "$COMPOSE_DIR" "$COMPOSE_PREFIX" '' '' "$COMPOSE_EXTENSION")
COMPOSE_LOOK_CODE=$?
if [ "$COMPOSE_LOOK_CODE" -ne 1 ]; then
# Compose file found
LVL1_COMPOSE_FILE_ARGS="$LVL1_COMPOSE_FILE_ARGS -f $COMPOSE_LOOK_FILE"
LVL1_ABSTRACT_CONTEXT=$([ "$COMPOSE_LOOK_CODE" -eq 0 ] && echo 'false' || echo 'true')
fi
fi
# Parse context parts
if [ -n "$CONTEXT" ]; then
if [ -n "$LVL1_COMPOSE_FILE_ARGS" ]; then
# Check for standard root level compose files
LVL2_COMPOSE_FILE_ARGS="$LVL1_COMPOSE_FILE_ARGS"
LVL2_ENV_FILE_ARGS="$LVL1_ENV_FILE_ARGS"
LVL2_ABSTRACT_CONTEXT='false'
LVL2_PREFIX=''
while read -r CONTEXT_PART; do
if [ -z "$CONTEXT_PART" ]; then
# Skip empty parts
continue
fi
LVL2_PREFIX="$LVL2_PREFIX.$CONTEXT_PART"
# Look for universal env files
ENV_LOOK_FILES=$(look_env_files "$COMPOSE_DIR" '.-' "$CONTEXT_PART")
ENV_LOOK_CODE=$?
if [ "$ENV_LOOK_CODE" -eq 0 ]; then
while read -r ENV_LOOK_FILE; do
LVL2_ENV_FILE_ARGS="$LVL2_ENV_FILE_ARGS --env-file $ENV_LOOK_FILE"
done <<-EOF
$(echo "$ENV_LOOK_FILES")
EOF
fi
# Look for context env files
ENV_LOOK_FILES=$(look_env_files "$COMPOSE_DIR" '' "$LVL2_PREFIX")
ENV_LOOK_CODE=$?
if [ "$ENV_LOOK_CODE" -eq 0 ]; then
while read -r ENV_LOOK_FILE; do
LVL2_ENV_FILE_ARGS="$LVL2_ENV_FILE_ARGS --env-file $ENV_LOOK_FILE"
done <<-EOF
$(echo "$ENV_LOOK_FILES")
EOF
fi
# Look for context compose file
COMPOSE_LOOK_FILE=$(look_compose_file "$COMPOSE_DIR" "$COMPOSE_PREFIX" '' "$LVL2_PREFIX" "$COMPOSE_EXTENSION")
COMPOSE_LOOK_CODE=$?
if [ "$COMPOSE_LOOK_CODE" -ne 1 ]; then
# Context compose file found
LVL2_COMPOSE_FILE_ARGS="$LVL2_COMPOSE_FILE_ARGS -f $COMPOSE_LOOK_FILE"
LVL2_ABSTRACT_CONTEXT=$([ "$COMPOSE_LOOK_CODE" -eq 0 ] && echo 'false' || echo 'true')
else
# Context compose file not found: invalidate LVL2 and break loop
LVL2_COMPOSE_FILE_ARGS=''
break
fi
done <<-EOF
$(echo "$CONTEXT" | tr '.' "\n")
EOF
if [ -n "$LVL2_COMPOSE_FILE_ARGS" ]; then
# Context resolved: break loop
break
fi
fi
LVL2_COMPOSE_FILE_ARGS=''
LVL2_ENV_FILE_ARGS="$LVL1_ENV_FILE_ARGS"
LVL2_ABSTRACT_CONTEXT='false'
# Check for non-standard root level compose files
# Extract first context part
CONTEXT_FIRST="${CONTEXT%%.*}"
CONTEXT_REST="$([ "$CONTEXT" = "$CONTEXT_FIRST" ] && echo '' || echo "${CONTEXT#*.}")"
# Look for universal env files
ENV_LOOK_FILES=$(look_env_files "$COMPOSE_DIR" '.-' "$CONTEXT_FIRST")
ENV_LOOK_CODE=$?
if [ "$ENV_LOOK_CODE" -eq 0 ]; then
while read -r ENV_LOOK_FILE; do
LVL2_ENV_FILE_ARGS="$LVL2_ENV_FILE_ARGS --env-file $ENV_LOOK_FILE"
done <<-EOF
$(echo "$ENV_LOOK_FILES")
EOF
fi
# Look for context env files
ENV_LOOK_FILES=$(look_env_files "$COMPOSE_DIR" '' "$CONTEXT_FIRST")
ENV_LOOK_CODE=$?
if [ "$ENV_LOOK_CODE" -eq 0 ]; then
while read -r ENV_LOOK_FILE; do
LVL2_ENV_FILE_ARGS="$LVL2_ENV_FILE_ARGS --env-file $ENV_LOOK_FILE"
done <<-EOF
$(echo "$ENV_LOOK_FILES")
EOF
fi
# Look for root compose file
COMPOSE_LOOK_FILE=$(look_compose_file "$COMPOSE_DIR" "$COMPOSE_PREFIX" "-$CONTEXT_FIRST" '' "$COMPOSE_EXTENSION")
COMPOSE_LOOK_CODE=$?
if [ "$COMPOSE_LOOK_CODE" -ne 1 ]; then
# Compose file found
LVL2_COMPOSE_FILE_ARGS="$LVL2_COMPOSE_FILE_ARGS -f $COMPOSE_LOOK_FILE"
LVL2_ABSTRACT_CONTEXT=$([ "$COMPOSE_LOOK_CODE" -eq 0 ] && echo 'false' || echo 'true')
fi
# Parse context parts
if [ -n "$CONTEXT_REST" ]; then
LVL2_PREFIX=''
while read -r CONTEXT_PART; do
if [ -z "$CONTEXT_PART" ]; then
# Skip empty parts
continue
fi
LVL2_PREFIX="$LVL2_PREFIX.$CONTEXT_PART"
# Look for universal env files
ENV_LOOK_FILES=$(look_env_files "$COMPOSE_DIR" '.-' "$CONTEXT_PART")
ENV_LOOK_CODE=$?
if [ "$ENV_LOOK_CODE" -eq 0 ]; then
while read -r ENV_LOOK_FILE; do
LVL2_ENV_FILE_ARGS="$LVL2_ENV_FILE_ARGS --env-file $ENV_LOOK_FILE"
done <<-EOF
$(echo "$ENV_LOOK_FILES")
EOF
fi
# Look for context env files
ENV_LOOK_FILES=$(look_env_files "$COMPOSE_DIR" '' "$LVL2_PREFIX")
ENV_LOOK_CODE=$?
if [ "$ENV_LOOK_CODE" -eq 0 ]; then
while read -r ENV_LOOK_FILE; do
LVL2_ENV_FILE_ARGS="$LVL2_ENV_FILE_ARGS --env-file $ENV_LOOK_FILE"
done <<-EOF
$(echo "$ENV_LOOK_FILES")
EOF
fi
# Look for context compose file
COMPOSE_LOOK_FILE=$(look_compose_file "$COMPOSE_DIR" "$COMPOSE_PREFIX" "-$CONTEXT_FIRST" "$LVL2_PREFIX" "$COMPOSE_EXTENSION")
COMPOSE_LOOK_CODE=$?
if [ "$COMPOSE_LOOK_CODE" -ne 1 ]; then
# Context compose file found
LVL2_COMPOSE_FILE_ARGS="$LVL2_COMPOSE_FILE_ARGS -f $COMPOSE_LOOK_FILE"
LVL2_ABSTRACT_CONTEXT=$([ "$COMPOSE_LOOK_CODE" -eq 0 ] && echo 'false' || echo 'true')
else
# Context compose file not found: invalidate LVL2 and break loop
LVL2_COMPOSE_FILE_ARGS=''
break
fi
done <<-EOF
$(echo "$CONTEXT_REST" | tr '.' "\n")
EOF
if [ -n "$LVL2_COMPOSE_FILE_ARGS" ]; then
# Context resolved: break loop
break
fi
fi
fi
if [ -n "$LVL2_COMPOSE_FILE_ARGS" ]; then
# Context resolved: break loop
break
fi
if [ -n "$LVL1_COMPOSE_FILE_ARGS" ]; then
# Check for special contexts
if [ -z "$CONTEXT" ] || [ "$CONTEXT" = 'dev' ]; then
# Look for standard override compose file
COMPOSE_LOOK_FILE=$(look_compose_file "$COMPOSE_DIR" "$COMPOSE_PREFIX" '' 'override' "$COMPOSE_EXTENSION")
COMPOSE_LOOK_CODE=$?
if [ "$COMPOSE_LOOK_CODE" -ne 1 ]; then
# Standard override compose file found
LVL1_COMPOSE_FILE_ARGS="$LVL1_COMPOSE_FILE_ARGS -f $COMPOSE_LOOK_FILE"
LVL1_ABSTRACT_CONTEXT=$([ "$COMPOSE_LOOK_CODE" -eq 0 ] && echo 'false' || echo 'true')
break
elif [ "$CONTEXT" = 'dev' ]; then
# Standard override compose file not found, but was required: invalidate LVL1 & break loop
LVL1_COMPOSE_FILE_ARGS=''
break
fi
elif [ "$CONTEXT" != 'prod' ]; then
# Context not resolved: invalidate LVL1 & break loop
LVL1_COMPOSE_FILE_ARGS=''
break
fi
if [ -n "$LVL1_COMPOSE_FILE_ARGS" ]; then
# Context resolved: break loop
break
fi
fi
# End of while loop over compose file naming schemes
done <<-EOF
1
2
3
4
EOF
if [ -n "$LVL2_COMPOSE_FILE_ARGS" ]; then
# Context resolved: use LVL2 arguments & break loop
COMPOSE_FILE_ARGS="$COMPOSE_FILE_ARGS $LVL2_COMPOSE_FILE_ARGS"
ENV_FILE_ARGS="$ENV_FILE_ARGS $LVL2_ENV_FILE_ARGS"
ABSTRACT_CONTEXT="$LVL2_ABSTRACT_CONTEXT"
break
elif [ -n "$LVL1_COMPOSE_FILE_ARGS" ]; then
# Context resolved: use LVL1 arguments & break loop
COMPOSE_FILE_ARGS="$COMPOSE_FILE_ARGS $LVL1_COMPOSE_FILE_ARGS"
ENV_FILE_ARGS="$ENV_FILE_ARGS $LVL1_ENV_FILE_ARGS"
ABSTRACT_CONTEXT="$LVL1_ABSTRACT_CONTEXT"