Skip to content

Commit d325146

Browse files
dvaerumJ0WI
andauthored
Added entrypoint hooks for your own custom scripts (#1964)
* Added entrypoint hooks for your own custom scripts Signed-off-by: Dennis Vestergaard Værum <[email protected]> * Small changes: - Only execute shell-scripts (mening files ending with .sh) - Sort the files before executing them, had forgotten 😅 - Added a message when a hook script finish - Added prefix arror to message to show the are related Signed-off-by: Dennis Vestergaard Værum <[email protected]> * Show in the search msg that it only searches for '*.sh' files Signed-off-by: Dennis Vestergaard Værum <[email protected]> * Fixed spelling mistake Co-authored-by: J0WI <[email protected]> Signed-off-by: Dennis Værum <[email protected]> * Updated the `README.md` file Signed-off-by: Dennis Vestergaard Værum <[email protected]> * change from using find to using a for-loop to located the `.sh` files Signed-off-by: Dennis Vestergaard Værum <[email protected]> * Fix bug - that would make docker-entrypoint.sh failed, hook folders was empty Signed-off-by: Dennis Vestergaard Værum <[email protected]> --------- Signed-off-by: Dennis Vestergaard Værum <[email protected]> Signed-off-by: Dennis Værum <[email protected]> Co-authored-by: J0WI <[email protected]>
1 parent ceb2893 commit d325146

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

Dockerfile-alpine.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ RUN { \
9999
} > "${PHP_INI_DIR}/conf.d/nextcloud.ini"; \
100100
\
101101
mkdir /var/www/data; \
102+
mkdir -p /docker-entrypoint-hooks.d/pre-installation \
103+
/docker-entrypoint-hooks.d/post-installation \
104+
/docker-entrypoint-hooks.d/pre-upgrade \
105+
/docker-entrypoint-hooks.d/post-upgrade \
106+
/docker-entrypoint-hooks.d/before-starting; \
102107
chown -R www-data:root /var/www; \
103108
chmod -R g=u /var/www
104109

Dockerfile-debian.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ RUN { \
111111
} > "${PHP_INI_DIR}/conf.d/nextcloud.ini"; \
112112
\
113113
mkdir /var/www/data; \
114+
mkdir -p /docker-entrypoint-hooks.d/pre-installation \
115+
/docker-entrypoint-hooks.d/post-installation \
116+
/docker-entrypoint-hooks.d/pre-upgrade \
117+
/docker-entrypoint-hooks.d/post-upgrade \
118+
/docker-entrypoint-hooks.d/before-starting; \
114119
chown -R www-data:root /var/www; \
115120
chmod -R g=u /var/www
116121

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,38 @@ To customize other PHP limits you can simply change the following variables:
202202
- `PHP_UPLOAD_LIMIT` (default `512M`) This sets the upload limit (`post_max_size` and `upload_max_filesize`) for big files. Note that you may have to change other limits depending on your client, webserver or operating system. Check the [Nextcloud documentation](https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/big_file_upload_configuration.html) for more information.
203203

204204

205+
## Auto configuration via hook folders
206+
207+
There are 5 hooks
208+
209+
- `pre-installation` Executed before the Nextcloud is installed/initiated
210+
- `post-installation` Executed after the Nextcloud is installed/initiated
211+
- `pre-upgrade` Executed before the Nextcloud is upgraded
212+
- `post-upgrade` Executed after the Nextcloud is upgraded
213+
- `before-starting` Executed before the Nextcloud starts
214+
215+
To use the hooks triggered by the `entrypoint` script, either
216+
- Added your script(s) to the individual of the hook folder(s), which are located at the path `/docker-entrypoint-hooks.d` in the container
217+
- Use volume(s) if you want to use script from the host system inside the container, see example.
218+
219+
**Note:** Only the script(s) located in a hook folder (not sub-folders), ending with `.sh` and marked as executable, will be executed.
220+
221+
**Example:** Mount using volumes
222+
```yaml
223+
...
224+
app:
225+
image: nextcloud:stable
226+
227+
volumes:
228+
- ./app-hooks/pre-installation:/docker-entrypoint-hooks.d/pre-installation
229+
- ./app-hooks/post-installation:/docker-entrypoint-hooks.d/post-installation
230+
- ./app-hooks/pre-upgrade:/docker-entrypoint-hooks.d/pre-upgrade
231+
- ./app-hooks/post-upgrade:/docker-entrypoint-hooks.d/post-upgrade
232+
- ./app-hooks/before-starting:/docker-entrypoint-hooks.d/before-starting
233+
...
234+
```
235+
236+
205237
## Using the apache image behind a reverse proxy and auto configure server host and protocol
206238

207239
The apache image will replace the remote addr (IP address visible to Nextcloud) with the IP address from `X-Real-IP` if the request is coming from a proxy in `10.0.0.0/8`, `172.16.0.0/12` or `192.168.0.0/16` by default. If you want Nextcloud to pick up the server host (`HTTP_X_FORWARDED_HOST`), protocol (`HTTP_X_FORWARDED_PROTO`) and client IP (`HTTP_X_FORWARDED_FOR`) from a trusted proxy, then disable rewrite IP and add the reverse proxy's IP address to `TRUSTED_PROXIES`.

docker-entrypoint.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,39 @@ run_as() {
1919
fi
2020
}
2121

22+
# Execute all executable files in a given directory in alphanumeric order
23+
run_path() {
24+
local hook_folder_path="/docker-entrypoint-hooks.d/$1"
25+
local return_code=0
26+
27+
echo "=> Searching for scripts (*.sh) to run, located in the folder: ${hook_folder_path}"
28+
29+
if [ -z "$(ls -A "${hook_folder_path}")" ]; then
30+
echo "==> but the hook folder \"$(basename "${hook_folder_path}")\" is empty, so nothing to do"
31+
return 0
32+
fi
33+
34+
(
35+
for script_file_path in "${hook_folder_path}/"*.sh; do
36+
if ! [ -x "${script_file_path}" ] && [ -f "${script_file_path}" ]; then
37+
echo "==> The script \"${script_file_path}\" in the folder \"${hook_folder_path}\" was skipping, because it didn't have the executable flag"
38+
continue
39+
fi
40+
41+
echo "==> Running the script (cwd: $(pwd)): \"${script_file_path}\""
42+
43+
run_as "${script_file_path}" || return_code="$?"
44+
45+
if [ "${return_code}" -ne "0" ]; then
46+
echo "==> Failed at executing \"${script_file_path}\". Exit code: ${return_code}"
47+
exit 1
48+
fi
49+
50+
echo "==> Finished the script: \"${script_file_path}\""
51+
done
52+
)
53+
}
54+
2255
# usage: file_env VAR [DEFAULT]
2356
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
2457
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
@@ -182,6 +215,8 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP
182215
fi
183216

184217
if [ "$install" = true ]; then
218+
run_path pre-installation
219+
185220
echo "Starting nextcloud installation"
186221
max_retries=10
187222
try=0
@@ -204,19 +239,24 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP
204239
NC_TRUSTED_DOMAIN_IDX=$((NC_TRUSTED_DOMAIN_IDX+1))
205240
done
206241
fi
242+
243+
run_path post-installation
207244
else
208245
echo "Please run the web-based installer on first connect!"
209246
fi
210247
fi
211248
# Upgrade
212249
else
250+
run_path pre-upgrade
251+
213252
run_as 'php /var/www/html/occ upgrade'
214253

215254
run_as 'php /var/www/html/occ app:list' | sed -n "/Enabled:/,/Disabled:/p" > /tmp/list_after
216255
echo "The following apps have been disabled:"
217256
diff /tmp/list_before /tmp/list_after | grep '<' | cut -d- -f2 | cut -d: -f1
218257
rm -f /tmp/list_before /tmp/list_after
219258

259+
run_path post-upgrade
220260
fi
221261

222262
echo "Initializing finished"
@@ -227,6 +267,8 @@ if expr "$1" : "apache" 1>/dev/null || [ "$1" = "php-fpm" ] || [ "${NEXTCLOUD_UP
227267
run_as 'php /var/www/html/occ maintenance:update:htaccess'
228268
fi
229269
) 9> /var/www/html/nextcloud-init-sync.lock
270+
271+
run_path before-starting
230272
fi
231273

232274
exec "$@"

0 commit comments

Comments
 (0)