-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocker-Compose.yaml
More file actions
224 lines (206 loc) · 7.76 KB
/
Docker-Compose.yaml
File metadata and controls
224 lines (206 loc) · 7.76 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
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
services:
# SETUP INSTRUCTIONS (FOR WINDOWS AND MAC)
# 1. Create the required folders for mounting the volumes if they do not exist
# (refer to the volumes section at the end of this file)
# 2. Go to "Docker Desktop → Settings → Resources → File Sharing" → Add the project’s root folder.
apache-php:
image: customized-apache-httpd-plus-php:1.0
build:
context: ./images/apache-httpd-plus-php
dockerfile: Dockerfile
hostname: apache-2.4-php-8.2
container_name: apache-2.4-php-8.2
restart: always
ports:
- 8002:80
volumes:
# To store the web-related files in a persistent volume (directory/folder) in the host
- apache-httpd-www:/var/www/html/
# To store the PHP configuration file in the host
- php-config:/usr/local/etc/php/
environment:
PHP_DISPLAY_ERRORS: 1
PHP_DISPLAY_STARTUP_ERRORS: 1
command: >
/bin/bash -c "
apache2-foreground
"
mysql-8.0.40-debian:
image: customized-mysql:1.0
build:
context: ./images/mysql
dockerfile: Dockerfile
hostname: mysql-8.0.40-debian
container_name: mysql-8.0.40-debian
restart: no
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: 5trathm0re
volumes:
# To store the server's configuration file in the host
- mysql-config:/etc/mysql/
# To store the server's log files in the host
- mysql-log:/var/log/mysql/
# To store the server's data persistently in the host
- mysql-data:/var/lib/mysql/
# To run the SQL scripts used to create the required databases
- mysql-init:/docker-entrypoint-initdb.d/
command: >
/bin/bash -c "
chmod 644 /etc/mysql/my.cnf &&
chmod 644 /etc/mysql/conf.d/docker.cnf &&
chmod 644 /etc/mysql/conf.d/mysql.cnf &&
exec docker-entrypoint.sh mysqld
"
postgres-17.2:
image: postgres:17.2
hostname: postgres-17.2
container_name: postgres-17.2
restart: always
shm_size: 1g
ports:
- 5433:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 5trathm0re
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
# To store the server's configuration files (postgresql.conf and pg_hba.conf) in the host
- postgres-config:/etc/postgresql/
# To store the server's log files in the host
- postgres-log:/var/log/postgresql/
# To store the server's data persistently in the host
- postgres-data:/var/lib/postgresql/data
# To run the SQL scripts used to create the required databases
- postgres-init:/docker-entrypoint-initdb.d/
command: postgres -c 'config_file=/etc/postgresql/postgresql.conf'
mysql_backup:
image: customized-mysql:1.0
build:
context: ./images/mysql
dockerfile: Dockerfile
hostname: mysql_backup
container_name: mysql_backup
# restart: no → The default. The container runs once. If it stops, then it stays stopped.
# restart: always → The container always runs, even after the host machine restarts.
# restart: on-failure → The container restarts only if it exists with a non-zero exit code.
# restart: on-failure:5 → The container restarts only if it exists with a non-zero exit code, but it is restarted up to a maximum of 5 times in a row.
# restart: unless-stopped → The container always restarts unless it is manually stopped.
restart: unless-stopped
depends_on:
- mysql-8.0.40-debian
volumes:
# To store the server's backups persistently in the host
- mysql-backup:/backup/mysql/
# Secret file containing the credentials to access the MySQL server
- mysql-secrets:/root/
# `sleep 30` (30 seconds) provides a startup delay to give the MySQL server time to start
# before the first backup is initiated.
# `sleep 10800` (3 hours) provides a periodic backup interval that repeats forever,
# as long as the container is running.
# ----------- We can change this to `sleep 120` for teaching purposes, i.e., perform the backups every 2 minutes.
# Retention Policy for the Backups (to avoid filling up the storage space):
# Best Practice: Retention policies should be enforced by the storage layer, not in a shell loop inside the container.
# `find /backup/mysql -type f -name '*.sql' -mtime +7 -print -delete >> $$LOG_FILE 2>&1;`
# ----------- We can change this to `find /backup/mysql -type f -name '*.sql' -mmin +2 -print -delete >> $$LOG_FILE 2>&1;` for files older than 2 minutes.
# -mtime +7 → finds files older than 7 days (assuming that you will not take more than 7 days to notice corrupted databases).
# -type f → finds only files, not directories.
# -name '*.sql' → only database dumps.
# -delete → simple, permanent deletion without any confirmation.
# -print → prints the names of the files that would be deleted (basic logging).
# $$LOG_FILE 2>&1; → Take errors and send them to the same place as the normal output.
command: >
/bin/bash -c "
LOG_FILE=/backup/mysql/backup.log;
touch $$LOG_FILE &&
chmod 600 /root/.my.cnf &&
sleep 30 &&
while true; do
echo \"[$(date '+%Y-%m-%d %H:%M:%S')] Backup started\" >> $$LOG_FILE;
if mysqldump --all-databases --single-transaction --quick --lock-tables=false > /backup/mysql/full_dump_$(date +%Y%m%d_%H%M).sql; then
echo \"[$(date '+%Y-%m-%d %H:%M:%S')] Backup completed successfully\" >> $$LOG_FILE;
find /backup/mysql -type f -name '*.sql' -mtime +7 -print -delete >> $$LOG_FILE 2>&1;
echo \"[$(date '+%Y-%m-%d %H:%M:%S')] Backup retention policy enforced\" >> $$LOG_FILE;
else
echo \"[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: Backup failed\" >> $$LOG_FILE;
fi
sleep 10800;
done
"
volumes:
# MySQL volumes
mysql-config:
driver: local # The volume will be stored locally on the host machine
driver_opts:
type: none # Binds to the path specified in "device" instead of creating a new directory/file on the host
o: bind # Confirms that Docker maps a directory/file on the host directly into the container
device: ./container-volumes/mysql/etc-mysql/
mysql-log:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/mysql/var-log-mysql/
mysql-data:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/mysql/var-lib-mysql/
mysql-init:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/mysql/init-scripts/
mysql-backup:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/backup/mysql/
mysql-secrets:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/docker-secrets/mysql/
# PostgreSQL volumes
postgres-config:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/postgresql/etc-postgresql/
postgres-log:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/postgresql/var-log-postgresql/
postgres-data:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/postgresql/var-lib-postgresql-data/
postgres-init:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/postgresql/init-scripts/
# Apache volumes
apache-httpd-www:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/apache/var-www-html/
php-config:
driver: local
driver_opts:
type: none
o: bind
device: ./container-volumes/apache/etc-php/