Overhaul image.

This commit is contained in:
redxef 2022-03-07 17:58:18 +01:00
parent af9cc470fb
commit 86dcac05e9
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
6 changed files with 60 additions and 50 deletions

5
.dockerignore Normal file
View file

@ -0,0 +1,5 @@
.gitignore
example/
arch.txt
buildx.sh
readme.md

3
.gitignore vendored
View file

@ -1,5 +1,2 @@
arch.txt arch.txt
buildx.sh buildx.sh
nginx.conf
server.conf
index.html

View file

@ -1,11 +1,10 @@
FROM alpine:latest FROM alpine:latest
RUN apk update && apk upgrade && apk add nginx gettext && \ RUN apk add --upgrade --no-cache nginx gettext inotify-tools \
rm -r /etc/nginx && mv /etc/nginx /etc/nginx.tmpl \
&& touch /etc/envsubst.conf
COPY start-nginx.sh /usr/local/bin/ COPY start-nginx.sh /usr/local/bin/
COPY environment_variables.txt /
USER root
ENTRYPOINT ["/bin/sh", "-c"] ENTRYPOINT ["/bin/sh", "-c"]
CMD ["start-nginx.sh"] CMD ["start-nginx.sh"]

View file

@ -1 +0,0 @@
${DOMAIN_NAME}

View file

@ -3,26 +3,23 @@
A simple docker image for configuring nginx with environment variables. A simple docker image for configuring nginx with environment variables.
## Usage ## Usage
This image does not ship with a default configuration. Mount you whole nginx configuration into /etc/nginx.tmpl.
To get started place your nginx config in `/etc/nginx/nginx.conf` The configuration files can contain environment variables compatible
and your normal server configurations in `/etc/nginx/sites-enabled/`. with `envsubst`, list these variables in a file `/etc/envsubst.conf`.
Additionally, the server gets reloaded when a update to one of the
Every configuration file in `/etc/nginx/sites-available/` will get referenced certificates happens.
passed to `envsubst` and written to `/etc/nginx/sites-enabled/`.
To specify which variables to substitute place a file
`/environment_variables.txt` in the docker container with all variables
which should be passed to envsubst.
## Minimal Example ## Minimal Example
```sh ```sh
docker run \ cd example
-v "$PWD/nginx.conf":/etc/nginx/nginx.conf \ docker run --rm -it \
-v "$PWD/server.conf":/etc/nginx/sites-available/server.conf \ -e DOMAIN_NAME=localhost \
-v "$PWD/index.html":/var/www/html/index.html \ -p 80:80 -p 443:443
-e DOMAIN_NAME=localhost -p 80:80 \ -v "$PWD/nginx.conf:/etc/nginx.tmpl/nginx.conf" \
redxef/nginx-envsubst:latest -v "$PWD/envsubst.conf:/etc/envsubst.conf" \
-v "$PWD/cert:/cert" \
redxef/nginx-envsubst
``` ```
## Source ## Source

View file

@ -1,35 +1,48 @@
#!/usr/bin/env sh #!/usr/bin/env sh
set -x pids=""
enable_server() { run_prog() {
for name in "$@"; do "$@" &
src_dir="/etc/nginx/sites-available" pids="$! $pids"
dst_dir="/etc/nginx/sites-enabled" }
mkdir -p "$dst_dir"
envsubst '${DOMAIN_NAME}' < "$src_dir/$name.conf" > "$dst_dir/$name.conf" trap_sig() {
printf '%s' "$pids" | while IFS= read -r pid; do
echo "pid=$pid"
kill -s $1 $pid
done done
} }
enable_ssh_server() { trap 'trap_sig TERM' SIGTERM
for name in "$@"; do
src_dir="/etc/nginx/ssh-available" srcdir=/etc/nginx.tmpl/
dst_dir="/etc/nginx/ssh-enabled" dstdir=/etc/nginx/
mkdir -p "$dst_dir"
envsubst '${DOMAIN_NAME}' < "$src_dir/$name.conf" > "$dst_dir/$name.conf" find "$srcdir" -type d | while read -r src_directory; do
dst_directory="$(echo "$src_directory" | sed "s|^$srcdir|$dstdir|")"
mkdir -p "$dst_directory"
done
find "$srcdir" -type f | while read -r src_file; do
dst_file="$(echo "$src_file" | sed "s|^$srcdir|$dstdir|")"
envsubst "$(cat /etc/envsubst.conf)" < "$src_file" > "$dst_file"
done
run_nginx() {
find "$dstdir"
nginx -g 'daemon off;'
}
run_inotifywait() {
while find "$dstdir" -type f -exec \
sed -En '/ssl_certificate/ s/^\s*ssl_certificate(_key)? (.*);.*$/\2/p' {} \; | sort | uniq | \
inotifywait --fromfile=-; do
nginx -s reload
done done
} }
sub_env_vars="$(cat /environment_variables.txt)" run_prog run_nginx
run_prog run_inotifywait
echo "Enabling servers" wait $pids
(
cd "/etc/nginx/sites-available" || exit $?
test -d "../sites-enabled" || mkdir "../sites-enabled"
for f in *.conf; do
envsubst "$sub_env_vars" < "$f" > "../sites-enabled/$f"
done
)
echo "Starting nginx"
exec nginx -g 'daemon off;' -c /etc/nginx/nginx.conf