fix(laravel): apply runtime DB config and startup entrypoint
Laravel images no longer bake config:cache at build time, which ignored K8s DB env vars. Add DB_CONNECTION, clear config on boot, generate APP_KEY when missing, run migrations, and rebuild caches at container start. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1009,7 +1009,7 @@ RUN composer dump-autoload --optimize --no-dev
|
||||
# --- Production stage ---
|
||||
FROM php:${phpVersion}-fpm-alpine
|
||||
|
||||
RUN apk add --no-cache nginx supervisor curl \\
|
||||
RUN apk add --no-cache nginx supervisor curl openssl \\
|
||||
&& docker-php-ext-install pdo pdo_mysql opcache \\
|
||||
&& docker-php-ext-install pdo_pgsql 2>/dev/null || true
|
||||
|
||||
@@ -1063,14 +1063,20 @@ RUN [ -f docker/supervisord.conf ] && cp docker/supervisord.conf /etc/supervisor
|
||||
# Ensure storage and cache directories exist and are writable
|
||||
RUN mkdir -p storage/logs storage/framework/cache storage/framework/sessions storage/framework/views bootstrap/cache \\
|
||||
&& chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
|
||||
RUN php artisan config:cache && php artisan route:cache && php artisan view:cache || true
|
||||
RUN php artisan storage:link || true
|
||||
|
||||
RUN echo '#!/bin/sh' > /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'set -e' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'cd /var/www/html' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'mkdir -p storage/logs storage/framework/cache storage/framework/sessions storage/framework/views' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'chown -R www-data:www-data storage' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'mkdir -p storage/logs storage/framework/cache storage/framework/sessions storage/framework/views storage/app/public bootstrap/cache' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'chown -R www-data:www-data storage bootstrap/cache' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'if [ -z "$APP_KEY" ] || [ "$APP_KEY" = "null" ]; then export APP_KEY="base64:$(openssl rand -base64 32)"; fi' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'php artisan config:clear || true' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'php artisan cache:clear || true' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'php artisan migrate --force --no-interaction || true' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'php artisan storage:link || true' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'php artisan config:cache || true' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'php artisan route:cache || true' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'php artisan view:cache || true' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
echo 'exec /usr/bin/supervisord -c /etc/supervisord.conf' >> /usr/local/bin/cloudhost-laravel-entrypoint.sh && \\
|
||||
chmod +x /usr/local/bin/cloudhost-laravel-entrypoint.sh
|
||||
|
||||
|
||||
@@ -435,7 +435,14 @@ export class KubernetesService implements OnModuleInit {
|
||||
(ctx.runtime === AppRuntime.LARAVEL || ctx.runtime === AppRuntime.PHP) &&
|
||||
ctx.databaseType !== DatabaseType.NONE
|
||||
) {
|
||||
const dbConnection =
|
||||
ctx.databaseType === DatabaseType.POSTGRESQL
|
||||
? 'pgsql'
|
||||
: ctx.databaseType === DatabaseType.MONGODB
|
||||
? 'mongodb'
|
||||
: 'mysql';
|
||||
extraEnv.push(
|
||||
{ name: 'DB_CONNECTION', value: dbConnection },
|
||||
{ name: 'DB_DATABASE', value: ctx.appName.replace(/-/g, '_') },
|
||||
{ name: 'DB_USERNAME', valueFrom: { secretKeyRef: { name: `${ctx.appName}-db-secret`, key: 'username' } } },
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user