This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker.mk
871 lines (802 loc) · 45.9 KB
/
docker.mk
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
DOCKER_COMPOSE_YML = $(DOCKER_DIR)/docker-compose.yml
DOCKER_DIR = $(abspath $(PROJECT_DIR)/build/docker)
DOCKER_DIR_REL = $(shell echo $(DOCKER_DIR) | sed "s;$(PROJECT_DIR);;g")
DOCKER_LIB_DIR = $(LIB_DIR)/docker
DOCKER_LIB_DIR_REL = $(shell echo $(DOCKER_LIB_DIR) | sed "s;$(PROJECT_DIR);;g")
DOCKER_LIB_IMAGE_DIR = $(LIB_DIR)/docker/image
DOCKER_LIB_IMAGE_DIR_REL = $(shell echo $(DOCKER_LIB_IMAGE_DIR) | sed "s;$(PROJECT_DIR);;g")
DOCKER_NETWORK = $(PROJECT_GROUP_SHORT)/$(PROJECT_NAME_SHORT)/$(BUILD_ID)
DOCKER_REGISTRY = $(AWS_ECR)/$(PROJECT_GROUP_SHORT)/$(PROJECT_NAME_SHORT)
DOCKER_LIBRARY_REGISTRY = nhsd
DOCKER_ALPINE_VERSION = 3.16.0
DOCKER_COMPOSER_VERSION = 2.3.7
DOCKER_CONFIG_LINT_VERSION = v1.6.0
DOCKER_DIND_VERSION = 20.10.17-dind
DOCKER_EDITORCONFIG_CHECKER_VERSION = 2.4.0
DOCKER_ELASTICSEARCH_VERSION = 8.2.2
DOCKER_GRADLE_VERSION = 7.2.0-jdk$(JAVA_VERSION)
DOCKER_LOCALSTACK_VERSION = $(LOCALSTACK_VERSION)
DOCKER_MAVEN_VERSION = 3.8.3-openjdk-$(JAVA_VERSION)-slim
DOCKER_NGINX_VERSION = 1.22.0-alpine
DOCKER_NODE_VERSION = $(NODE_VERSION)-alpine
DOCKER_OPENJDK_VERSION = $(JAVA_VERSION)-alpine
DOCKER_POSTGRES_VERSION = $(POSTGRES_VERSION)-alpine
DOCKER_POSTMAN_NEWMAN_VERSION = $(POSTMAN_NEWMAN_VERSION)-alpine
DOCKER_PYTHON_VERSION = $(PYTHON_VERSION)-alpine
DOCKER_SONAR_SCANNER_CLI_VERSION = $(SONAR_SCANNER_CLI_VERSION)
DOCKER_CHECKOV_VERSION = 2.0.1222
DOCKER_TERRAFORM_COMPLIANCE_VERSION = 1.3.33
DOCKER_TERRAFORM_TFSEC_VERSION = v1.26.0
DOCKER_TERRAFORM_VERSION = $(TERRAFORM_VERSION)
DOCKER_WIREMOCK_VERSION = $(WIREMOCK_VERSION)-alpine
DOCKER_LIBRARY_ELASTICSEARCH_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/elasticsearch/VERSION 2> /dev/null)
DOCKER_LIBRARY_NGINX_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/nginx/VERSION 2> /dev/null)
DOCKER_LIBRARY_NODE_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/node/VERSION 2> /dev/null)
DOCKER_LIBRARY_PIPELINE_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/pipeline/VERSION 2> /dev/null)
DOCKER_LIBRARY_POSTGRES_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/postgres/VERSION 2> /dev/null)
DOCKER_LIBRARY_PYTHON_APP_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/python-app/VERSION 2> /dev/null)
DOCKER_LIBRARY_PYTHON_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/python/VERSION 2> /dev/null)
DOCKER_LIBRARY_TOOLS_VERSION = $(shell cat $(DOCKER_LIB_IMAGE_DIR)/tools/VERSION 2> /dev/null)
COMPOSE_HTTP_TIMEOUT := $(or $(COMPOSE_HTTP_TIMEOUT), 6000)
DOCKER_CLIENT_TIMEOUT := $(or $(DOCKER_CLIENT_TIMEOUT), 6000)
# ==============================================================================
docker-create-from-template: ### Create Docker image from template - mandatory: NAME,TEMPLATE=[library template image name]
mkdir -p $(DOCKER_DIR)
if [ ! -f $(DOCKER_DIR)/docker-compose.yml ]; then
cp -rfv \
build/automation/lib/project/template/build/docker/docker-compose.yml \
$(DOCKER_DIR_REL)
fi
rm -rf $(DOCKER_DIR)/$(NAME)
cp -rfv $(DOCKER_LIB_DIR)/template/$(TEMPLATE) $(DOCKER_DIR)/$(NAME)
find $(DOCKER_DIR)/$(NAME) -type f -name '.gitkeep' -print | xargs rm -fv
# Replace template values
export SUFFIX=_TEMPLATE_TO_REPLACE
export VERSION=$$(make docker-image-get-version NAME=$(TEMPLATE))
make -s file-replace-variables-in-dir DIR=$(DOCKER_DIR)/$(NAME)
make -s file-replace-variables FILE=$(DOCKER_DIR)/docker-compose.yml
make -s file-replace-variables FILE=Makefile
# ==============================================================================
docker-config: ### Configure Docker networking
docker network create $(DOCKER_NETWORK) 2> /dev/null ||:
docker-build docker-image: ### Build Docker image - mandatory: NAME; optional: VERSION,FROM_CACHE=true,BUILD_OPTS=[build options],EXAMPLE=true
reg=$$(make _docker-get-reg)
# Try to execute `make build` from the image directory
if [ -d $(DOCKER_LIB_IMAGE_DIR)/$(NAME) ] && [ -z "$(__DOCKER_BUILD)" ]; then
cd $(DOCKER_LIB_IMAGE_DIR)/$(NAME)
make build __DOCKER_BUILD=true DOCKER_REGISTRY=$(DOCKER_LIBRARY_REGISTRY)
exit
elif [ -d $(DOCKER_CUSTOM_DIR)/$(NAME) ] && [ -z "$(__DOCKER_BUILD)" ]; then
cd $(DOCKER_CUSTOM_DIR)/$(NAME)
make build __DOCKER_BUILD=true && exit || cd $(PROJECT_DIR)
elif [ -d $(DOCKER_DIR)/$(NAME) ] && [ -z "$(__DOCKER_BUILD)" ]; then
cd $(DOCKER_DIR)/$(NAME)
make build __DOCKER_BUILD=true && exit || cd $(PROJECT_DIR)
fi
# Dockerfile
make NAME=$(NAME) \
docker-create-dockerfile FILE=Dockerfile$(shell [ -n "$(EXAMPLE)" ] && echo .example) \
docker-image-set-version VERSION=$(VERSION)
# Cache
cache_from=
if [[ "$(FROM_CACHE)" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$$ ]]; then
make docker-pull NAME=$(NAME) VERSION=latest
cache_from="--cache-from $$reg/$(NAME):latest"
fi
# Build
dir=$$(make _docker-get-dir)
export IMAGE=$$reg/$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example)
export VERSION=$$(make docker-image-get-version)
make -s file-replace-variables FILE=$$dir/Dockerfile.effective
docker build --rm \
--build-arg IMAGE=$$IMAGE \
--build-arg VERSION=$$VERSION \
--build-arg BUILD_ID=$(BUILD_ID) \
--build-arg BUILD_DATE=$(BUILD_DATE) \
--build-arg BUILD_REPO=$(BUILD_REPO) \
--build-arg BUILD_BRANCH=$(BUILD_BRANCH) \
--build-arg BUILD_COMMIT_HASH=$(BUILD_COMMIT_HASH) \
--build-arg BUILD_COMMIT_DATE=$(BUILD_COMMIT_DATE) \
--label name=$$IMAGE \
--label version=$$VERSION \
--label build-id=$(BUILD_ID) \
--label build-date=$(BUILD_DATE) \
--label build-repo=$(BUILD_REPO) \
--label build-branch=$(BUILD_BRANCH) \
--label build-commit-hash=$(BUILD_COMMIT_HASH) \
--label build-commit-date=$(BUILD_COMMIT_DATE) \
$(BUILD_OPTS) $$cache_from \
--file $$dir/Dockerfile.effective \
--tag $$reg/$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example):$$(make docker-image-get-version) \
$$dir
# Tag
docker tag \
$$reg/$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example):$$(make docker-image-get-version) \
$$reg/$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example):latest
docker rmi --force $$(docker images | grep "<none>" | awk '{ print $$3 }') 2> /dev/null ||:
make docker-image-keep-latest-only NAME=$(NAME)
docker image inspect $$reg/$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example):latest --format='{{.Size}}'
docker-test: ### Test image - mandatory: NAME; optional: ARGS,CMD,GOSS_OPTS,EXAMPLE=true
dir=$$(make _docker-get-dir)
reg=$$(make _docker-get-reg)
GOSS_FILES_PATH=$$dir/test \
GOSS_FILE=$(shell [ -z "$(EXAMPLE)" ] && echo goss.yaml || echo goss-example.yaml) \
CONTAINER_LOG_OUTPUT=$(TMP_DIR)/container-$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example)-$(BUILD_COMMIT_HASH)-$(BUILD_ID).log \
$(GOSS_OPTS) \
dgoss run --interactive $(_TTY) \
$(ARGS) \
$$reg/$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example):latest \
$(CMD)
docker-login: ### Log into the Docker registry - optional: DOCKER_USERNAME,DOCKER_PASSWORD
if [ -n "$(DOCKER_USERNAME)" ] && [ -n "$$(make _docker-get-login-password)" ]; then
make _docker-get-login-password | docker login --username "$(DOCKER_USERNAME)" --password-stdin
else
make aws-ecr-get-login-password | docker login --username AWS --password-stdin $(AWS_ECR)
fi
docker-create-repository: ### Create Docker repository to store an image - mandatory: NAME; optional: POLICY_FILE=[policy file]
make aws-ecr-create-repository NAME=$(NAME) POLICY_FILE=$(POLICY_FILE)
docker-push: ### Push Docker image - mandatory: NAME; optional: VERSION|TAG
make docker-login
reg=$$(make _docker-get-reg)
if [ -n "$(or $(VERSION), $(TAG))" ]; then
docker push $$reg/$(NAME):$(or $(VERSION), $(TAG))
else
docker push $$reg/$(NAME):$$(make docker-image-get-version)
fi
docker push $$reg/$(NAME):latest 2> /dev/null ||:
docker-pull: ### Pull Docker image - mandatory: NAME,DIGEST|VERSION|TAG
[ $$(make _docker-is-lib-image) == false ] && make docker-login
reg=$$(make _docker-get-reg)
if [ -n "$(DIGEST)" ]; then
docker pull $$reg/$(NAME)@$(DIGEST) ||:
else
docker pull $$reg/$(NAME):$(or $(VERSION), $(TAG)) ||:
fi
docker-tag: ### Tag latest or provide arguments - mandatory: NAME,VERSION|TAG|[SOURCE,TARGET]|[DIGEST,VERSION|TAG]
reg=$$(make _docker-get-reg)
if [ -n "$(DIGEST)" ] && [ -n "$(TAG)" ]; then
docker tag \
$$reg/$(NAME)@$(DIGEST) \
$$reg/$(NAME):$(or $(VERSION), $(TAG))
elif [ -n "$(SOURCE)" ] && [ -n "$(TARGET)" ]; then
docker tag \
$$reg/$(NAME):$(SOURCE) \
$$reg/$(NAME):$(TARGET)
elif [ -n "$(or $(VERSION), $(TAG))" ]; then
docker tag \
$$reg/$(NAME):latest \
$$reg/$(NAME):$(or $(VERSION), $(TAG))
fi
docker-rename: ### Rename Docker image - mandatory: NAME,AS
reg=$$(make _docker-get-reg)
docker tag \
$$reg/$(NAME):$$(make docker-image-get-version) \
$$reg/$(AS):$$(make docker-image-get-version)
docker tag \
$$reg/$(NAME):latest \
$$reg/$(AS):latest
make docker-image-keep-latest-only NAME=$(AS)
docker-clean: ### Clean Docker files
find $(DOCKER_DIR) -type f -name '.version' -print0 | xargs -0 rm -v 2> /dev/null ||:
find $(DOCKER_DIR) -type f -name 'Dockerfile.effective' -print0 | xargs -0 rm -v 2> /dev/null ||:
find $(DOCKER_LIB_IMAGE_DIR) -type f -name '.version' -print0 | xargs -0 rm -v 2> /dev/null ||:
find $(DOCKER_LIB_IMAGE_DIR) -type f -name 'Dockerfile.effective' -print0 | xargs -0 rm -v 2> /dev/null ||:
docker-prune: docker-clean ### Clean Docker resources - optional: ALL=true
docker rmi --force $$(docker images | grep $(DOCKER_REGISTRY) | awk '{ print $$3 }') 2> /dev/null ||:
docker rmi --force $$(docker images | grep $(DOCKER_LIBRARY_REGISTRY) | awk '{ print $$3 }') 2> /dev/null ||:
docker network rm $(DOCKER_NETWORK) 2> /dev/null ||:
[[ "$(ALL)" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$$ ]] && docker system prune --volumes --all --force ||:
# ==============================================================================
docker-create-dockerfile: ### Create effective Dockerfile - mandatory: NAME; optional FILE=[Dockerfile name, defaults to Dockerfile]
dir=$$(pwd)
cd $$(make _docker-get-dir)
cat $(or $(FILE), Dockerfile) $(DOCKER_LIB_DIR)/image/Dockerfile.metadata > Dockerfile.effective
sed -i " \
s#DOCKER_REGISTRY#$(DOCKER_REGISTRY)#g; \
s#AWS_ECR#$(AWS_ECR)#g; \
s#AWS_ACCOUNT_ID_MGMT#$(AWS_ACCOUNT_ID_MGMT)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/elasticsearch:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/elasticsearch:$(DOCKER_LIBRARY_ELASTICSEARCH_VERSION)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/nginx:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/nginx:$(DOCKER_LIBRARY_NGINX_VERSION)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/node:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/node:$(DOCKER_LIBRARY_NODE_VERSION)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/pipeline:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/pipeline:$(DOCKER_LIBRARY_PIPELINE_VERSION)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/postgres:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/postgres:$(DOCKER_LIBRARY_POSTGRES_VERSION)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/python-app:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/python-app:$(DOCKER_LIBRARY_PYTHON_APP_VERSION)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/python:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/python:$(DOCKER_LIBRARY_PYTHON_VERSION)#g; \
s#FROM $(DOCKER_LIBRARY_REGISTRY)/tools:latest#FROM $(DOCKER_LIBRARY_REGISTRY)/tools:$(DOCKER_LIBRARY_TOOLS_VERSION)#g; \
s#FROM alpine:latest#FROM alpine:$(DOCKER_ALPINE_VERSION)#g; \
s#FROM bitnami/elasticsearch:latest#FROM bitnami/elasticsearch:$(DOCKER_ELASTICSEARCH_VERSION)#g; \
s#FROM docker:latest#FROM docker:$(DOCKER_DIND_VERSION)#g; \
s#FROM gradle:latest#FROM gradle:$(DOCKER_GRADLE_VERSION)#g; \
s#FROM maven:latest#FROM maven:$(DOCKER_MAVEN_VERSION)#g; \
s#FROM nginx:latest#FROM nginx:$(DOCKER_NGINX_VERSION)#g; \
s#FROM node:latest#FROM node:$(DOCKER_NODE_VERSION)#g; \
s#FROM openjdk:latest#FROM openjdk:$(DOCKER_OPENJDK_VERSION)#g; \
s#FROM postgres:latest#FROM postgres:$(DOCKER_POSTGRES_VERSION)#g; \
s#FROM postman/newman:latest#FROM postman/newman:$(DOCKER_POSTMAN_NEWMAN_VERSION)#g; \
s#FROM python:latest#FROM python:$(DOCKER_PYTHON_VERSION)#g; \
s#FROM wiremock/wiremock:latest#FROM wiremock/wiremock:$(DOCKER_WIREMOCK_VERSION)#g; \
" Dockerfile.effective
cd $$dir
docker-image-get-version: ### Get effective Docker image version - mandatory: NAME
dir=$$(make _docker-get-dir)
cat $$dir/.version 2> /dev/null || cat $$dir/VERSION 2> /dev/null || echo unknown
docker-image-set-version: ### Set effective Docker image version - mandatory: NAME; optional: VERSION
if [ -d $(DOCKER_LIB_IMAGE_DIR)/$(NAME) ] && [ -z "$(DOCKER_CUSTOM_DIR)" ]; then
rm -f $(DOCKER_LIB_IMAGE_DIR)/$(NAME)/.version
exit
fi
dir=$$(make _docker-get-dir)
if [ -n "$(VERSION)" ]; then
echo $(VERSION) > $$dir/.version
else
echo $$(cat $$dir/VERSION) | \
sed "s/YYYY/$$(date --date=$(BUILD_DATE) -u +"%Y")/g" | \
sed "s/mm/$$(date --date=$(BUILD_DATE) -u +"%m")/g" | \
sed "s/dd/$$(date --date=$(BUILD_DATE) -u +"%d")/g" | \
sed "s/HH/$$(date --date=$(BUILD_DATE) -u +"%H")/g" | \
sed "s/MM/$$(date --date=$(BUILD_DATE) -u +"%M")/g" | \
sed "s/ss/$$(date --date=$(BUILD_DATE) -u +"%S")/g" | \
sed "s/SS/$$(date --date=$(BUILD_DATE) -u +"%S")/g" | \
sed "s/hash/$$(git rev-parse --short HEAD)/g" \
> $$dir/.version
fi
# ==============================================================================
docker-image-pull-or-build: ### Pull or build image - mandatory: NAME; optional VERSION|TAG=[defaults to 'latest'],LATEST=true
version=$(or $(or $(VERSION), $(TAG)), latest)
image=$(DOCKER_LIBRARY_REGISTRY)/$(NAME):$$version
if [ -z "$$(docker images --filter=reference="$$image" --quiet)" ]; then
make docker-pull NAME=$(NAME) VERSION=$$version ||:
fi
if [ -z "$$(docker images --filter=reference="$$image" --quiet)" ]; then
make docker-build NAME=$(NAME) || ( \
echo "ERROR: No image $$image found"; \
exit 1 \
)
fi
if [ -n "$$(docker images --filter=reference="$$image" --quiet)" ]; then
[[ "$(LATEST)" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$$ ]] && \
make docker-tag NAME=$(NAME) SOURCE=$$version TARGET=latest ||:
fi
docker-image-keep-latest-only: ### Remove other images than latest - mandatory: NAME
reg=$$(make _docker-get-reg)
docker rmi --force $$( \
docker images --filter=reference="$$reg/$(NAME):*" --quiet | \
grep -v $$(docker images --filter=reference="$$reg/$(NAME):latest" --quiet) \
) 2> /dev/null ||:
docker-image-start: ### Start container - mandatory: NAME; optional: CMD,DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],EXAMPLE=true
reg=$$(make _docker-get-reg)
docker run --interactive $(_TTY) $$(echo $(ARGS) | grep -- "--attach" > /dev/null 2>&1 && : || echo "--detach") \
--name $(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example)-$(BUILD_COMMIT_HASH)-$(BUILD_ID) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$$(echo $(ARGS) | sed -e "s/--attach//g") \
$$reg/$(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example):latest \
$(CMD)
docker-image-stop: ### Stop container - mandatory: NAME; optional: EXAMPLE=true
docker stop $(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example)-$(BUILD_COMMIT_HASH)-$(BUILD_ID) 2> /dev/null ||:
docker rm --force --volumes $(NAME)-$(BUILD_COMMIT_HASH)-$(BUILD_ID) 2> /dev/null ||:
docker-image-log: ### Log output of a container - mandatory: NAME; optional: EXAMPLE=true
docker logs --follow $(NAME)$(shell [ -n "$(EXAMPLE)" ] && echo -example)-$(BUILD_COMMIT_HASH)-$(BUILD_ID)
docker-image-bash: ### Bash into a container - mandatory: NAME
docker exec --interactive $(_TTY) --user root \
$(NAME)-$(BUILD_COMMIT_HASH)-$(BUILD_ID) \
bash --login || \
docker exec --interactive $(_TTY) --user root \
$(NAME)-$(BUILD_COMMIT_HASH)-$(BUILD_ID) \
sh --login ||:
docker-image-clean: docker-image-stop ### Clean up container and image resources - mandatory: NAME
dir=$$(make _docker-get-dir)
reg=$$(make _docker-get-reg)
docker rmi --force $$(docker images --filter=reference="$$reg/$(NAME)-example:*" --quiet) 2> /dev/null ||:
docker rmi --force $$(docker images --filter=reference="$$reg/$(NAME):*" --quiet) 2> /dev/null ||:
rm -fv \
$$dir/.version \
$$dir/$(NAME)-*-image.tar.gz \
$$dir/Dockerfile.effective
docker-image-save: ### Save image as a flat file - mandatory: NAME; optional: VERSION|TAG
dir=$$(make _docker-get-dir)
reg=$$(make _docker-get-reg)
version=$(or $(VERSION), $(TAG))
if [ -z "$$version" ]; then
version=$$(make docker-image-get-version)
fi
docker save $$reg/$(NAME):$$version | gzip > $$dir/$(NAME)-$$version-image.tar.gz
docker-image-load: ### Load image from a flat file - mandatory: NAME; optional: VERSION|TAG
dir=$$(make _docker-get-dir)
version=$(or $(VERSION), $(TAG))
if [ -z "$$version" ]; then
version=$$(make docker-image-get-version)
fi
gunzip -c $$dir/$(NAME)-$$version-image.tar.gz | docker load
# ==============================================================================
docker-run: ### Run specified image - mandatory: IMAGE; optional: CMD,SH=true,DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo $$(echo '$(IMAGE)' | md5sum | cut -c1-7)-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
if [[ ! "$(SH)" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$$ ]]; then
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$(IMAGE) \
$(CMD)
else
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$(IMAGE) \
/bin/sh -c " \
$(CMD) \
"
fi
docker-run-composer: ### Run composer container - mandatory: CMD; optional: DIR,ARGS=[Docker args],LIB_VOLUME_MOUNT=true,VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
mkdir -p $(TMP_DIR)/.composer
lib_volume_mount=$$(([ $(BUILD_ID) -eq 0 ] || [ "$(LIB_VOLUME_MOUNT)" == true ]) && echo "--volume $(TMP_DIR)/.composer:/tmp" ||:)
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo composer:$(DOCKER_COMPOSER_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo composer-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
docker-run-editorconfig: ### Run editorconfig container - optional: DIR=[working directory],EXCLUDE=[file pattern e.g. '\.txt$$'],ARGS=[Docker args],VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
if [ $(PROJECT_NAME) = $(DEVOPS_PROJECT_NAME) ]; then
exclude='$(shell [ -n "$(EXCLUDE)" ] && echo '$(EXCLUDE)|')markdown|linux-amd64$$|\.drawio|\.p12|\.jks|\.so$$'
else
exclude='$(shell [ -n "$(EXCLUDE)" ] && echo '$(EXCLUDE)|')build/automation|markdown|linux-amd64$$|\.drawio|\.p12|\.jks|\.so$$'
fi
make docker-config > /dev/null 2>&1
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo mstruebing/editorconfig-checker:$(DOCKER_EDITORCONFIG_CHECKER_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo editorconfig-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--volume $$([ -n "$(DIR)" ] && echo $(abspath $(DIR)) || echo $(PWD)):/check \
--network $(DOCKER_NETWORK) \
--workdir /check \
$(ARGS) \
$$image \
ec --exclude $$exclude
docker-run-gradle: ### Run gradle container - mandatory: CMD; optional: DIR,ARGS=[Docker args],LIB_VOLUME_MOUNT=true,VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
mkdir -p $(TMP_DIR)/.gradle
lib_volume_mount=$$(([ $(BUILD_ID) -eq 0 ] || [ "$(LIB_VOLUME_MOUNT)" == true ]) && echo "--volume $(TMP_DIR)/.gradle:/home/gradle/.gradle" ||:)
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo gradle:$(DOCKER_GRADLE_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo gradle-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--env GRADLE_USER_HOME=/home/gradle/.gradle \
--volume $(PROJECT_DIR):/project \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
docker-run-mvn: ### Run maven container - mandatory: CMD; optional: DIR,ARGS=[Docker args],LIB_VOLUME_MOUNT=true,VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
mkdir -p $(TMP_DIR)/.m2
lib_volume_mount=$$(([ $(BUILD_ID) -eq 0 ] || [ "$(LIB_VOLUME_MOUNT)" == true ]) && echo "--volume $(TMP_DIR)/.m2:/tmp/.m2" ||:)
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo maven:$(DOCKER_MAVEN_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo mvn-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
keystore=
if [ -f $(ETC_DIR)/keystore.jks ]; then
keystore=" \
-Djavax.net.ssl.trustStore=/project/$(ETC_DIR_REL)/keystore.jks \
-Djavax.net.ssl.trustStorePassword=changeit \
"
fi
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--env MAVEN_CONFIG=/tmp/.m2 \
--volume $(PROJECT_DIR):/project \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
/bin/sh -c " \
mvn -Duser.home=/tmp $$keystore $(CMD) \
"
docker-run-node: ### Run node container - mandatory: CMD; optional: DIR,ARGS=[Docker args],LIB_VOLUME_MOUNT=true,VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
mkdir -p $(TMP_DIR)/.cache
touch $(TMP_DIR)/.npmrc
touch $(TMP_DIR)/.yarnrc
lib_volume_mount=$$(([ $(BUILD_ID) -eq 0 ] || [ "$(LIB_VOLUME_MOUNT)" == true ]) && echo "--volume $(TMP_DIR)/.cache:/home/default/.cache" ||:)
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo node:$(DOCKER_NODE_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo node-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--volume $(TMP_DIR)/.npmrc:/home/default/.npmrc \
--volume $(TMP_DIR)/.yarnrc:/home/default/.yarnrc \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
/bin/sh -c " \
groupadd default -g $$(id -g) 2> /dev/null ||: && \
useradd default -u $$(id -u) -g $$(id -g) 2> /dev/null ||: && \
chown $$(id -u):$$(id -g) /home/\$$(id -nu $$(id -u)) ||: && \
su \$$(id -nu $$(id -u)) -c 'cd /project/$(DIR); $(CMD)' \
"
docker-run-postman: ### Run postman (newman) container - mandatory: DIR,CMD
make docker-config > /dev/null 2>&1
make docker-run IMAGE=postman/newman:$(DOCKER_POSTMAN_NEWMAN_VERSION) \
ARGS="--volume $(DIR):/etc/newman" \
DIR="$(DIR)" \
CMD="$(CMD)"
docker-run-python: ### Run python container - mandatory: CMD; optional: SH=true,DIR,ARGS=[Docker args],LIB_VOLUME_MOUNT=true,VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
mkdir -p $(TMP_DIR)/.python/pip/{cache,packages}
lib_volume_mount=$$(([ $(BUILD_ID) -eq 0 ] || [ "$(LIB_VOLUME_MOUNT)" == true ]) && echo "--volume $(TMP_DIR)/.python/pip/cache:/tmp/.cache/pip --volume $(TMP_DIR)/.python/pip/packages:/tmp/.packages" ||:)
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo python:$(DOCKER_PYTHON_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo python-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
if [[ ! "$(SH)" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$$ ]]; then
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--env PIP_TARGET=/tmp/.packages \
--env PYTHONPATH=/tmp/.packages \
--env XDG_CACHE_HOME=/tmp/.cache \
--volume $(PROJECT_DIR):/project \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
else
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--env PIP_TARGET=/tmp/.packages \
--env PYTHONPATH=/tmp/.packages \
--env XDG_CACHE_HOME=/tmp/.cache \
--volume $(PROJECT_DIR):/project \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
/bin/sh -c " \
$(CMD) \
"
fi
docker-run-sonar-scanner-cli: ### Run sonar-scanner-cli container - mandatory: CMD; optional: SH=true,DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
mkdir -p $(TMP_DIR)/.sonar/cache
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo sonarsource/sonar-scanner-cli:$(DOCKER_SONAR_SCANNER_CLI_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo node-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/usr/src \
--volume $(TMP_DIR)/.sonar/cache:/opt/sonar-scanner/.sonar/cache \
--network $(DOCKER_NETWORK) \
--workdir /usr/src/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
docker-run-terraform: ### Run terraform container - mandatory: CMD; optional: DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo hashicorp/terraform:$(DOCKER_TERRAFORM_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo terraform-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _list-variables PATTERN="^TF_VAR_") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
docker-run-terraform-tfsec: ### Run terraform tfsec container - optional: DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]; SEE: https://github.com/tfsec/tfsec
make docker-config > /dev/null 2>&1
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo aquasec/tfsec:$(DOCKER_TERRAFORM_TFSEC_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo tfsec-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _list-variables PATTERN="^TF_VAR_") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
.
docker-run-checkov: ### Run checkov container - optional: DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]; SEE: https://github.com/bridgecrewio/checkov
make docker-config > /dev/null 2>&1
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo bridgecrew/checkov:$(DOCKER_CHECKOV_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo tfsec-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _list-variables PATTERN="^TF_VAR_") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
--directory /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g")
docker-run-terraform-compliance: ### Run terraform compliance container - mandatory: CMD=[-p file -f repo]; optional: DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]; SEE: https://github.com/terraform-compliance/cli
make docker-config > /dev/null 2>&1
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo eerkunt/terraform-compliance:$(DOCKER_TERRAFORM_COMPLIANCE_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo tfsec-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _list-variables PATTERN="^TF_VAR_") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
docker-run-config-lint: ### Run config lint container - mandatory: CMD; optional: DIR,ARGS=[Docker args],IMAGE=[image name],CONTAINER=[container name]; SEE: https://github.com/stelligent/config-lint
make docker-config > /dev/null 2>&1
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo stelligent/config-lint:$(DOCKER_CONFIG_LINT_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo tfsec-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
# ==============================================================================
docker-run-postgres: ### Run postgres container - mandatory: CMD; optional: DIR,ARGS=[Docker args],VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo $(DOCKER_LIBRARY_REGISTRY)/postgres:$(DOCKER_LIBRARY_POSTGRES_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo postgres-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
make docker-image-pull-or-build NAME=postgres VERSION=$(DOCKER_LIBRARY_POSTGRES_VERSION) >&2
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--volume $(PROJECT_DIR):/project \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
docker-run-tools: ### Run tools (Python) container - mandatory: CMD; optional: SH=true,DIR,ARGS=[Docker args],LIB_VOLUME_MOUNT=true,VARS_FILE=[Makefile vars file],IMAGE=[image name],CONTAINER=[container name]
make docker-config > /dev/null 2>&1
mkdir -p $(TMP_DIR)/.python/pip/{cache,packages}
mkdir -p $(HOME)/.aws
lib_volume_mount=$$(([ $(BUILD_ID) -eq 0 ] || [ "$(LIB_VOLUME_MOUNT)" == true ]) && echo "--volume $(TMP_DIR)/.python/pip/cache:/tmp/.cache/pip --volume $(TMP_DIR)/.python/pip/packages:/tmp/.packages" ||:)
image=$$([ -n "$(IMAGE)" ] && echo $(IMAGE) || echo $(DOCKER_LIBRARY_REGISTRY)/tools:$(DOCKER_LIBRARY_TOOLS_VERSION))
container=$$([ -n "$(CONTAINER)" ] && echo $(CONTAINER) || echo tools-$(BUILD_COMMIT_HASH)-$(BUILD_ID)-$$(date --date=$$(date -u +"%Y-%m-%dT%H:%M:%S%z") -u +"%Y%m%d%H%M%S" 2> /dev/null)-$$(make secret-random LENGTH=8))
make docker-image-pull-or-build NAME=tools VERSION=$(DOCKER_LIBRARY_TOOLS_VERSION) >&2
if [[ ! "$(SH)" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$$ ]]; then
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--env HOME=/tmp \
--env PIP_TARGET=/tmp/.packages \
--env PYTHONPATH=/tmp/.packages \
--env XDG_CACHE_HOME=/tmp/.cache \
--volume $(PROJECT_DIR):/project \
--volume $(HOME)/.aws:/tmp/.aws \
--volume $(HOME)/bin:/tmp/bin \
--volume $(HOME)/etc:/tmp/etc \
--volume $(HOME)/usr:/tmp/usr \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
$(CMD)
else
docker run --interactive $(_TTY) --rm \
--name $$container \
--user $$(id -u):$$(id -g) \
--env-file <(make _list-variables PATTERN="^(AWS|TX|TEXAS|NHSD|TERRAFORM)") \
--env-file <(make _list-variables PATTERN="^(DB|DATABASE|SMTP|APP|APPLICATION|UI|API|SERVER|HOST|URL)") \
--env-file <(make _list-variables PATTERN="^(PROFILE|ENVIRONMENT|BUILD|PROGRAMME|ORG|SERVICE|PROJECT)") \
--env-file <(make _docker-get-variables-from-file VARS_FILE=$(VARS_FILE)) \
--env HOME=/tmp \
--env PIP_TARGET=/tmp/.packages \
--env PYTHONPATH=/tmp/.packages \
--env XDG_CACHE_HOME=/tmp/.cache \
--volume $(PROJECT_DIR):/project \
--volume $(HOME)/.aws:/tmp/.aws \
--volume $(HOME)/bin:/tmp/bin \
--volume $(HOME)/etc:/tmp/etc \
--volume $(HOME)/usr:/tmp/usr \
$$lib_volume_mount \
--network $(DOCKER_NETWORK) \
--workdir /project/$(shell echo $(abspath $(DIR)) | sed "s;$(PROJECT_DIR);;g") \
$(ARGS) \
$$image \
/bin/sh -c " \
$(CMD) \
"
fi
# ==============================================================================
docker-compose-start: ### Start Docker Compose - optional: YML=[docker-compose.yml, defaults to $(DOCKER_COMPOSE_YML)]
make docker-config
yml=$$(make _docker-get-docker-compose-yml YML=$(YML))
docker-compose --file $$yml \
up --no-build --detach
docker-compose-start-single-service: ### Start Docker Compose - mandatory: NAME=[service name]; optional: YML=[docker-compose.yml, defaults to $(DOCKER_COMPOSE_YML)]
make docker-config
yml=$$(make _docker-get-docker-compose-yml YML=$(YML))
name=$$([ "$(BUILD_ID)" != 0 ] && echo $(NAME)-$(BUILD_ID) || echo $(NAME))
docker-compose --file $$yml \
up --no-build --detach $$name
docker-compose-stop: ### Stop Docker Compose - optional: YML=[docker-compose.yml, defaults to $(DOCKER_COMPOSE_YML)],ALL=true
make docker-config
yml=$$(make _docker-get-docker-compose-yml YML=$(YML))
docker-compose --file $$yml \
rm \
--stop \
--force
docker rm --force --volumes $$(docker ps --all --filter "name=.*$(BUILD_ID).*" --quiet) 2> /dev/null ||:
[[ "$(ALL)" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$$ ]] && docker rm --force --volumes $$(docker ps --all --quiet) 2> /dev/null ||:
docker-compose-log: ### Log Docker Compose output - optional: DO_NOT_FOLLOW=true,YML=[docker-compose.yml, defaults to $(DOCKER_COMPOSE_YML)]
yml=$$(make _docker-get-docker-compose-yml YML=$(YML))
docker-compose --file $$yml \
logs $$(echo $(DO_NOT_FOLLOW) | grep -E 'true|yes|y|on|1|TRUE|YES|Y|ON' > /dev/null 2>&1 && : || echo "--follow")
docker-compose-exec: ### Run Docker Compose exec command - mandatory: CMD; optional: YML=[docker-compose.yml, defaults to $(DOCKER_COMPOSE_YML)]
yml=$$(make _docker-get-docker-compose-yml YML=$(YML))
docker-compose --file $$yml \
exec $(CMD)
# ==============================================================================
_docker-get-dir:
if [ -n "$(DOCKER_CUSTOM_DIR)" ] && [ -d $(DOCKER_CUSTOM_DIR)/$(NAME) ]; then
echo $(DOCKER_CUSTOM_DIR)/$(NAME)
elif [ -d $(DOCKER_LIB_IMAGE_DIR)/$(NAME) ]; then
echo $(DOCKER_LIB_IMAGE_DIR)/$(NAME)
else
echo $(DOCKER_DIR)/$(NAME)
fi
_docker-get-reg:
if [ -n "$(NAME)" ] && (([ -n "$(DOCKER_CUSTOM_DIR)" ] && [ -d $(DOCKER_CUSTOM_DIR)/$(NAME) ]) || [ -d $(DOCKER_LIB_IMAGE_DIR)/$(NAME) ]); then
echo $(DOCKER_LIBRARY_REGISTRY)
else
echo $(DOCKER_REGISTRY)
fi
_docker-get-variables-from-file:
if [ -f "$(VARS_FILE)" ]; then
vars=$$(cat $(VARS_FILE) | grep -Eo "^[A-Za-z0-9_]*")
for var in $$vars; do
value=$$(echo $$(eval echo "\$$$$var"))
echo $${var}=$${value}
done
fi
_docker-get-login-password:
echo $(DOCKER_PASSWORD)
_docker-get-docker-compose-yml:
yml=$(or $(YML), $(DOCKER_COMPOSE_YML))
if [ "$(BUILD_ID)" != 0 ]; then
make -s docker-run-tools ARGS="--env BUILD_ID=$(BUILD_ID)" CMD=" \
$(BIN_DIR_REL)/docker-compose-processor.py \
$$(echo $$yml | sed 's;//;/;g' | sed "s;$(PROJECT_DIR);;g") \
$(TMP_DIR_REL)/docker-compose-$(BUILD_ID).yml \
"
yml=$(TMP_DIR)/docker-compose-$(BUILD_ID).yml
fi
echo $$yml
_docker-is-lib-image:
([ -n "$(NAME)" ] && [ -d $(DOCKER_LIB_IMAGE_DIR)/$(NAME) ]) && echo true || echo false
_list-variables: ### List environment variables that match the pattern - mandatory: PATTERN="^AWS_"; return: [variables list]
env | grep -Ei "$(PATTERN)" | sed -e 's/[[:space:]]*$$//' | grep -Ev '^[A-Za-z0-9_]+=$$' | sort | grep -v '[[:space:]]' ||:
# ==============================================================================
docker-image-get-digest: ### Get image digest by matching tag pattern - mandatory: NAME=[image name],VERSION|TAG=[string to match version/tag of an image]
[ $$(make _docker-is-lib-image NAME=$(NAME)) == false ] && make docker-login > /dev/null 2>&1
make aws-ecr-get-image-digest \
REPO=$$(make _docker-get-reg)/$(NAME) \
TAG=$(or $(VERSION), $(TAG))
docker-image-find-and-version-as: ### Find image based on git commit hash and tag it - mandatory: VERSION|TAG=[new version/tag],NAME=[image name]; optional: COMMIT=[git commit hash, defaults to main]
commit=$(or $(COMMIT), $$(make git-branch-get-main-name))
hash=$$(make git-commit-get-hash COMMIT=$$commit)
digest=$$(make docker-image-get-digest NAME=$(NAME) TAG=$$hash)
make docker-pull NAME=$(NAME) DIGEST=$$digest
make docker-tag NAME=$(NAME) DIGEST=$$digest TAG=$(or $(VERSION), $(TAG))
make docker-push NAME=$(NAME) TAG=$(or $(VERSION), $(TAG))
docker-repo-list-tags: ### List repository tags - mandatory: REPO=[repository name]
(
curl "https://registry.hub.docker.com/api/content/v1/repositories/public/library/$(REPO)/tags?page=1&page_size=100" 2>/dev/null | jq -r '.results[].name';
curl "https://registry.hub.docker.com/api/content/v1/repositories/public/library/$(REPO)/tags?page=2&page_size=100" 2>/dev/null | jq -r '.results[].name'
curl "https://registry.hub.docker.com/api/content/v1/repositories/public/library/$(REPO)/tags?page=3&page_size=100" 2>/dev/null | jq -r '.results[].name'
curl "https://registry.hub.docker.com/api/content/v1/repositories/public/library/$(REPO)/tags?page=4&page_size=100" 2>/dev/null | jq -r '.results[].name'
curl "https://registry.hub.docker.com/api/content/v1/repositories/public/library/$(REPO)/tags?page=5&page_size=100" 2>/dev/null | jq -r '.results[].name'
) | sort
# ==============================================================================
.SILENT: \
_docker-get-dir \
_docker-get-docker-compose-yml \
_docker-get-login-password \
_docker-get-reg \
_docker-get-variables-from-file \
_docker-is-lib-image \
_list-variables \
docker-image-get-digest \
docker-image-get-version \
docker-image-set-version \
docker-login \
docker-repo-list-tags