DevOps

Laravel Dockerfile Generator

Scaffold a production-optimized Dockerfile for Laravel PHP applications with Composer dependency installation, Apache or PHP-FPM, and proper file permissions.

Options

# Stage 1: Composer
FROM composer:2.6 AS vendor
WORKDIR /app
COPY database/ database/
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# Stage 2: Runner
FROM php:8.2-fpm-alpine
WORKDIR /var/www/html
COPY . .
COPY --from=vendor /app/vendor/ ./vendor/
ENV NODE_ENV=production
EXPOSE 3000
RUN chown -R www-data:www-data /var/www/html\nUSER www-data\nCMD ["php-fpm"]

Laravel Dockerfile Generator: Complete Guide

Laravel Multi-Stage Dockerfile with Composer

Laravel applications depend on Composer for package management. Using a multi-stage build with the official Composer image ensures dependencies are installed in an isolated environment, and only the final vendor directory is copied into the production image.

# Stage 1: Composer
FROM composer:2.6 AS vendor
WORKDIR /app
COPY database/ database/
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# Stage 2: Runner
FROM php:8.2-apache
WORKDIR /var/www/html
COPY . .
COPY --from=vendor /app/vendor/ ./vendor/
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
EXPOSE 80
CMD ["apache2-foreground"]

How to Use

  1. 1

    Runtime Pre-Selected

    This page has pre-configured the generator for Laravel. Adjust port, base image, and environment variables as needed.

  2. 2

    Toggle Multi-Stage Build

    Enable multi-stage builds to separate the build environment from the runtime, drastically reducing image size.

  3. 3

    Enable Non-Root Security

    Check the non-root user option to run your container as a non-privileged user for production security.

  4. 4

    Copy & Use

    Switch between Dockerfile, docker-compose.yml, and .dockerignore tabs to copy each file.

Frequently Asked Questions

How do I Dockerize a Laravel application?+

Use a multi-stage Dockerfile: Stage 1 uses the Composer image to install PHP dependencies. Stage 2 copies the vendor directory into a PHP Apache or FPM image, sets proper permissions for the storage and bootstrap/cache directories, and serves the application.

Should I use PHP-FPM or Apache for Laravel in Docker?+

PHP-FPM paired with Nginx is generally recommended for production (better performance and resource usage). PHP Apache is simpler to set up and works well for smaller applications. Our generator supports both options via the base image selector.