Skip to content

Commit cd98e75

Browse files
committed
Run envsubst on container start to make templating easier
This entrypoint script will look for files in /etc/nginx/templates/ with a ".conf.template" suffix, run them through envsubst replacing defined environment variables, and output to /etc/nginx/conf.d/ preserving file name sans .template. Fixes nginx#232.
1 parent 47f11ab commit cd98e75

16 files changed

+264
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
ME=$(basename $0)
6+
7+
auto_envsubst() {
8+
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
9+
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
10+
local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
11+
12+
local template defined_envs relative_path output_path subdir
13+
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
14+
[ -d "$template_dir" ] || return 0
15+
if [ ! -w "$output_dir" ]; then
16+
echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
17+
return 0
18+
fi
19+
for template in $(find "$template_dir" -follow -name "*$suffix"); do
20+
relative_path="${template#$template_dir/}"
21+
output_path="$output_dir/${relative_path%$suffix}"
22+
subdir=$(dirname "$relative_path")
23+
# create a subdirectory where the template file exists
24+
mkdir -p "$output_dir/$subdir"
25+
echo "$ME: Running envsubst on $template to $output_path"
26+
envsubst "$defined_envs" < "$template" > "$output_path"
27+
done
28+
}
29+
30+
auto_envsubst
31+
32+
exit 0

mainline/alpine-perl/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ RUN set -x \
110110

111111
COPY docker-entrypoint.sh /
112112
COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
113+
COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
113114
ENTRYPOINT ["/docker-entrypoint.sh"]
114115

115116
EXPOSE 80
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
ME=$(basename $0)
6+
7+
auto_envsubst() {
8+
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
9+
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
10+
local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
11+
12+
local template defined_envs relative_path output_path subdir
13+
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
14+
[ -d "$template_dir" ] || return 0
15+
if [ ! -w "$output_dir" ]; then
16+
echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
17+
return 0
18+
fi
19+
for template in $(find "$template_dir" -follow -name "*$suffix"); do
20+
relative_path="${template#$template_dir/}"
21+
output_path="$output_dir/${relative_path%$suffix}"
22+
subdir=$(dirname "$relative_path")
23+
# create a subdirectory where the template file exists
24+
mkdir -p "$output_dir/$subdir"
25+
echo "$ME: Running envsubst on $template to $output_path"
26+
envsubst "$defined_envs" < "$template" > "$output_path"
27+
done
28+
}
29+
30+
auto_envsubst
31+
32+
exit 0

mainline/alpine/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ RUN set -x \
109109

110110
COPY docker-entrypoint.sh /
111111
COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
112+
COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
112113
ENTRYPOINT ["/docker-entrypoint.sh"]
113114

114115
EXPOSE 80
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
ME=$(basename $0)
6+
7+
auto_envsubst() {
8+
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
9+
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
10+
local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
11+
12+
local template defined_envs relative_path output_path subdir
13+
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
14+
[ -d "$template_dir" ] || return 0
15+
if [ ! -w "$output_dir" ]; then
16+
echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
17+
return 0
18+
fi
19+
for template in $(find "$template_dir" -follow -name "*$suffix"); do
20+
relative_path="${template#$template_dir/}"
21+
output_path="$output_dir/${relative_path%$suffix}"
22+
subdir=$(dirname "$relative_path")
23+
# create a subdirectory where the template file exists
24+
mkdir -p "$output_dir/$subdir"
25+
echo "$ME: Running envsubst on $template to $output_path"
26+
envsubst "$defined_envs" < "$template" > "$output_path"
27+
done
28+
}
29+
30+
auto_envsubst
31+
32+
exit 0

mainline/buster-perl/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ RUN set -x \
101101

102102
COPY docker-entrypoint.sh /
103103
COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
104+
COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
104105
ENTRYPOINT ["/docker-entrypoint.sh"]
105106

106107
EXPOSE 80
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
ME=$(basename $0)
6+
7+
auto_envsubst() {
8+
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
9+
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
10+
local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
11+
12+
local template defined_envs relative_path output_path subdir
13+
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
14+
[ -d "$template_dir" ] || return 0
15+
if [ ! -w "$output_dir" ]; then
16+
echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
17+
return 0
18+
fi
19+
for template in $(find "$template_dir" -follow -name "*$suffix"); do
20+
relative_path="${template#$template_dir/}"
21+
output_path="$output_dir/${relative_path%$suffix}"
22+
subdir=$(dirname "$relative_path")
23+
# create a subdirectory where the template file exists
24+
mkdir -p "$output_dir/$subdir"
25+
echo "$ME: Running envsubst on $template to $output_path"
26+
envsubst "$defined_envs" < "$template" > "$output_path"
27+
done
28+
}
29+
30+
auto_envsubst
31+
32+
exit 0

mainline/buster/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ RUN set -x \
100100

101101
COPY docker-entrypoint.sh /
102102
COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
103+
COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
103104
ENTRYPOINT ["/docker-entrypoint.sh"]
104105

105106
EXPOSE 80
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
ME=$(basename $0)
6+
7+
auto_envsubst() {
8+
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
9+
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
10+
local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
11+
12+
local template defined_envs relative_path output_path subdir
13+
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
14+
[ -d "$template_dir" ] || return 0
15+
if [ ! -w "$output_dir" ]; then
16+
echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable, exiting" 1>&2
17+
return 0
18+
fi
19+
for template in $(find "$template_dir" -follow -name "*$suffix"); do
20+
relative_path="${template#$template_dir/}"
21+
output_path="$output_dir/${relative_path%$suffix}"
22+
subdir=$(dirname "$relative_path")
23+
# create a subdirectory where the template file exists
24+
mkdir -p "$output_dir/$subdir"
25+
echo "$ME: Running envsubst on $template to $output_path"
26+
envsubst "$defined_envs" < "$template" > "$output_path"
27+
done
28+
}
29+
30+
auto_envsubst
31+
32+
exit 0

stable/alpine-perl/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ RUN set -x \
110110

111111
COPY docker-entrypoint.sh /
112112
COPY 10-listen-on-ipv6-by-default.sh /docker-entrypoint.d
113+
COPY 20-envsubst-on-templates.sh /docker-entrypoint.d
113114
ENTRYPOINT ["/docker-entrypoint.sh"]
114115

115116
EXPOSE 80

0 commit comments

Comments
 (0)