-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMakefile
More file actions
156 lines (139 loc) · 4.42 KB
/
Makefile
File metadata and controls
156 lines (139 loc) · 4.42 KB
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
#
# Environment specific vars. Defaults set below.
#
include env_make
#
# Phony targets.
#
.PHONY: help build run-dev run-release save destroy
#
# Iterated after major releases.
#
VERSION ?= 1.0.0
#
# App image name: <user>/<appname>
#
APP_IMAGE_NAME ?= lev-interactive/myapp
#
# Set NODE_ENV for node/express.
#
NODE_ENV ?= development
#
# Main app container name.
#
APP_CONTAINER_NAME ?= myapp_container
#
# Container for database.
#
MONGO_CONTAINER_NAME ?= myapp_mongo_container
#
# Container for redis.
#
REDIS_CONTAINER_NAME ?= myapp_redis_container
#
# Container for nginx.
#
NGINX_CONTAINER_NAME ?= myapp_nginx_container
#
# Backup directory for db exports.
#
BACKUPS ?= backups
#
# Current directory.
#
CURDIR = $(shell pwd)
#
# Last backed up mongodb dump.
#
LAST_DUMP := $(shell ls -t $(CURDIR)/$(BACKUPS)/mongo | head -1)
help:
@echo "\nApplication management. Please make sure you have the env_make file setup.\n"
@echo "Usage: \n\
make build This builds the $(APP_IMAGE_NAME) image. \n\
make run-dev This will start the application mapping port 80 to 3030. All src \n\
files will be volumed as well for automatic restarts.\n\
be working in for instant changes. Runs on port 3000. \n\
make run-release This will run a container without the volumes on port 80. Good \n\
for production. @TODO \n\
make save This will save the database in the backups directory. \n\
make restore This will restore from the last time you saved. \n\
make destroy Stops and removes all running containers. \
"
build:
@docker build -t $(APP_IMAGE_NAME):$(VERSION) --rm .
save:
@if ! docker ps -a | grep -qF $(MONGO_CONTAINER_NAME); then \
echo "No mongo container is running."; exit 1; \
fi
@mkdir -p $(BACKUPS) $(BACKUPS)/mongo
@docker exec $(MONGO_CONTAINER_NAME) mongodump \
--out /var/backups/mongo/dump-`date +%Y-%m-%d:%H:%M:%S`
restore:
docker exec $(MONGO_CONTAINER_NAME) mongorestore --drop \
/var/backups/mongo/$(LAST_DUMP)
run-dev:
@echo "\n\033[0;33m=> Starting Redis --------------------------------------\033[0m"
@docker run -d \
--restart always \
--name $(REDIS_CONTAINER_NAME) \
redis:latest
@echo "\n\033[0;33m=> Starting Mongo --------------------------------------\033[0m"
@docker run -d \
--restart always \
--name $(MONGO_CONTAINER_NAME) \
-v $(CURDIR)/server-config/mongo/mongo.conf:/etc/mongod.conf \
-v $(CURDIR)/$(BACKUPS):/var/backups \
mongo:3.0
@echo "\n\033[0;33m=> Starting app instances (clustered) --------------------------------------\033[0m"
@docker run -d \
--name $(APP_CONTAINER_NAME)_1 \
--link $(MONGO_CONTAINER_NAME):mongo \
--link $(REDIS_CONTAINER_NAME):redis \
--restart always \
-e NODE_ENV=$(NODE_ENV) \
-v $(CURDIR)/src:/app/src \
$(APP_IMAGE_NAME):$(VERSION)
@docker run -d \
--name $(APP_CONTAINER_NAME)_2 \
--link $(MONGO_CONTAINER_NAME):mongo \
--link $(REDIS_CONTAINER_NAME):redis \
--restart always \
-e NODE_ENV=$(NODE_ENV) \
-v $(CURDIR)/src:/app/src \
$(APP_IMAGE_NAME):$(VERSION)
@docker run -d \
--name $(APP_CONTAINER_NAME)_3 \
--link $(MONGO_CONTAINER_NAME):mongo \
--link $(REDIS_CONTAINER_NAME):redis \
--restart always \
-e NODE_ENV=$(NODE_ENV) \
-v $(CURDIR)/src:/app/src \
$(APP_IMAGE_NAME):$(VERSION)
@echo "\n\033[0;33m=> Starting NGINX --------------------------------------\033[0m"
@docker run -d \
--name $(NGINX_CONTAINER_NAME) \
--link $(APP_CONTAINER_NAME)_1:nodeapp_1 \
--link $(APP_CONTAINER_NAME)_2:nodeapp_2 \
--link $(APP_CONTAINER_NAME)_3:nodeapp_3 \
--restart always \
-p "3030:80" \
-v $(CURDIR)/server-config/nginx/sites-enabled:/etc/nginx/sites-enabled \
-v $(CURDIR)/server-config/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx:1.7.9
@echo "\n\033[0;33m=> Tailing the nginx logs. --------------------------------------\033[0m"
docker logs -f $(NGINX_CONTAINER_NAME)
destroy:
@echo "\033[0mTearing down environment. \033[0m"
@-docker rmi $(docker images -f "dangling=true" -q)
@-docker stop $(APP_CONTAINER_NAME)_1
@-docker rm -v $(APP_CONTAINER_NAME)_1
@-docker stop $(APP_CONTAINER_NAME)_2
@-docker rm -v $(APP_CONTAINER_NAME)_2
@-docker stop $(APP_CONTAINER_NAME)_3
@-docker rm -v $(APP_CONTAINER_NAME)_3
@-docker stop $(MONGO_CONTAINER_NAME)
@-docker rm -v $(MONGO_CONTAINER_NAME)
@-docker stop $(REDIS_CONTAINER_NAME)
@-docker rm -v $(REDIS_CONTAINER_NAME)
@-docker stop $(NGINX_CONTAINER_NAME)
@-docker rm -v $(NGINX_CONTAINER_NAME)