-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
82 lines (67 loc) · 2.88 KB
/
entrypoint.sh
File metadata and controls
82 lines (67 loc) · 2.88 KB
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
#!/bin/sh
# Set timezone if TZ is provided
if [ -n "$TZ" ] && [ -f "/usr/share/zoneinfo/$TZ" ]; then
ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime && echo "$TZ" > /etc/timezone
echo "Timezone set to $TZ"
fi
# Default values
USER_ID=${PUID:-1000}
GROUP_ID=${PGID:-1000}
DOCKER_GID=${DOCKER_GID:-999}
# Detect host path for /cluster (DooD volume path resolution)
export HOST_CLUSTER_DIR=$(cat /proc/self/mountinfo | grep ' /cluster ' | cut -d ' ' -f 4)
if [ -z "$HOST_CLUSTER_DIR" ]; then
echo "Warning: Could not determine HOST_CLUSTER_DIR from /proc/self/mountinfo. /cluster may not be bind-mounted. Continuing..."
HOST_CLUSTER_DIR=""
fi
echo "Starting with UID: $USER_ID, GID: $GROUP_ID, DOCKER_GID: $DOCKER_GID, HOST_CLUSTER_DIR: $HOST_CLUSTER_DIR"
# Modify nodejs group if needed
if [ "$(id -g nextjs)" -ne "$GROUP_ID" ]; then
# Handle user/group duplication more safely
EXISTING_USER_WITH_UID=$(getent passwd "$USER_ID" | cut -d: -f1)
if [ -n "$EXISTING_USER_WITH_UID" ] && [ "$EXISTING_USER_WITH_UID" != "nextjs" ]; then
userdel -f "$EXISTING_USER_WITH_UID"
fi
EXISTING_GROUP_WITH_GID=$(getent group "$GROUP_ID" | cut -d: -f1)
if [ -n "$EXISTING_GROUP_WITH_GID" ] && [ "$EXISTING_GROUP_WITH_GID" != "nodejs" ]; then
groupdel "$EXISTING_GROUP_WITH_GID" || true
fi
groupmod -o -g "$GROUP_ID" nodejs
fi
# Modify nextjs user if needed
if [ "$(id -u nextjs)" -ne "$USER_ID" ]; then
usermod -o -u "$USER_ID" nextjs
fi
# Create docker group and add nextjs to it if it doesn't exist
DOCKER_GROUP_BY_GID=$(getent group "$DOCKER_GID" | cut -d: -f1)
if [ -n "$DOCKER_GROUP_BY_GID" ]; then
# Group with the target GID already exists, use its name
DOCKER_GROUP="$DOCKER_GROUP_BY_GID"
echo "Using existing group $DOCKER_GROUP with GID $DOCKER_GID"
else
# Group with target GID doesn't exist.
# Check if 'docker' name is already taken by another GID
if getent group docker > /dev/null; then
echo "Updating existing 'docker' group to GID $DOCKER_GID"
groupmod -o -g "$DOCKER_GID" docker
DOCKER_GROUP="docker"
else
echo "Creating 'docker' group with GID $DOCKER_GID"
groupadd -g "$DOCKER_GID" docker
DOCKER_GROUP="docker"
fi
fi
usermod -aG "$DOCKER_GROUP" nextjs
# Ensure /cluster exists and is writable by nextjs user
mkdir -p /cluster
# We only chown the directory itself to avoid long recursive operations
chown nextjs:nodejs /cluster 2>/dev/null || true
# If bind-mounted host dir is present, ensure ownership (best-effort)
if [ -n "$HOST_CLUSTER_DIR" ] && [ -d "$HOST_CLUSTER_DIR" ]; then
chown "$USER_ID":"$GROUP_ID" "$HOST_CLUSTER_DIR" 2>/dev/null || true
fi
echo "Checking permissions for /var/run/docker.sock..."
ls -l /var/run/docker.sock
echo "Execution identity: $(id nextjs)"
# Use gosu with only the username to ensure all supplementary groups are loaded
exec gosu nextjs "$@"