PHP Dockerfile
Dockerfile
# Use official PHP image with necessary extensions
FROM php:8.2-fpm
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
libonig-dev \
libzip-dev \
unzip \
git \
curl \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd mbstring zip pdo pdo_mysql exif
# Install Composer
COPY /usr/bin/composer /usr/bin/composer
# Copy application files
COPY . .
# Ensure .env exists
RUN cp .env.example .env || true
# Install dependencies
RUN composer install --no-dev --optimize-autoloader
# Set correct permissions
RUN chown -R www-data:www-data /var/www && \
find /var/www/ -type d -exec chmod 755 {} \; && \
find /var/www/ -type f -exec chmod 644 {} \; && \
chmod -R 775 storage bootstrap/cache database
# Switch to www-data user
USER www-data
# Generate application key
RUN php artisan key:generate
# Expose port 9000 for PHP-FPM
EXPOSE 9000
# Start PHP-FPM
CMD ["php-fpm"]
# Below is Optional if you want to add startup script
# COPY start.sh /start.sh
# RUN chmod 755 /start.sh
# ENTRYPOINT [ "./start.sh" ]
How toβ
Identify Laravel Requirementsβ
1. PHP version and libraryβ
Check composer.json
β require.php
or run command below
php -v
composer show --platform
PHP Extension (docker-php-ext-install ) | System Library (apt-get install ) |
---|---|
pdo_pgsql | libpq-dev |
zip | libzip-dev |
gd | libpng-dev , libjpeg-dev , libfreetype6-dev |
xml | libxml2-dev |
intl | libicu-dev |
2. PHP extensionsβ
script to know what extensions needed:
#!/bin/bash
# Make sure composer.lock file exist
if [ ! -f "composer.lock" ]; then
echo "Error: composer.lock not found!"
exit 1
fi
echo ""
# Extract required PHP extensions from composer.lock
EXTENSIONS=$(jq -r '.packages[].require | keys[] | select(startswith("ext-"))' composer.lock | sort -u | sed 's/"//g')
echo "Required Extensions :"
for EXT in $EXTENSIONS; do
echo "$EXT"
done
echo ""
Pros
You donβt need to install all extensions manually, as the PHP Docker image usually includes some by default. You can check it with docker run -it --rm [PHP_IMAGE] php -m
3. Trial and errorβ
Run composer install
and note missing extensions, Or Just copy your composer.json & .lock, then ask GPT
Select Base Imageβ
php:8.2-fpm
β For Laravel with Nginxphp:8.2-apache
β For Laravel with Apachephp:8.2-cli
β For CLI usage
Install Composer Dependenciesβ
COPY /usr/bin/composer /usr/bin/composer
RUN composer install --no-dev --optimize-autoloader
Set Up Laravel Permissionsβ
Laravel needs the storage/
and bootstrap/cache/
folders to be writable:
RUN chown -R www-data:www-data /var/www \
&& chmod -R 775 /var/www/storage \
&& chmod -R 775 /var/www/bootstrap/cache
Set Environment Variables & Optimizeβ
- Use
.env
file (via docker-compose). - Optimize Laravel for production:
RUN php artisan config:cache && php artisan route:cache
Startup Commandβ
CMD ["php-fpm"]
# or
CMD ["apache2-foreground"]