Building a Docker Image for PHP with Laravel Installer: A Complete Guide
When creating a Docker container for your PHP environment, ensuring it’s optimized for your Laravel projects can save significant time and effort. By integrating the Laravel installer into your Docker image, you’ll have a streamlined setup where you can directly scaffold new Laravel projects within your container. Here’s a step-by-step guide to building a PHP Docker image with the Laravel installer, along with additional tips and considerations.
Why Include the Laravel Installer in Your PHP Docker Image?
The Laravel installer is a convenient tool for bootstrapping new Laravel projects with a single command (laravel new <project-name>
). Including it in your Docker image eliminates the need to install it every time you start a container, boosting productivity.
Step 1: Base Dockerfile Setup
We begin by extending the official php:8.4-fpm
image and installing essential system dependencies. These include libraries and extensions required by PHP and Laravel, such as MySQL, PostgreSQL, GD for image manipulation, and Memcached.
Dockerfile Overview
Here’s the Dockerfile setup:
FROM php:8.4-fpm
# Install system dependencies
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y \
git \
zip \
unzip \
libmariadb-dev \
libpq-dev \
libfreetype-dev \
libjpeg62-turbo-dev \
libpng-dev \
libmemcached-dev \
zlib1g-dev \
libsqlite3-dev \
libssl-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd mysqli pdo_mysql pdo_pgsql pgsql pdo exif pdo_sqlite \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Key Points:
- PHP Extensions: Installing extensions like
pdo_mysql
,gd
, andpdo_pgsql
ensures compatibility with common Laravel packages. - System Libraries: Tools like
zip
,unzip
, andgit
are essential for Laravel and Composer operations.
Step 2: Adding Memcached Support
Memcached is commonly used for caching in Laravel. Here, we install the Memcached PHP extension from source:
# Install memcached from source
RUN curl -L -o php-memcached.tar.gz https://github.com/php-memcached-dev/php-memcached/archive/refs/tags/v3.3.0.tar.gz \
&& tar -xvzf php-memcached.tar.gz \
&& cd php-memcached-3.3.0 \
&& phpize \
&& ./configure \
&& make \
&& make install \
&& docker-php-ext-enable memcached
Key Consideration:
- Always ensure you’re using the latest stable version of extensions for compatibility and security.
Step 3: Installing Composer
Composer is the backbone of modern PHP development, and it’s a must-have for any Laravel project. Install Composer globally:
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Step 4: Adding Laravel Installer
With Composer installed, you can add the Laravel installer to your image:
# Install Laravel Installer globally
RUN composer global require laravel/installer \
&& echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> /root/.bashrc
Key Points:
- The Laravel installer is installed globally using Composer.
- The
PATH
update ensures that thelaravel
command is accessible from anywhere inside the container.
Step 5: Building and Verifying Your Image
- Build the Docker Image:
docker build -t php-laravel:latest .
- Run a Container from the Image:
docker run -it --rm php-laravel:latest bash
- Verify Laravel Installer: Inside the container, run:
laravel --version
You should see the Laravel installer version output.
Additional Considerations
- Set Up a Non-Root User: By default, PHP Docker containers run as the
root
user. This can cause file permission issues in your Laravel projects. Add a non-root user:
RUN useradd -ms /bin/bash laravel
USER laravel
- Install Node.js and npm: Many Laravel projects use tools like Vite for asset bundling. Consider adding Node.js:
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs
- Optimize Image Size: To reduce image size, consider using multi-stage builds:
FROM php:8.4-fpm AS base
# Install dependencies and build Laravel installer
RUN apt-get update && apt-get install -y git zip unzip ...
RUN composer global require laravel/installer
FROM php:8.4-fpm
COPY --from=base /root/.composer /root/.composer
- Environment Variables: Predefine common Laravel environment variables, like:
ENV APP_ENV=local \
APP_DEBUG=true \
APP_KEY=base64:...
Finally
By integrating the Laravel installer, Composer, and other essential tools into your PHP Docker image, you can create a robust, reusable development environment tailored for Laravel projects. Don’t forget to address potential issues like file permissions and include additional tools like Node.js for a more comprehensive setup.
With this guide, you now have everything you need to start building your PHP containers optimized for Laravel development.
Comments ()