|
| 1 | +# Laravel Docker Environment |
| 2 | + |
| 3 | +This project provides a production-ready Docker setup for a Laravel application using `serversideup/php` images. It includes a multi-stage `Dockerfile` for optimized builds and a `docker-compose.yml` file to orchestrate the application, database, and caching services. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Docker |
| 8 | +- Docker Compose |
| 9 | + |
| 10 | +## Setup & Installation |
| 11 | + |
| 12 | +1. **Clone the Repository:** |
| 13 | + Clone this project to your local machine. |
| 14 | + |
| 15 | +2. **Configure Environment:** |
| 16 | + Create a `.env` file in the project root by copying the example file: |
| 17 | + ```bash |
| 18 | + cp .env.example .env |
| 19 | + ``` |
| 20 | + Update the `.env` file with your specific application and database settings. The default database configuration is set up to work with the `docker-compose.yml` file provided. |
| 21 | + |
| 22 | +3. **Generate Application Key:** |
| 23 | + If this is a new project, you'll need to generate a Laravel application key. First, start the containers, then run the command. |
| 24 | + ```bash |
| 25 | + # Start containers in the background |
| 26 | + docker-compose up -d --build |
| 27 | +
|
| 28 | + # Generate the key |
| 29 | + docker-compose exec app php artisan key:generate |
| 30 | + ``` |
| 31 | +
|
| 32 | +## Usage |
| 33 | +
|
| 34 | +- **Build and Start Containers:** |
| 35 | + ```bash |
| 36 | + docker-compose up -d --build |
| 37 | + ``` |
| 38 | + Your application will be available at `http://localhost:8080`. |
| 39 | +
|
| 40 | +- **Stop Containers:** |
| 41 | + ```bash |
| 42 | + docker-compose down |
| 43 | + ``` |
| 44 | +
|
| 45 | +- **Stop Containers and Remove Volumes:** (Use this to start fresh) |
| 46 | + ```bash |
| 47 | + docker-compose down -v |
| 48 | + ``` |
| 49 | +
|
| 50 | +## Scheduled Tasks (Cron) |
| 51 | +
|
| 52 | +To run Laravel's scheduled tasks, this setup includes a dedicated `scheduler` service in the `docker-compose.yml` file. |
| 53 | + |
| 54 | +This container uses the same application image but runs a loop that executes `php artisan schedule:run` every minute. This is the standard way to handle cron jobs in a containerized Laravel application. |
| 55 | + |
| 56 | +All of your scheduled tasks are defined in the `app/Console/Kernel.php` file as usual. The `scheduler` service will automatically pick up and execute any tasks you define there. |
| 57 | + |
| 58 | +## Laravel Automations (`serversideup/php`) |
| 59 | + |
| 60 | +The `serversideup/php` image provides a powerful automation system that can run common Laravel commands for you when the container starts. This is perfect for production deployments. |
| 61 | + |
| 62 | +### Master Switch |
| 63 | + |
| 64 | +All automations are controlled by a single master switch. They will **only run** if this is enabled. |
| 65 | + |
| 66 | +- **`AUTORUN_ENABLED`**: Set to `"true"` to enable the automation scripts. Defaults to `false`. |
| 67 | + |
| 68 | +### Automation Flags |
| 69 | + |
| 70 | +If `AUTORUN_ENABLED` is set to `"true"`, you can then toggle individual automations. |
| 71 | + |
| 72 | +| Environment Variable | Default | Command Executed | Description | |
| 73 | +| ---------------------------------- | ------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------- | |
| 74 | +| `AUTORUN_LARAVEL_MIGRATION` | `true` | `php artisan migrate --force` | Automatically runs your database migrations. | |
| 75 | +| `AUTORUN_LARAVEL_CONFIG_CACHE` | `true` | `php artisan config:cache` | Caches your configuration files for a significant performance boost. | |
| 76 | +| `AUTORUN_LARAVEL_ROUTE_CACHE` | `true` | `php artisan route:cache` | Caches your route definitions. | |
| 77 | +| `AUTORUN_LARAVEL_VIEW_CACHE` | `true` | `php artisan view:cache` | Compiles and caches your Blade templates. | |
| 78 | +| `AUTORUN_LARAVEL_EVENT_CACHE` | `true` | `php artisan event:cache` | Caches your application's events and listeners. | |
| 79 | +| `AUTORUN_LARAVEL_STORAGE_LINK` | `true` | `php artisan storage:link` | Creates the symbolic link from `public/storage` to `storage/app/public`. | |
| 80 | +| `AUTORUN_LARAVEL_MIGRATION_ISOLATION` | `false` | `php artisan migrate --force --isolated` | Ensures only one container runs migrations at a time. Requires Laravel 9.38+ and a locking database. | |
| 81 | +| `AUTORUN_LARAVEL_MIGRATION_TIMEOUT` | `30` | N/A | The number of seconds the script will wait for the database to be available before migrating. | |
| 82 | +
|
| 83 | +## Manual Artisan Commands |
| 84 | +
|
| 85 | +You can run any `artisan` command manually by executing it inside the `app` container. |
| 86 | +
|
| 87 | +- **Run Migrations:** |
| 88 | + ```bash |
| 89 | + docker-compose exec app php artisan migrate |
| 90 | + ``` |
| 91 | +
|
| 92 | +- **Access Tinker:** |
| 93 | + ```bash |
| 94 | + docker-compose exec app php artisan tinker |
| 95 | + ``` |
| 96 | +
|
| 97 | +- **Clear Cache:** |
| 98 | + ```bash |
| 99 | + docker-compose exec app php artisan optimize:clear |
| 100 | + ``` |
0 commit comments