Enhance Dockerfile configurations for API and frontend, including MySQL client installation, improved TypeScript build process, and a custom startup script for database migration. Update production environment example with refined database and application settings.
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
# ScriptShare API Dockerfile
|
||||
FROM node:18-alpine
|
||||
|
||||
# Install system dependencies for native modules
|
||||
RUN apk add --no-cache python3 make g++ libc6-compat curl
|
||||
# Install system dependencies for native modules and MySQL client
|
||||
RUN apk add --no-cache python3 make g++ libc6-compat curl mysql-client
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
@ -15,15 +15,45 @@ RUN npm ci --only=production=false
|
||||
# Copy source code
|
||||
COPY . .
|
||||
|
||||
# Build TypeScript
|
||||
RUN npx tsc --build
|
||||
# Copy database configuration files
|
||||
COPY src/lib/db/ src/lib/db/
|
||||
COPY drizzle.config.ts ./
|
||||
|
||||
# Build TypeScript with API-specific config
|
||||
RUN npm run build:api
|
||||
|
||||
# Create startup script for database migration and server start
|
||||
RUN cat > start.sh << 'EOF'
|
||||
#!/bin/sh
|
||||
echo "Starting ScriptShare API..."
|
||||
|
||||
# Wait for database to be ready
|
||||
echo "Waiting for database connection..."
|
||||
until mysqladmin ping -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" --silent; do
|
||||
echo "Database is unavailable - sleeping"
|
||||
sleep 2
|
||||
done
|
||||
|
||||
echo "Database is ready!"
|
||||
|
||||
# Run database migrations if needed
|
||||
echo "Running database migrations..."
|
||||
npm run db:migrate || echo "Migrations completed or not needed"
|
||||
|
||||
# Start the API server
|
||||
echo "Starting API server..."
|
||||
exec npm run start:api
|
||||
EOF
|
||||
|
||||
# Make startup script executable
|
||||
RUN chmod +x start.sh
|
||||
|
||||
# Expose port
|
||||
EXPOSE 3000
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
||||
# Health check that includes database connectivity
|
||||
HEALTHCHECK --interval=30s --timeout=15s --start-period=60s --retries=5 \
|
||||
CMD curl -f http://localhost:3000/api/health || exit 1
|
||||
|
||||
# Start the API server
|
||||
CMD ["npm", "run", "start:api"]
|
||||
# Start with our custom startup script
|
||||
CMD ["./start.sh"]
|
Reference in New Issue
Block a user