Add Docker and Nginx configuration for Cloud Run deployment

This commit is contained in:
Frank John Begornia
2025-08-12 00:28:17 +08:00
parent f197490606
commit 88912ee8e3
8 changed files with 353 additions and 44 deletions

View File

@@ -1,46 +1,81 @@
# Use the official PHP image based on Alpine Linux
FROM php:5.6-fpm-alpine
# Build stage
FROM php:5.6-fpm-alpine as composer
# Install system dependencies and PHP extensions
RUN apk --update --no-cache add \
nginx \
libpng-dev \
libjpeg-turbo-dev \
freetype-dev \
libzip-dev \
# Install system dependencies and PHP extensions required for Composer
RUN apk add --no-cache \
git \
curl \
zip \
unzip \
libmcrypt \
libmcrypt-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd pdo pdo_mysql zip mcrypt
zlib-dev \
libzip-dev \
autoconf \
make \
gcc \
g++ \
&& docker-php-ext-install mcrypt mbstring zip \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& composer self-update --1
ENV COMPOSER_ALLOW_SUPERUSER=1
WORKDIR /app
COPY composer.* ./
# Install dependencies with Composer (optimize later after full copy)
RUN composer config platform.php 5.6.40 \
&& composer install --prefer-dist --no-dev --no-scripts --no-autoloader
# Copy the rest of the application and optimize autoload
COPY . .
RUN composer dump-autoload --optimize --no-dev --classmap-authoritative
# Production stage
FROM php:5.6-fpm-alpine
# Install runtime dependencies & build PHP extensions
RUN apk add --no-cache \
nginx \
curl \
libpng \
libjpeg-turbo \
freetype \
libzip \
libmcrypt \
zlib \
&& apk add --no-cache --virtual .build-deps \
autoconf make gcc g++ \
libpng-dev libjpeg-turbo-dev freetype-dev libzip-dev libmcrypt-dev zlib-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j"$(nproc)" gd pdo pdo_mysql zip mcrypt mbstring opcache \
&& docker-php-ext-enable mcrypt \
&& apk del .build-deps \
&& php -m | grep -i mcrypt
# Configure PHP for production
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
&& echo "opcache.enable=1" >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo "opcache.memory_consumption=128" >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo "opcache.interned_strings_buffer=8" >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo "opcache.max_accelerated_files=4000" >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo "opcache.revalidate_freq=60" >> /usr/local/etc/php/conf.d/opcache.ini \
&& echo "opcache.fast_shutdown=1" >> /usr/local/etc/php/conf.d/opcache.ini
# Set the working directory in the container
WORKDIR /var/www
# Clear cache
# RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Copy the Laravel application files to the container
# Copy vendor & app code from build stage
COPY --from=composer /app/vendor ./vendor
COPY . .
# Set appropriate permissions for Laravel storage and bootstrap cache
RUN chown -R www-data:www-data storage bootstrap
# Set appropriate permissions and create required directories
RUN chown -R www-data:www-data storage bootstrap \
&& mkdir -p /run/php \
&& chown www-data:www-data /run/php \
&& php artisan key:generate || true
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Healthcheck (FPM listens on 9000; adjust as needed if behind nginx)
HEALTHCHECK --interval=30s --timeout=5s CMD php -m > /dev/null || exit 1
# Install Laravel dependencies
RUN composer install --no-plugins --no-scripts
# Generate Laravel application key
RUN php artisan key:generate
# Create directory for the socket and set permissions
RUN mkdir -p /run/php && chown www-data:www-data /run/php
# Copy the www.conf file to PHP-FPM pool.d directory
# COPY www.conf /usr/local/etc/php-fpm.d/www.conf
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]