-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
70 lines (57 loc) · 2.41 KB
/
docker-entrypoint.sh
File metadata and controls
70 lines (57 loc) · 2.41 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
#!/bin/sh
#
# This script is the entrypoint for the Docker container.
# It ensures the database is ready before running migrations and starting the app.
# Exit immediately if a command exits with a non-zero status.
set -e
# Enable color output
export FORCE_COLOR=1
export NODE_ENV=${NODE_ENV:-production}
# Determine DB host/port for readiness checks.
# Prefer DATABASE_URL so the healthcheck matches what Prisma will use.
if [ -n "${DATABASE_URL:-}" ]; then
DB_URL=$(printf '%s' "$DATABASE_URL" | sed 's/^"//; s/"$//')
DB_HOSTPORT=$(printf '%s' "$DB_URL" | sed -E 's#^[a-zA-Z0-9+.-]+://[^@]+@([^/]+)/.*#\1#')
# If parsing failed (no '@' segment), fall back to explicit vars/defaults.
if [ "$DB_HOSTPORT" = "$DB_URL" ]; then
DB_HOST=${DATABASE_HOST:-db}
DB_PORT=${DATABASE_PORT:-5432}
else
DB_HOST_FROM_URL=${DB_HOSTPORT%%:*}
DB_PORT_FROM_URL=${DB_HOSTPORT##*:}
if [ "$DB_HOST_FROM_URL" = "$DB_HOSTPORT" ]; then
DB_PORT_FROM_URL=5432
fi
DB_HOST=${DATABASE_HOST:-$DB_HOST_FROM_URL}
DB_PORT=${DATABASE_PORT:-$DB_PORT_FROM_URL}
fi
else
# We'll set this to 'db' in docker-compose.yml, but default safely.
DB_HOST=${DATABASE_HOST:-db}
DB_PORT=${DATABASE_PORT:-5432}
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}🐳 Starting CodeBuilder application...${NC}"
echo -e "${YELLOW}⏳ Waiting for database at $DB_HOST:$DB_PORT to be ready...${NC}"
# Loop until we can successfully connect to the database port.
# nc (netcat) is a small utility perfect for this.
while ! nc -z "$DB_HOST" "$DB_PORT"; do
sleep 1 # wait for 1 second before trying again
done
echo -e "${GREEN}✅ Database is ready.${NC}"
# Run Prisma migrations.
# 'prisma migrate deploy' is the command intended for production/CI/CD environments.
# It applies pending migrations without generating new ones.
echo -e "${YELLOW}🔄 Running database migrations...${NC}"
npx prisma migrate deploy
echo -e "${GREEN}✅ Migrations complete.${NC}"
echo -e "${BLUE}🚀 Starting Next.js application...${NC}"
# Now, execute the main command provided to the container (e.g., "pnpm start").
# 'exec "$@"' replaces the shell process with the given command,
# ensuring it becomes the main process (PID 1) and receives signals correctly.
exec "$@"