-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
203 lines (161 loc) · 7.24 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
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
#******************************************************************************#
# INCEPTION MAKEFILE #
#******************************************************************************#
#------------------------------------------------------------------------------#
# COLOR SETTINGS #
#------------------------------------------------------------------------------#
W := \033[0m
G := \033[1;32m
Y := \033[1;33m
#------------------------------------------------------------------------------#
# RULES #
#------------------------------------------------------------------------------#
all: setup up
setup:
@echo "\n-------------------- $YConfiguration $W--------------------"
@echo "Running configuration script :"
@echo " ...User set as: ${LOGIN}"
@./srcs/requirements/tools/setup.sh 2>/dev/null
@echo " ...Host configuration is done"
@mkdir -p ${PATH_DATA}
@mkdir -p ${PATH_DATA}/mariadb-data
@mkdir -p ${PATH_DATA}/wordpress-data
@echo " ...data dir. created for mariadb and wordpress"
@echo "Configuration is $Gdone$W"
@echo "-------------------------------------------------------\n"
up: setup
@echo "---------------------- $YBuilding $W-----------------------"
docker-compose -f ${DC_FILE} up --detach --build --remove-orphans
@echo "-------------------------------------------------------\n"
start:
@docker-compose -f ${DC_FILE} up --detach --no-build
stop:
@echo "---------------------- $YStopping $W-----------------------"
@docker-compose -f ${DC_FILE} stop
@echo "-------------------------------------------------------\n"
clean: stop
@echo "---------------------- $YCleaning$W -----------------------"
@./srcs/requirements/tools/cleanup.sh
@if [ -d "${PATH_DATA}" ]; then \
echo "Removing data directory ${PATH_DATA}"; \
sudo rm -rf ${PATH_DATA}; \
fi
@if [ -n "$$(docker-compose -f ${DC_FILE} ps -q)" ]; then \
echo "Bringing down services defined in ${DC_FILE}..."; \
docker-compose -f ${DC_FILE} down --volumes --remove-orphans; \
fi
@if [ -n "$$(docker container ls -aq)" ]; then \
echo "Removing all stopped containers..."; \
yes | docker container prune; \
fi
@if [ -n "$$(docker images -aq)" ]; then \
echo "Removing all unused images..."; \
yes | docker image prune -a; \
fi
@echo "-------------------------------------------------------\n"
fclean: stop clean
@echo "---------------------- $YFCleaning$W -----------------------"
@docker system prune
@echo "-------------------------------------------------------\n"
re: fclean all
#------------------------------------------------------------------------------#
# SETTINGS #
#------------------------------------------------------------------------------#
# Include environment variables
-include .env.make
# Docker Compose file
DC_FILE := ./srcs/docker-compose.yml
# Convert .env to Makefile format
.env.make: srcs/.env
@sudo cat srcs/.env | sed 's/=/?=/g' > .env.make
#------------------------------------------------------------------------------#
# HELP MENU #
#------------------------------------------------------------------------------#
help:
@echo "Usage: make [command]"
@echo "Commands:"
@echo " all $(G)Build all images and launch the containers$(W)"
@echo " bash-mariadb $(G)Access bash shell in the MariaDB container$(W)"
@echo " bash-wordpress $(G)Access bash shell in the WordPress container$(W)"
@echo " build $(G)Build all Docker images (Nginx, MariaDB, WordPress)$(W)"
@echo " build-mariadb $(G)Build the MariaDB Docker image$(W)"
@echo " build-nginx $(G)Build the Nginx Docker image$(W)"
@echo " build-wordpress $(G)Build the WordPress Docker image$(W)"
@echo " fclean $(G)Remove all containers, images, volumes, and custom networks$(W)"
@echo " inspect-mariadb $(G)Inspect the MariaDB volume$(W)"
@echo " inspect-wordpress $(G)Inspect the WordPress volume$(W)"
@echo " list $(G)List all running containers using docker-compose ps$(W)"
@echo " logs-mariadb $(G)Tail logs from the MariaDB container$(W)"
@echo " logs-wordpress $(G)Tail logs from the WordPress container$(W)"
@echo " mysql $(G)Access MariaDB MySQL prompt$(W)"
@echo " mysql-test $(G)Access MariaDB MySQL prompt in a test container$(W)"
@echo " network $(G)List all Docker networks$(W)"
@echo " re $(G)Rebuild and restart the containers$(W)"
@echo " run-mariadb $(G)Run MariaDB container in detached mode$(W)"
@echo " run-nginx $(G)Run Nginx container in detached mode$(W)"
@echo " run-wordpress $(G)Run WordPress container in detached mode$(W)"
@echo " setup $(G)Prepare environment, directories for data$(W)"
@echo " start $(G)Alias for 'up', start services in background without rebuild$(W)"
@echo " stop $(G)Stop services defined in docker-compose.yml$(W)"
@echo " stop-mariadb $(G)Stop the MariaDB test container$(W)"
@echo " stop-nginx $(G)Stop the Nginx test container$(W)"
@echo " up $(G)Start services defined in docker-compose.yml in background$(W)"
@echo " volume $(G)List all Docker volumes$(W)"
#------------------------------------------------------------------------------#
# TOOLS #
#------------------------------------------------------------------------------#
# Docker build targets
build: build-nginx build-mariadb build-wordpress
build-nginx:
docker build -t nginx srcs/requirements/nginx/
build-mariadb:
docker build -t mariadb srcs/requirements/mariadb/
build-wordpress:
docker build -t wordpress srcs/requirements/wordpress/
# Docker run targets for testing
run: run-nginx run-mariadb run-wordpress
run-nginx:
docker run -ti --name nginx-test -p 443:443 -d nginx
run-mariadb:
docker run -ti --name mariadb-test -d mariadb
run-wordpress:
docker run -ti --name wordpress-test -d wordpress
# Docker exec shortcuts
mysql:
docker exec -it mariadb mysql -u root -p'${DB_ROOT}'
mysql-test:
docker exec -it mariadb-test mysql -u root -p'${DB_ROOT}'
bash-mariadb:
docker exec -it mariadb bash
bash-wordpress:
docker exec -it wordpress bash
# Docker logs targets
logs:
cd srcs && docker-compose logs -f
logs-mariadb:
docker logs -f mariadb
logs-wordpress:
docker logs -f wordpress
# List running containers
list:
cd srcs && docker-compose ps
# docker ps -a
# Volumes list and inspect
volume:
docker volume ls
inspect-mariadb:
docker volume inspect 'srcs_mariadb-vol'
inspect-wordpress:
docker volume inspect 'srcs_wordpress-vol'
# list networks
network:
docker network ls
# Stop test containers
stop-test: stop-nginx stop-mariadb
stop-nginx:
docker stop nginx-test
stop-mariadb:
docker stop mariadb-test
stop-wordpress:
docker stop wordpress-test
.PHONY: all stop build run list del fclean clean down up start setup build-nginx build-mariadb build-wordpress run-nginx run-mariadb run-wordpress logs-mariadb logs-wordpress stop-test stop-nginx stop-mariadb re