|
1 |
| -#!/usr/bin/env sh |
| 1 | +#!/bin/sh |
2 | 2 | #
|
3 |
| -# Entrypoint for NestJS container. |
4 |
| -# - waits for the database |
5 |
| -# - runs migrations |
6 |
| -# - starts the app |
| 3 | +# This script is the entrypoint for the Docker container. |
| 4 | +# It ensures the database is ready before running migrations and starting the app. |
7 | 5 |
|
| 6 | +# Exit immediately if a command exits with a non-zero status. |
8 | 7 | set -e
|
9 | 8 |
|
10 |
| -# Color codes for output |
| 9 | +# Enable color output |
| 10 | +export FORCE_COLOR=1 |
| 11 | +export NODE_ENV=${NODE_ENV:-production} |
| 12 | + |
| 13 | +# The host for the database, read from an environment variable. |
| 14 | +# We'll set this to 'db' in the docker-compose.yml file. |
| 15 | +DB_HOST=${DATABASE_HOST:-db} |
| 16 | +DB_PORT=${DATABASE_PORT:-5432} |
| 17 | + |
| 18 | +# Colors for output |
11 | 19 | RED='\033[0;31m'
|
12 | 20 | GREEN='\033[0;32m'
|
13 | 21 | YELLOW='\033[1;33m'
|
14 | 22 | BLUE='\033[0;34m'
|
15 | 23 | NC='\033[0m' # No Color
|
16 | 24 |
|
17 |
| -export FORCE_COLOR=1 |
18 |
| -export NODE_ENV=${NODE_ENV:-production} |
| 25 | +echo -e "${BLUE}π³ Starting CodeBuilder application...${NC}" |
| 26 | +echo -e "${YELLOW}β³ Waiting for database at $DB_HOST:$DB_PORT to be ready...${NC}" |
19 | 27 |
|
20 |
| -DB_HOST=${DB_HOST:-db} |
21 |
| -DB_PORT=${DB_PORT:-5432} |
22 |
| - |
23 |
| -echo -e "${BLUE}π³ Starting NestJS application...${NC}" |
24 |
| -echo -e "${YELLOW}β³ Waiting for database at ${DB_HOST}:${DB_PORT}...${NC}" |
25 |
| - |
26 |
| -# Wait until the DB port is open |
| 28 | +# Loop until we can successfully connect to the database port. |
| 29 | +# nc (netcat) is a small utility perfect for this. |
27 | 30 | while ! nc -z $DB_HOST $DB_PORT; do
|
28 |
| - sleep 1 |
| 31 | + sleep 1 # wait for 1 second before trying again |
29 | 32 | done
|
30 | 33 |
|
31 | 34 | echo -e "${GREEN}β
Database is ready.${NC}"
|
32 |
| -echo -e "${YELLOW}π Running database migrations...${NC}" |
33 | 35 |
|
34 |
| -# run migrations (adjust script name if needed) |
35 |
| -pnpm run migration:run |
| 36 | +# Run Prisma migrations. |
| 37 | +# 'prisma migrate deploy' is the command intended for production/CI/CD environments. |
| 38 | +# It applies pending migrations without generating new ones. |
| 39 | +echo -e "${YELLOW}π Running database migrations...${NC}" |
| 40 | +npx prisma migrate deploy |
36 | 41 |
|
37 | 42 | echo -e "${GREEN}β
Migrations complete.${NC}"
|
38 |
| -echo -e "${BLUE}π Launching NestJS (prod)...${NC}" |
39 | 43 |
|
40 |
| -# Replace this shell with the main process (PID 1) |
41 |
| -exec node dist/main.js |
| 44 | +echo -e "${BLUE}π Starting NestJS application...${NC}" |
| 45 | + |
| 46 | +# Now, execute the main command provided to the container (e.g., "pnpm start"). |
| 47 | +# 'exec "$@"' replaces the shell process with the given command, |
| 48 | +# ensuring it becomes the main process (PID 1) and receives signals correctly. |
| 49 | +exec "$@" |
0 commit comments