Mega_Aleksandar's avatar

Production php-fpm-alpine image for Laravel

Hello everyone,

Lary already helped me out with this, but I am just trying to make sure that all of the packages are in order for a php-fpm-alpine docker image for Laravel.

The image that Laravel currently uses is almost 1GB in size and I know it can go down significantly.

So, is there an official image for Laravel that uses php-fpm-alpine or list of packages that need to be installed on the php-fpm-alpine docker image for Laravel?

Thank you in advance.

0 likes
1 reply
LaryAI's avatar
Level 58

To create a production-ready php-fpm-alpine Docker image for Laravel, you can start with the official php:alpine image and then install the necessary extensions and dependencies required by Laravel. Below is a Dockerfile example that outlines the steps to achieve this:

# Use the official PHP image as a base
FROM php:8.1-fpm-alpine

# Set working directory
WORKDIR /var/www

# Install system dependencies
RUN apk --no-cache add \
    build-base \
    libpng-dev \
    libjpeg-turbo-dev \
    libwebp-dev \
    libxpm-dev \
    freetype-dev \
    libzip-dev \
    zip \
    unzip \
    git \
    bash \
    fcgi \
    libmcrypt-dev \
    oniguruma-dev \
    postgresql-dev

# Clear cache
RUN rm -rf /var/cache/apk/*

# Install PHP extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install gd \
    && docker-php-ext-install pdo pdo_mysql pdo_pgsql mbstring zip exif pcntl bcmath opcache

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www

# Change current user to www
USER www-data

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Explanation:

  1. Base Image: We start with the php:8.1-fpm-alpine image, which is a lightweight version of PHP with FPM (FastCGI Process Manager).

  2. Working Directory: Set the working directory to /var/www.

  3. System Dependencies: Install necessary system dependencies using apk. These include libraries for image processing, zip handling, and other utilities.

  4. Clear Cache: Remove the APK cache to reduce the image size.

  5. PHP Extensions: Install required PHP extensions for Laravel. This includes extensions for database handling, image processing, and other functionalities.

  6. Composer: Copy Composer from the official Composer image to the current image.

  7. Application Code: Copy the application code to the working directory and set the appropriate permissions.

  8. User: Change the current user to www-data for security reasons.

  9. Expose Port: Expose port 9000, which is the default port for PHP-FPM.

  10. CMD: Start the PHP-FPM server.

Building the Docker Image

To build the Docker image, navigate to the directory containing your Dockerfile and run:

docker build -t laravel-php-fpm-alpine .

Running the Docker Container

To run the Docker container, use the following command:

docker run -p 9000:9000 -v $(pwd):/var/www laravel-php-fpm-alpine

This setup should give you a much smaller Docker image compared to the default Laravel image, while still including all necessary dependencies and extensions for running a Laravel application.

1 like

Please or to participate in this conversation.