2
2
# This Makefile doesn't consider Windows Environment. If you use it in Windows, please be careful.
3
3
SHELL := /bin/sh
4
4
5
+ define NEWLINE
6
+
7
+ endef
8
+
5
9
# -------- color constant start -------
6
10
RED =\033[31;01m
7
11
GREEN =\033[32;01m
@@ -38,7 +42,8 @@ _compose_version=""
38
42
39
43
# The params will be override if specified by input
40
44
# SHOW_TIP whether or not show the init info
41
- SHOW_TIP? =true
45
+ SHOW_TIP? =false
46
+ override CURRENT_SCRIPT_DIR =${_current_script_dir}
42
47
43
48
# PROJECT_ROOT the project root dir
44
49
PROJECT_ROOT? =${_default_project_root}
@@ -52,8 +57,28 @@ REGISTRY_PASSWORD?=
52
57
REPOSITORY? =${_default_repository}
53
58
TAG? =${_default_tag}
54
59
60
+ # image id
55
61
IMAGE_ID? =
62
+ # container id
56
63
CONTAINER_ID? =
64
+ # display logs count, default 200
65
+ LOGS_COUNT? =200
66
+
67
+ # for image list filter, format: -f reference=""
68
+ IMAGE_LIST_PARAMS? =-f reference=""
69
+
70
+ CONTAINER_LIST_PARAMS? =
71
+
72
+ COMPOSE_RUN_PARAMS? ="-d"
73
+
74
+ # for run container, the image(name or id) use
75
+ CONTAINER_IMAGE? =${REPOSITORY}:${TAG}
76
+ # for run container, container name, format: --name <container_name>
77
+ CONTAINER_NAME_PARAM? =
78
+ # for run container, format like: -d daemon -v v1:v1 ... -p p1:p2 ... -e k1=v1 ...
79
+ CONTAINER_PARAMS? =
80
+ # for run container, params for app need, container CMD need
81
+ CONTAINER_CMD_PARAMS? =
57
82
58
83
# check or override: SHOW_TIP
59
84
ifeq (,$(strip ${SHOW_TIP}) )
@@ -128,14 +153,16 @@ $(info [init] vcs: [git=${_git_version}, commit_short=${gitCommitShort}, comm
128
153
$(info [init] env : [docker=${_docker_version}, compose=${_compose_version}])
129
154
$(info [init] base : [ docker_file=${DOCKER_FILE}])
130
155
$(info [init] base : [compose_file=${DOCKER_COMPOSE_FILE}])
156
+ $(info ${NEWLINE})
131
157
endif
132
158
133
159
134
160
# ----------- Makefile commands -----------
135
161
.PHONY : default help env cp-ignore \
136
- login login-auth build tag push list-image run run-daemon stop list-docker \
137
- rmi image-prune rm docker-prune compose-run compose-daemon compose-stop
138
- prune remove
162
+ login login-auth build tag push list-image list-docker\
163
+ run stop \
164
+ rmi image-prune rm docker-prune compose-run compose-stop \
165
+ logs
139
166
140
167
default : help
141
168
@@ -150,6 +177,11 @@ env:
150
177
@echo -e " ${COLOR_INFO} $( shell docker-compose --version) ${COLOR_RESET} "
151
178
@echo -e " [command-env] ${GREEN} execute success${COLOR_RESET} "
152
179
180
+ # # cp-ignore: copy the default '.dockerignore' file to the root dir of your project, pls rename it manually
181
+ cp-ignore :
182
+ cp ${CURRENT_SCRIPT_DIR} /.dockerignore-example ${PROJECT_ROOT}
183
+ @echo -e " [command-cp-ignore] ${GREEN} execute success${COLOR_RESET} "
184
+
153
185
# # login: login docker-hub
154
186
login :
155
187
docker login
@@ -159,3 +191,77 @@ login:
159
191
login-auth :
160
192
docker login -u " ${REGISTRY_ACCOUNT} " " ${REGISTRY} " -p " ${REGISTRY_PASSWORD} "
161
193
@echo -e " [command-login-auth] ${GREEN} execute success${COLOR_RESET} "
194
+
195
+ # # build: docker build with REPOSITORY(default your dir name) and TAG(default latest)
196
+ build :
197
+ @echo -e " building image with name:[${REPOSITORY} :${TAG} ], use Dockerfile:${DOCKER_FILE} "
198
+ @cd ${PROJECT_ROOT} && docker build -t ${REPOSITORY} :${TAG} -f ${DOCKER_FILE} .
199
+ @echo -e " build image with name:[${REPOSITORY} :${TAG} ], use Dockerfile:${DOCKER_FILE} "
200
+ @echo -e " [command-build] ${GREEN} execute success${COLOR_RESET} "
201
+
202
+ # # tag: docker tag, only support rename tag by image id
203
+ tag :
204
+ @docker tag ${IMAGE_ID} ${REPOSITORY} :${TAG}
205
+ @echo -e " [command-tag] ${GREEN} image with ID [${IMAGE_ID} ], rename to [${REPOSITORY} :${TAG}${COLOR_RESET} ]"
206
+ @echo -e " [command-tag] ${GREEN} execute success${COLOR_RESET} "
207
+
208
+ # # push: push docker images to docker hub or others. shall login before push!
209
+ push :
210
+ docker push ${REPOSITORY} :${TAG}
211
+ @echo -e " [command-push] ${GREEN} push ${REPOSITORY} :${TAG} over${COLOR_RESET} "
212
+
213
+ # # list-image: list docker images, can with filter, -f reference="", ref 'docker images --help'
214
+ list-image :
215
+ @docker images ${IMAGE_LIST_PARAMS}
216
+ @echo -e " [command-list-image] ${GREEN} list all images, with params:[${IMAGE_LIST_PARAMS} ]${COLOR_RESET} "
217
+
218
+ # # list-docker: list docker container, can with filter, ref 'docker ps --help' or 'docker container ls --help'
219
+ list-docker :
220
+ @docker ps ${CONTAINER_LIST_PARAMS}
221
+ @echo -e " [command-list-docker] ${GREEN} list containers with params:[${CONTAINER_LIST_PARAMS} ]${COLOR_RESET} "
222
+
223
+ # # run: docker run with specified params, must set 'CONTAINER_IMAGE', format(image name or id)
224
+ run :
225
+ @echo -e " [command-run] ${GREEN} run docker, with container image:[${CONTAINER_IMAGE} ]${COLOR_RESET} "
226
+ docker run -it ${CONTAINER_NAME_PARAM} --rm ${CONTAINER_PARAMS} ${CONTAINER_IMAGE} ${CONTAINER_CMD_PARAMS}
227
+
228
+ # # stop: stop the running containers with container ids
229
+ stop :
230
+ docker stop ${CONTAINER_ID}
231
+ @echo -e " [command-stop] ${GREEN} stop container, with container id:[${CONTAINER_ID} ]${COLOR_RESET} "
232
+
233
+ # # rmi: remove the images with image ids
234
+ rmi :
235
+ docker rmi ${IMAGE_ID}
236
+ @echo -e " [command-rmi] ${GREEN} remove images :[${IMAGE_ID} ]${COLOR_RESET} "
237
+
238
+ # # image-prune: remove all unused images, ref docker image --help
239
+ image-prune :
240
+ docker image prune
241
+ @echo -e " [command-image-prune] ${GREEN} remove all unused images${COLOR_RESET} "
242
+
243
+ # # rm: remove the containers with container ids
244
+ rm :
245
+ docker rm ${CONTAIN_ID}
246
+ @echo -e " [command-rm] ${GREEN} remove containers :[${CONTAIN_ID} ]${COLOR_RESET} "
247
+
248
+ # # docker-prune: remove all stopped containers, ref docker container --help
249
+ docker-prune :
250
+ docker image prune
251
+ @echo -e " [command-docker-prune] ${GREEN} remove all stopped containers${COLOR_RESET} "
252
+
253
+ compose-run :
254
+ @echo -e " [command-compose-run] ${GREEN} docker-compose run with file:[${DOCKER_COMPOSE_FILE} ]${COLOR_RESET} "
255
+ docker-compose -f ${DOCKER_COMPOSE_FILE} up ${COMPOSE_RUN_PARAMS}
256
+
257
+ compose-stop :
258
+ @echo -e " [command-compose-stop] ${GREEN} docker-compose stop with file:[${DOCKER_COMPOSE_FILE} ]${COLOR_RESET} "
259
+ docker-compose -f ${DOCKER_COMPOSE_FILE} stop
260
+
261
+ logs :
262
+ docker logs -fn ${LOGS_COUNT} ${CONTAINER_ID}
263
+ @echo -e " [command-logs] ${GREEN} the latest ${LOGS_COUNT} logs, for container_id:${CONTAINER_ID}${COLOR_RESET} "
264
+
265
+ compose-logs :
266
+ docker-compose logs
267
+ @echo -e " [command-compose-logs] ${GREEN} execute success${COLOR_RESET} "
0 commit comments