Skip to content

Commit 6b94dda

Browse files
eriksjolundthresheek
authored andcommitted
docker-entrypoint: don't close fd 3.
The file descriptor 3 in the container might be an activated socket that was inherited from systemd via Podman and should therefore not be closed. Fixes nginx#702. Signed-off-by: Erik Sjölund <[email protected]>
1 parent 6675128 commit 6b94dda

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

entrypoint/10-listen-on-ipv6-by-default.sh

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,65 @@
33

44
set -e
55

6+
entrypoint_log() {
7+
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
8+
echo "$@"
9+
fi
10+
}
11+
612
ME=$(basename $0)
713
DEFAULT_CONF_FILE="etc/nginx/conf.d/default.conf"
814

915
# check if we have ipv6 available
1016
if [ ! -f "/proc/net/if_inet6" ]; then
11-
echo >&3 "$ME: info: ipv6 not available"
17+
entrypoint_log "$ME: info: ipv6 not available"
1218
exit 0
1319
fi
1420

1521
if [ ! -f "/$DEFAULT_CONF_FILE" ]; then
16-
echo >&3 "$ME: info: /$DEFAULT_CONF_FILE is not a file or does not exist"
22+
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE is not a file or does not exist"
1723
exit 0
1824
fi
1925

2026
# check if the file can be modified, e.g. not on a r/o filesystem
21-
touch /$DEFAULT_CONF_FILE 2>/dev/null || { echo >&3 "$ME: info: can not modify /$DEFAULT_CONF_FILE (read-only file system?)"; exit 0; }
27+
touch /$DEFAULT_CONF_FILE 2>/dev/null || { entrypoint_log "$ME: info: can not modify /$DEFAULT_CONF_FILE (read-only file system?)"; exit 0; }
2228

2329
# check if the file is already modified, e.g. on a container restart
24-
grep -q "listen \[::]\:80;" /$DEFAULT_CONF_FILE && { echo >&3 "$ME: info: IPv6 listen already enabled"; exit 0; }
30+
grep -q "listen \[::]\:80;" /$DEFAULT_CONF_FILE && { entrypoint_log "$ME: info: IPv6 listen already enabled"; exit 0; }
2531

2632
if [ -f "/etc/os-release" ]; then
2733
. /etc/os-release
2834
else
29-
echo >&3 "$ME: info: can not guess the operating system"
35+
entrypoint_log "$ME: info: can not guess the operating system"
3036
exit 0
3137
fi
3238

33-
echo >&3 "$ME: info: Getting the checksum of /$DEFAULT_CONF_FILE"
39+
entrypoint_log "$ME: info: Getting the checksum of /$DEFAULT_CONF_FILE"
3440

3541
case "$ID" in
3642
"debian")
3743
CHECKSUM=$(dpkg-query --show --showformat='${Conffiles}\n' nginx | grep $DEFAULT_CONF_FILE | cut -d' ' -f 3)
3844
echo "$CHECKSUM /$DEFAULT_CONF_FILE" | md5sum -c - >/dev/null 2>&1 || {
39-
echo >&3 "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
45+
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
4046
exit 0
4147
}
4248
;;
4349
"alpine")
4450
CHECKSUM=$(apk manifest nginx 2>/dev/null| grep $DEFAULT_CONF_FILE | cut -d' ' -f 1 | cut -d ':' -f 2)
4551
echo "$CHECKSUM /$DEFAULT_CONF_FILE" | sha1sum -c - >/dev/null 2>&1 || {
46-
echo >&3 "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
52+
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
4753
exit 0
4854
}
4955
;;
5056
*)
51-
echo >&3 "$ME: info: Unsupported distribution"
57+
entrypoint_log "$ME: info: Unsupported distribution"
5258
exit 0
5359
;;
5460
esac
5561

5662
# enable ipv6 on default.conf listen sockets
5763
sed -i -E 's,listen 80;,listen 80;\n listen [::]:80;,' /$DEFAULT_CONF_FILE
5864

59-
echo >&3 "$ME: info: Enabled listen on IPv6 in /$DEFAULT_CONF_FILE"
65+
entrypoint_log "$ME: info: Enabled listen on IPv6 in /$DEFAULT_CONF_FILE"
6066

6167
exit 0

entrypoint/20-envsubst-on-templates.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ set -e
44

55
ME=$(basename $0)
66

7+
entrypoint_log() {
8+
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
9+
echo "$@"
10+
fi
11+
}
12+
713
auto_envsubst() {
814
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
915
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
@@ -13,7 +19,7 @@ auto_envsubst() {
1319
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
1420
[ -d "$template_dir" ] || return 0
1521
if [ ! -w "$output_dir" ]; then
16-
echo >&3 "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
22+
entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
1723
return 0
1824
fi
1925
find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do
@@ -22,7 +28,7 @@ auto_envsubst() {
2228
subdir=$(dirname "$relative_path")
2329
# create a subdirectory where the template file exists
2430
mkdir -p "$output_dir/$subdir"
25-
echo >&3 "$ME: Running envsubst on $template to $output_path"
31+
entrypoint_log "$ME: Running envsubst on $template to $output_path"
2632
envsubst "$defined_envs" < "$template" > "$output_path"
2733
done
2834
}

entrypoint/docker-entrypoint.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,44 @@
33

44
set -e
55

6-
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
7-
exec 3>&1
8-
else
9-
exec 3>/dev/null
10-
fi
6+
entrypoint_log() {
7+
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
8+
echo "$@"
9+
fi
10+
}
1111

1212
if [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then
1313
if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then
14-
echo >&3 "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration"
14+
entrypoint_log "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration"
1515

16-
echo >&3 "$0: Looking for shell scripts in /docker-entrypoint.d/"
16+
entrypoint_log "$0: Looking for shell scripts in /docker-entrypoint.d/"
1717
find "/docker-entrypoint.d/" -follow -type f -print | sort -V | while read -r f; do
1818
case "$f" in
1919
*.envsh)
2020
if [ -x "$f" ]; then
21-
echo >&3 "$0: Sourcing $f";
21+
entrypoint_log "$0: Sourcing $f";
2222
source "$f"
2323
else
2424
# warn on shell scripts without exec bit
25-
echo >&3 "$0: Ignoring $f, not executable";
25+
entrypoint_log "$0: Ignoring $f, not executable";
2626
fi
2727
;;
2828
*.sh)
2929
if [ -x "$f" ]; then
30-
echo >&3 "$0: Launching $f";
30+
entrypoint_log "$0: Launching $f";
3131
"$f"
3232
else
3333
# warn on shell scripts without exec bit
34-
echo >&3 "$0: Ignoring $f, not executable";
34+
entrypoint_log "$0: Ignoring $f, not executable";
3535
fi
3636
;;
37-
*) echo >&3 "$0: Ignoring $f";;
37+
*) entrypoint_log "$0: Ignoring $f";;
3838
esac
3939
done
4040

41-
echo >&3 "$0: Configuration complete; ready for start up"
41+
entrypoint_log "$0: Configuration complete; ready for start up"
4242
else
43-
echo >&3 "$0: No files found in /docker-entrypoint.d/, skipping configuration"
43+
entrypoint_log "$0: No files found in /docker-entrypoint.d/, skipping configuration"
4444
fi
4545
fi
4646

0 commit comments

Comments
 (0)