Skip to content

Commit b35c7c6

Browse files
author
Bryan Latten
committed
Merge pull request #6 from bryanlatten/master
Run.d: allow hooks for processes during container start
2 parents ca628c8 + cd24f4a commit b35c7c6

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ Variable | Example | Description
77
`SERVER_MAX_BODY_SIZE` | `SERVER_MAX_BODY_SIZE=4M` | Allows the downstream application to specify a non-default `client_max_body_size` configuration for the `server`-level directive in `/etc/nginx/sites-available/default`
88
`SERVER_INDEX` | `SERVER_INDEX index.html index.html index.php` | Changes the default pages to hit for folder and web roots
99
`SERVER_APP_NAME` | `APP_NAME='view'` | Sets a kv pair to be consumed by logging service for easy parsing and searching
10+
11+
12+
### Runtime Commands
13+
14+
To inject things into the runtime process, add shell scripts (ending in .sh) into the
15+
`/run.d` folder. These will be executed during container start.

container/root/run.d/10-nginx.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
CONFIG_SITE=/etc/nginx/sites-available/default
3+
CONFIG_SERVER=/etc/nginx/nginx.conf
4+
5+
echo '[nginx] setting sensible defaults'
6+
7+
# Configure nginx to use as many workers as there are cores for the running container
8+
sed -i "s/worker_processes [0-9]\+/worker_processes $(nproc)/" $CONFIG_SERVER
9+
sed -i "s/worker_connections [0-9]\+/worker_connections 1024/" $CONFIG_SERVER
10+
11+
echo '[nginx] piping logs to STDOUT'
12+
# Ensure nginx is configured to write logs to STDOUT
13+
# Also set log_format to output SERVER_APP_NAME placeholder
14+
sed -i "s/access_log [a-z\/\.\;]\+/ log_format main \'\$remote_addr - \$remote_user [\$time_local] \"\$request\" \$status \$bytes_sent \"\$http_referer\" \"\$http_user_agent\" ${SERVER_APP_NAME}\';\n access_log \/dev\/stdout main;\n/" $CONFIG_SERVER
15+
sed -i "s/error_log [a-z\/\.\ \;]\+/error_log \/dev\/stdout info;/" $CONFIG_SERVER
16+
17+
if [[ $SERVER_MAX_BODY_SIZE ]]
18+
then
19+
echo "[nginx] server client max body is ${SERVER_MAX_BODY_SIZE}"
20+
sed -i "s/client_max_body_size 1m/client_max_body_size ${SERVER_MAX_BODY_SIZE}/" $CONFIG_SITE
21+
fi
22+
23+
if [[ $SERVER_INDEX ]]
24+
then
25+
echo "[nginx] server index is ${SERVER_INDEX}"
26+
sed -i "s/index index.html index.htm/index ${SERVER_INDEX}/" $CONFIG_SITE
27+
fi

container/root/run.d/README

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Runtime commands
2+
---
3+
4+
Allows children to inject things into the run process.
5+
6+
Drop bash scripts in here to be executed by entrypoint during startup.

container/root/run.sh

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
#!/bin/bash
2-
CONFIG_SITE=/etc/nginx/sites-available/default
3-
CONFIG_SERVER=/etc/nginx/nginx.conf
2+
RUN_SCRIPTS=/run.d
43

5-
echo '[nginx] setting sensible defaults'
6-
7-
# Configure nginx to use as many workers as there are cores for the running container
8-
sed -i "s/worker_processes [0-9]\+/worker_processes $(nproc)/" $CONFIG_SERVER
9-
sed -i "s/worker_connections [0-9]\+/worker_connections 1024/" $CONFIG_SERVER
10-
11-
echo '[nginx] piping logs to STDOUT'
12-
# Ensure nginx is configured to write logs to STDOUT
13-
# Also set log_format to output SERVER_APP_NAME placeholder
14-
sed -i "s/access_log [a-z\/\.\;]\+/ log_format main \'\$remote_addr - \$remote_user [\$time_local] \"\$request\" \$status \$bytes_sent \"\$http_referer\" \"\$http_user_agent\" ${SERVER_APP_NAME}\';\n access_log \/dev\/stdout main;\n/" $CONFIG_SERVER
15-
sed -i "s/error_log [a-z\/\.\ \;]\+/error_log \/dev\/stdout info;/" $CONFIG_SERVER
16-
17-
if [[ $SERVER_MAX_BODY_SIZE ]]
18-
then
19-
echo "[nginx] server client max body is ${SERVER_MAX_BODY_SIZE}"
20-
sed -i "s/client_max_body_size 1m/client_max_body_size ${SERVER_MAX_BODY_SIZE}/" $CONFIG_SITE
21-
fi
22-
23-
if [[ $SERVER_INDEX ]]
24-
then
25-
echo "[nginx] server index is ${SERVER_INDEX}"
26-
sed -i "s/index index.html index.htm/index ${SERVER_INDEX}/" $CONFIG_SITE
27-
fi
4+
# Run shell scripts (ending in .sh) in run.d directory, if any
5+
for file in $RUN_SCRIPTS/*.sh; do
6+
echo "[run.d] executing ${file}"
7+
/bin/bash $file
8+
done
289

2910
echo "[nginx] starting (foreground)"
3011
exec /usr/sbin/nginx -g "daemon off;"

0 commit comments

Comments
 (0)