## Cloud Run-ready Laravel container using webdevops/php-nginx
## Multi-stage build: composer + node build, then runtime

# ---------- Composer build stage ----------
FROM composer:2 AS composer_builder

ARG LARAVEL_VERSION=^11.0
WORKDIR /app

# Create Laravel skeleton during build
RUN composer create-project --prefer-dist laravel/laravel:"${LARAVEL_VERSION}" /app \
 && composer config sort-packages true \
 && composer require \
    livewire/livewire \
    laravel/sanctum \
    spatie/laravel-permission \
    maatwebsite/excel \
    guzzlehttp/guzzle \
    google/cloud-storage \
    league/flysystem-google-cloud-storage \
    intervention/image

# Copy overlay from repository to app (your domain code lives in app_src)
COPY app_src/ /app/

# Ensure proper permissions for storage and bootstrap cache
RUN chown -R www-data:www-data storage bootstrap/cache \
 && chmod -R ug+rwX storage bootstrap/cache

# ---------- Node/Vite build stage ----------
FROM node:20-alpine AS node_builder
WORKDIR /app
COPY --from=composer_builder /app /app

# Install if package.json exists (Laravel 11 ships with vite config)
RUN if [ -f package.json ]; then \
      npm ci --no-audit --no-fund; \
      npm run build; \
    fi

# ---------- Runtime stage ----------
FROM webdevops/php-nginx:8.3-alpine

ENV WEB_DOCUMENT_ROOT=/var/www/html/public \
    APP_ENV=production \
    APP_DEBUG=false \
    PHP_DATE_TIMEZONE=UTC \
    PORT=8080

# Required PHP extensions are already included in base image
WORKDIR /var/www/html

# Copy built application and frontend assets
COPY --from=node_builder /app /var/www/html

# Expose Cloud Run port
EXPOSE 8080

# Healthcheck (simple)
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
  CMD wget -qO- http://127.0.0.1:${PORT}/health || exit 1

# Add entrypoint to run one-time tasks (key generate, storage link, optional migrate)
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["supervisord"]


