-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-entrypoint.sh
85 lines (74 loc) · 2.95 KB
/
docker-entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh
export ENTRYPOINT_D="${ENTRYPOINT_D:-/etc/docker-entrypoint.d}"
export ENTRYPOINT_LOG_THRESHOLD="${ENTRYPOINT_LOG_THRESHOLD:-1}"
export ENTRYPOINT_RUN_AS_ROOT="${ENTRYPOINT_RUN_AS_ROOT:-}"
export ENTRYPOINT_SKIP_CONFIG="${ENTRYPOINT_SKIP_CONFIG:-}"
ENTRYPOINT_LOG_LEVELS="DBUG INFO WARN ERRO"
ENTRYPOINT_NAME="$(basename "$0")"
log () {
if [ "$1" -ge "$ENTRYPOINT_LOG_THRESHOLD" ]; then
LOG_LEVEL_NAME=$(echo "$ENTRYPOINT_LOG_LEVELS" | cut -d\ -f"$1")
echo >&2 "$(date "+%Y-%m-%d %H:%M:%S") $ENTRYPOINT_NAME ($LOG_LEVEL_NAME) $2"
fi
}
if [ -z "$ENTRYPOINT_RUN_AS_ROOT" ]; then
export DOCKER_GID="${DOCKER_GID:-23456}"
export DOCKER_UID="${DOCKER_UID:-12345}"
export DOCKER_GROUP="${DOCKER_GROUP:-user}"
export DOCKER_USER="${DOCKER_USER:-user}"
log 2 "Creating new group '$DOCKER_GROUP' with GID = $DOCKER_GID ..."
if addgroup --gid "$DOCKER_GID" --system "$DOCKER_GROUP"; then
log 1 " + Group created successfully."
else
EXISTING_GID=$(id -g "$DOCKER_GROUP")
log 3 " * Group already exists! Existing GID = $EXISTING_GID."
if [ "$EXISTING_GID" -ne "$DOCKER_GID" ]; then
log 4 " \`- Aborting due to GID mismatch."
exit 1
fi
fi
log 2 "Creating new user '$DOCKER_USER' with UID = $DOCKER_UID in group '$DOCKER_GROUP' ..."
if adduser --disabled-password --gecos "" --ingroup "$DOCKER_GROUP" \
--home "/home/$DOCKER_USER" --uid "$DOCKER_UID" "$DOCKER_USER"; then
log 1 " + User created successfully."
else
EXISTING_UID=$(id -u "$DOCKER_USER")
log 3 " * User already exists! Existing UID = $(id -u "$DOCKER_USER")."
if [ "$EXISTING_UID" -ne "$DOCKER_UID" ]; then
log 4 " \`- Aborting due to UID mismatch."
exit 1
fi
fi
fi
if [ -z "$ENTRYPOINT_SKIP_CONFIG" ]; then
if find "$ENTRYPOINT_D" -mindepth 1 -maxdepth 1 -print -quit 2>/dev/null | read -r _; then
log 2 "$ENTRYPOINT_D is not empty, attempting to perform configuration."
log 1 "Looking for shell scripts in $ENTRYPOINT_D ..."
find "$ENTRYPOINT_D" -follow -type f -print | sort -n | while read -r f; do
case "$f" in
*.sh)
if [ -x "$f" ]; then
log 1 " + Launching $f ...";
"$f"
else
log 3 " + Ignoring $f: not executable!";
fi
;;
*)
log 3 " + Ignoring $f: not a '.sh' script."
;;
esac
done
log 2 "Configuration complete."
else
log 2 "No files found in $ENTRYPOINT_D, skipping configuration."
fi
fi
STARTUP_USER="root"
if [ -z "$ENTRYPOINT_RUN_AS_ROOT" ]; then
STARTUP_USER="$DOCKER_USER"
export HOME="/home/$DOCKER_USER"
set -- su-exec "$DOCKER_USER" "$@"
fi
log 2 "Starting up as '$STARTUP_USER'!"
exec "$@"