-
Notifications
You must be signed in to change notification settings - Fork 11
/
Makefile
executable file
·151 lines (121 loc) · 4 KB
/
Makefile
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
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
VERSION:=$(shell [ -r VERSION ] && cat VERSION || echo latest)
NAMESPACE:=jorgeacf
REPO:=$(shell basename "$$PWD")
# Ignore arguments first word
CMD_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
CURRENT_USER_NAME := $(shell whoami)
#CURRENT_USER_ID := $(id -u ${CURRENT_USER_NAME})
#CURRENT_USER_GROUP_ID := $(id -g ${CURRENT_USER_NAME})
OS_NAME := $(shell uname -s | tr A-Z a-z)
SHELL += -eu
BLUE := \033[0;34m
GREEN := \033[0;32m
RED := \033[0;31m
NC := \033[0m
DOCKER_RUN_PARAMETERS=-p 80:80 -p 8080:8080 -p 8081:8081 -v $(HOME):/root
.SILENT:
#
# Single docker image targets
#
.DEFAULT_GOAL := help
.PHONY: help
help:
@echo ''
@echo "OS: ${OS_NAME}"
@echo "${GREEN}Docker Image: $(NAMESPACE)/$(REPO):$(VERSION)${NC}"
@echo ''
@echo 'Usage: make ${BLUE}[TARGET]${NC} ${RED}[EXTRA_ARGUMENTS]${NC}'
@echo 'Target:'
@echo ' ${BLUE}lint${NC} runs Dockerfile and entrypoint.sh lint tools.'
@echo ' ${BLUE}build${NC} this docker image locally.'
@echo ' ${BLUE}push${NC} this docker image to a remote repository.'
@echo ' ${BLUE}run${NC} a container of this docker image.'
@echo ' ${BLUE}run-d${NC} a container in the backgroud of this docker image.'
@echo ' ${BLUE}clean${NC} all containers of this docker image.'
@echo ''
@echo 'Extra arguments:'
@echo '${RED}cmd=${NC}: make cmd="whoami"'
.PHONY: version
version:
@echo ''
@echo "OS: \t\t${OS_NAME}"
@echo "Docker Image: \t$(NAMESPACE)/$(REPO):$(VERSION)"
@echo "Current User: \t${CURRENT_USER_NAME}"
@echo "Root Dir: \t${ROOT_DIR}"
@echo "CMD Args: \t\t${CMD_ARGS}"
@echo ''
# check Dockerfile with lint
.PHONY: all
all:
@$(MAKE) build
.PHONY: install-build-dependencies
install-build-dependencies:
ifeq ($(OS_NAME),darwin)
brew install hadolint
brew install shellcheck
endif
ifeq ($(OS_NAME),linux)
echo "On Linux"
endif
.PHONY: lint
lint:
#@dockerlint Dockerfile
# brew install hadolint
#hadolint Dockerfile
# brew install shellcheck
#shellcheck entrypoint.sh
.PHONY: build
build:
@$(MAKE) version
@$(MAKE) lint
#@sed -i -e "s/ARG NODEJS_VERSION=.*/ARG NODEJS_VERSION=$(VERSION)/g" Dockerfile
#@echo "${BLUE}Starting to build docker image...${NC}" $<
@docker build -t $(NAMESPACE)/$(REPO):$(VERSION) --build-arg VERSION=$(VERSION) . ; \
if [ $$? -ne 0 ] ; \
then echo "\n${RED} Build failed :(${NC}\n" ; \
else echo "\n${GREEN} ✓ Successfully build [$(NAMESPACE)/$(REPO):$(VERSION)] docker image. ${NC}\n" ; fi
.PHONY: push
push:
#@docker login --username=jorgeacf
@docker tag $(NAMESPACE)/$(REPO):$(VERSION) $(NAMESPACE)/$(REPO):$(VERSION)
@docker tag $(NAMESPACE)/$(REPO):$(VERSION) $(NAMESPACE)/$(REPO):latest
@docker push $(NAMESPACE)/$(REPO):$(VERSION)
@docker push $(NAMESPACE)/$(REPO):latest
.PHONY: run
run:
docker run -it \
$(DOCKER_RUN_PARAMETERS) \
$(NAMESPACE)/$(REPO):$(VERSION) ${CMD_ARGS}
.PHONY: run-d
run-d:
@docker run -itd \
$(DOCKER_RUN_PARAMETERS) \
$(NAMESPACE)/$(REPO):$(VERSION) ${CMD_ARGS}
.PHONY: exec
exec:
if [ -z "${CMD_ARGS}" ] ; \
then docker exec -it $$(docker ps -a -q --filter ancestor=$(NAMESPACE)/$(REPO):$(VERSION) --filter status=running --format="{{.ID}}") /bin/bash; \
else docker exec -it $$(docker ps -a -q --filter ancestor=$(NAMESPACE)/$(REPO):$(VERSION) --filter status=running --format="{{.ID}}") entrypoint.sh ${CMD_ARGS}; fi
.PHONY: clean
clean:
@docker stop $$(docker ps -a -q --filter ancestor=$(NAMESPACE)/$(REPO):$(VERSION) --format="{{.ID}}") >> /dev/null
@docker rm $$(docker ps -a -q --filter ancestor=$(NAMESPACE)/$(REPO):$(VERSION) --format="{{.ID}}")
.PHONY: rerun
rerun:
@$(MAKE) clean
@$(MAKE) build
@$(MAKE) run
.PHONY: rerun-d
rerun-d:
@$(MAKE) clean
@$(MAKE) build
@$(MAKE) run-d
#
# All docker image targets
#
.PHONY: build-all
build-all:
for x in os/*; do [ -d $$x ] || continue; pushd $$x; make build; popd; done
#for x in bigdata/*; do [ -d $$x ] || continue; pushd $$x; make build; popd; done
#for x in ci/*; do [ -d $$x ] || continue; pushd $$x; make build; popd; done