Laravel Docker Examples

repository·main·Indexed 43 days ago

https://github.com/dockersamples/laravel-docker-examples

A collection of modular Docker configurations for Laravel applications, featuring optimized setups for local development and production deployment. The stack includes PHP 8.5 FPM, Node.js 22.x, PostgreSQL 16, Redis, and Nginx, orchestrated via Docker Compose.

Tokens
1.6K
Snippets
4
Records
7
Agent score
5%

What's inside Laravel Docker Examples

  1. Understand the project structure and environments

    main

    The project uses a modular directory structure to separate shared configurations from environment-specific ones.

    Directory Layout

    • app/: The standard Laravel application directory.
    • docker/common/: Shared Docker configurations used by both environments.
    • docker/development/: Configurations specific to the development workflow (e.g., Xdebug, volume mounts).
    • docker/production/: Configurations optimized for production (e.g., minimal images, security hardening).
    • compose.dev.yaml: The Docker Compose file for the development environment.
    • compose.prod.yaml: The Docker Compose file for the production environment.

    Environment Comparison

    FeatureDevelopment (compose.dev.yaml)Production (compose.prod.yaml)
    GoalDeveloper productivity and debuggingPerformance, security, and stability
    ToolsIncludes Xdebug, Node.js (via NVM), and NPM
    Code UpdatesHot reloading via volume mounts
    Build StrategyExtends production builds with dev tools
    OptimizationFocus on tool availability
    SecurityWritable permissions for volumes
    AssetsCompiled via npm run dev
    AssetsPre-built during the build process
    ServicesPHP-FPM, Nginx, Redis, PostgreSQL, Node.jsPHP-FPM, Nginx, Redis, PostgreSQL
  2. Set up the Laravel development environment

    main

    To run the project locally for development, follow these steps to configure your environment variables, start the containers, and install dependencies.

    1. Clone the repository:

      git clone https://github.com/rw4lll/laravel-docker-examples.git
      cd laravel-docker-examples
    2. Configure environment variables: Copy the example environment file to .env:

      cp .env.example .env

      Important: Open .env and adjust the UID and GID variables to match your local user and group IDs. You can find these by running id -u and id -g in your terminal. This ensures file permissions work correctly between your host and the container.

    3. Start the services:

      docker compose -f compose.dev.yaml up -d
    4. Install dependencies and run migrations: Execute these commands inside the workspace container:

      docker compose -f compose.dev.yaml exec workspace composer install
      docker compose -f compose.dev.yaml exec workspace npm install
      docker compose -f compose.dev.yaml exec workspace npm run dev
      docker compose -f compose.dev.yaml exec workspace php artisan migrate
    5. Access the app: Navigate to http://localhost in your browser.

    git clone https://github.com/rw4lll/laravel-docker-examples.git
    cd laravel-docker-examples
    cp .env.example .env
    docker compose -f compose.dev.yaml up -d
  3. Configure core application settings via environment variables

    main

    The config/app.php file defines the core behavior of the Laravel application. Most settings are driven by environment variables (accessed via the env() helper), allowing you to change application behavior without modifying code.

    Key configuration areas include:

    • Identity: APP_NAME sets the application name used in notifications and UI.
    • Environment: APP_ENV determines the current environment (e.g., production, local).
    • Debugging: APP_DEBUG (boolean) toggles detailed error messages and stack traces.
    • Routing: APP_URL provides the base URL used by the Artisan CLI to generate URLs.
    • Localization: APP_LOCALE, APP_FALLBACK_LOCALE, and APP_FAKER_LOCALE control translation and data generation defaults.
    • Security: APP_KEY provides the encryption key, and APP_PREVIOUS_KEYS allows for key rotation.
  4. Technical stack specifications

    main

    The project uses the following technology stack:

    • PHP: Version 8.5 FPM (used in both environments).
    • Node.js: Version 22.x (used in development for Vite/frontend assets).
    • Database: PostgreSQL 16 (can be swapped for MySQL via configuration).
    • Cache/Session: Redis.
    • Web Server: Nginx.
    • Orchestration: Docker Compose.
  5. Manage the development environment with Docker Compose

    main

    Use the following commands to interact with the development environment orchestrated by compose.dev.yaml.

    Access the Workspace

    The workspace container is a sidecar that includes Composer, Node.js, and NPM for running development tasks.

    docker compose -f compose.dev.yaml exec workspace bash

    Run Artisan Commands

    Execute Laravel Artisan commands within the workspace container:

    docker compose -f compose.dev.yaml exec workspace php artisan migrate

    Lifecycle Management

    • Rebuild containers (useful after changing Dockerfiles or dependencies):
      docker compose -f compose.dev.yaml up -d --build
    - **Stop containers**:
      ```bash
    docker compose -f compose.dev.yaml down

    Viewing Logs

    • View all service logs:
      docker compose -f compose.dev.yaml logs -f
    - **View logs for a specific service (e.g., 'web')**:
      ```bash
    docker compose -f compose.dev.yaml logs -f web
  6. Configure application security and encryption keys

    main

    Laravel uses an encryption key to secure sensitive data.

    • APP_KEY: This must be set to a random, 32-character string before deployment. It is used by the encryption services.
    • cipher: Defines the encryption algorithm, defaulting to AES-256-CBC.
    • APP_PREVIOUS_KEYS: A comma-separated list of previous keys used to support decryption of data encrypted with older keys during a rotation process.
    'cipher' => 'AES-256-CBC',
    'key' => env('APP_KEY'),
    'previous_keys' => [
        ...array_filter(
            explode(',', (string) env('APP_PREVIOUS_KEYS', ''))
        ),
    ],
  7. Configure maintenance mode drivers

    main

    Maintenance mode determines how the application handles requests when it is in a 'down' state. You can configure the driver and the storage mechanism via environment variables.

    Supported drivers:

    • file: Local file-based maintenance mode.
    • cache: Allows maintenance mode to be controlled across multiple machines/servers.

    Configuration keys:

    • APP_MAINTENANCE_DRIVER: The driver to use (default: file).
    • APP_MAINTENANCE_STORE: The storage backend (e.g., database).
    'maintenance' => [
        'driver' => env('APP_MAINTENANCE_DRIVER', 'file'),
        'store' => env('APP_MAINTENANCE_STORE', 'database'),
    ],