Skip to content

Commit 1eb57d6

Browse files
committed
feat(docker): add FORCE_PERMISSIONS variable to set permissions on data files
1 parent 5029911 commit 1eb57d6

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { sql } from 'drizzle-orm'
2+
import { getDB } from './db'
3+
4+
async function checkConnection() {
5+
try {
6+
const db = await getDB()
7+
await db.execute(sql`SELECT 1`)
8+
console.log('Database is ready and accepting queries!')
9+
process.exit(0)
10+
} catch (error: any) {
11+
console.error(`Database check failed: ${error.message}`)
12+
process.exit(1)
13+
}
14+
}
15+
16+
checkConnection()

scripts/docker-entrypoint.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -eu
33

44
: "${PUID:?PUID is not defined}"
55
: "${PGID:?PGID is not defined}"
6+
: "${FORCE_PERMISSIONS:=false}"
67

78
# Create syncin group if needed
89
group_name=$(getent group "${PGID}" | cut -d: -f1)
@@ -24,9 +25,11 @@ chown "${PUID}:${PGID}" /app
2425
CURRENT_UID=$(stat -c '%u' /app/data)
2526
CURRENT_GID=$(stat -c '%g' /app/data)
2627

27-
if [ "${CURRENT_UID}" != "${PUID}" ] || [ "${CURRENT_GID}" != "${PGID}" ]; then
28+
if [ "${CURRENT_UID}" != "${PUID}" ] || [ "${CURRENT_GID}" != "${PGID}" ] || [ "${FORCE_PERMISSIONS}" = "true" ]; then
2829
chown -R "${PUID}:${PGID}" /app/data
2930
fi
3031

32+
umask 027
33+
3134
# Launch server as syncin user
3235
exec su-exec "${PUID}:${PGID}" "$@"

scripts/docker-sync-in-server.sh

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@
22

33
if [ "${SKIP_INIT}" != "true" ]; then
44
if [ ! -f .init ]; then
5-
# wait for database
6-
sleep 8
5+
echo "Waiting for database to be ready..."
6+
MAX_RETRIES=30
7+
COUNT=0
8+
CONNECTED=false
9+
10+
while [ $COUNT -lt $MAX_RETRIES ]; do
11+
COUNT=$((COUNT+1))
12+
echo "Database connection attempt ${COUNT}/${MAX_RETRIES}..."
13+
if OUTPUT=$(node server/infrastructure/database/scripts/check-db.js 2>&1); then
14+
CONNECTED=true
15+
break
16+
fi
17+
sleep 1
18+
done
19+
20+
if [ "$CONNECTED" = "false" ]; then
21+
echo "Error: Timeout waiting for database after ${MAX_RETRIES} attempts:"
22+
echo "$OUTPUT"
23+
exit 1
24+
fi
25+
726
# migrate database
827
if ! npx drizzle-kit migrate --config=server/infrastructure/database/configuration.js; then
928
echo "Error: unable to migrate database schema !" >&2
@@ -19,9 +38,15 @@ if [ "${SKIP_INIT}" != "true" ]; then
1938
fi
2039
fi
2140
touch .init
22-
chmod -R 755 /app/data
41+
chmod 750 /app/data
2342
fi
2443
else
2544
echo "SKIP_INIT invoked"
2645
fi
46+
47+
if [ "${FORCE_PERMISSIONS}" = "true" ]; then
48+
echo "FORCE_PERMISSIONS: Applying recursive permissions (Dirs: 750, Files: 640)..."
49+
chmod -R u=rwX,g=rX,o= /app/data
50+
fi
51+
2752
exec node server/main.js

0 commit comments

Comments
 (0)