abolaradev / laravel-dockerize
Docker tools for Laravel applications
Package info
github.com/abolaradev/laravel-dockerize
Language:Shell
pkg:composer/abolaradev/laravel-dockerize
Requires
- php: ^8.3
- illuminate/console: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A reusable Docker environment for Laravel applications, providing preconfigured services, development and production configurations, and automation scripts to simplify the Dockerization process.
Included Services
The Docker environment provides the following preconfigured services for Laravel applications.
Deployment-related services are defined in compose.yaml, while development-specific services and configurations are defined separately in compose.dev.yaml.
| Service | Description | Environment |
|---|---|---|
| Nginx | Web server and reverse proxy for HTTP requests. | All |
| PHP-FPM | Runs the Laravel application. | All |
| MySQL | Relational database for application data. | All |
| Redis | Handles caching, sessions, and queues. | All |
| Meilisearch | Provides full-text search functionality. | All |
| Queue Worker | Processes Laravel background jobs. | All |
| Scheduler | Executes scheduled Laravel tasks. | All |
| phpMyAdmin | Web-based MySQL database management interface. | Development |
| RedisInsight | Web-based Redis monitoring and management interface. | Development |
| Mailpit | Captures outgoing emails for development and testing. | Development |
Development-specific services are included when running Docker Compose with compose.dev.yaml.
Project Structure
.
├── docker/
│ ├── configs/
│ ├── scripts/
│ └── secrets/
├── Dockerfile
├── compose.yaml
├── compose.dev.yaml
├── .env
├── .env.docker.example
└── README.md
docker/configs/: Service configuration files.docker/scripts/: Automation and management scripts.docker/secrets/: Sensitive values used by Docker Secrets.Dockerfile: Multi-stage Docker build configuration.compose.yaml: Main deployment configuration.compose.dev.yaml: Development-specific configurations and services..env.docker.example: Example environment configuration.
Getting Started
1. Installation
Install the package via Composer:
composer require abolaradev/laravel-dockerize
2. Docker Installation
Run the following Artisan command from your Laravel project root:
php artisan docker:install
This command prepares your Laravel application for the Docker environment by performing the following operations:
- Publish Docker Files: Publishes the required Docker files, including
Dockerfile,compose.yaml,compose.dev.yaml, configuration files, and scripts to the root directory of your Laravel application. - Create Environment File: Creates a new
.envfile using.env.docker.exampleas the template. - Generate Application Key: Generates a new Laravel application encryption key by running
php artisan key:generate. - Configure Application Timezone: Configures the application's timezone.
- Create Default Docker Secrets: Creates the predefined Docker Secret files required by services such as MySQL, Redis, and Meilisearch by running
php artisan docker:secret --default. - Configure Laravel to Use Docker Secrets: Updates the relevant Laravel configuration files to retrieve sensitive values from Docker Secret files using the
docker_secret()helper function instead of accessing them directly through environment variables.
For example, in config/database.php, the MySQL password configuration is changed from:
'password' => env('DB_PASSWORD', ''),
to:
'password' => docker_secret('database_password'),
After installation, your Laravel application is ready to run in the Docker environment.
Running the Application
This environment supports two modes: Deployment and Production. Each mode uses a different Docker Compose configuration.
Deployment Environment
To build and start the application and all its services, including the development tools, run:
docker compose -f compose.yaml -f compose.dev.yaml up -d --build
This command combines both Compose files and starts all defined services.
Docker Compose Watch
Docker Compose Watch monitors local project files and automatically synchronizes changes or rebuilds services according to the configured watch rules.
In this project, Watch is configured for the app, queue, and scheduler services.
To start these services with Watch enabled, run:
docker compose -f compose.yaml -f compose.dev.yaml up --watch app queue scheduler
The configured behavior is as follows:
- app: Synchronizes application files and rebuilds when dependency files change.
- queue: Synchronizes changes in job classes and restarts the service.
- scheduler: Synchronizes changes in
routes/console.phpand restarts the service.
OPcache Configuration
For development, the following settings are enabled in docker/configs/php.ini:
opcache.validate_timestamps=1 opcache.revalidate_freq=0
These settings ensure that OPcache checks PHP file modification timestamps on every request (validate_timestamps=1) and immediately detects changes without waiting for a revalidation interval (revalidate_freq=0).
This allows PHP code changes to take effect without manually restarting the PHP-FPM container.
Note: For production, timestamp validation is disabled (opcache.validate_timestamps=0) to improve performance. In this mode, PHP-FPM must be restarted or OPcache reset for code changes to take effect.
Running the Frontend Development Server
Start the Vite development server in your local project directory:
npm run dev
Once all services are running, the Laravel application will be accessible at:
Production Environment
To build the application images and start all production services in the background, run:
docker compose up -d --build
Viewing Service Ports
To see which ports are exposed and bound to the host machine, run:
php artisan docker:ports
Example output:
SERVICE | LOCAL ADDRESS
----------------------------------------------------------
database | http://127.0.0.1:3307
mailpit | http://127.0.0.1:1025
mailpit | http://127.0.0.1:8025
phpmyadmin | http://127.0.0.1:8080
redis-insight | http://127.0.0.1:5540
webserver | http://127.0.0.1:8000
Viewing Service Logs
To follow the logs of individual services, use:
Queue Worker:
docker compose logs -f queue
Scheduler:
docker compose logs -f scheduler
Docker Secrets
Sensitive application data, such as passwords and API keys, is stored in Docker Secret files instead of being defined directly in the .env file.
All secret files are stored in:
docker/secrets/
Creating a Secret
To create a secret file, run:
php artisan docker:secret <secret_name>
A random value is generated automatically and stored in the corresponding secret file.
To specify a custom value, use the --key option:
php artisan docker:secret openai_api_key --key=your-api-key
This creates openai_api_key.txt with the specified value.
Creating Default Secrets
Use the --default option to create the predefined secret files required by services such as MySQL, Redis, and Meilisearch:
php artisan docker:secret --default
This creates the following files:
database_root_password.txtdatabase_password.txtredis_default_password.txtredis_password.txtmeilisearch_key.txt
Note: Default secrets are automatically generated during php artisan docker:install. You do not need to run this command manually during the initial installation.
Docker Compose references these files through its secrets configuration:
secrets: database_root_password: file: ./docker/secrets/database_root_password.txt database_password: file: ./docker/secrets/database_password.txt redis_default_password: file: ./docker/secrets/redis_default_password.txt redis_password: file: ./docker/secrets/redis_password.txt meilisearch_key: file: ./docker/secrets/meilisearch_key.txt
Using Docker Secrets in Laravel
The package provides a docker_secret() helper function to read secret values from their corresponding mounted files.
In Laravel configuration files, use docker_secret() instead of env() for values stored as Docker Secrets. For example:
// config/database.php 'mysql' => [ // ... 'password' => docker_secret('database_password'), ],
Pass the Docker Compose secret name to docker_secret(). The argument must match the secret name and the corresponding file name without the .txt extension.
| Secret File | Helper Argument |
|---|---|
database_password.txt |
database_password |
redis_password.txt |
redis_password |
meilisearch_key.txt |
meilisearch_key |
Important: Never commit secret files containing sensitive values to version control.
Rotating Passwords
You may need to change the credentials configured for MySQL and Redis for security or maintenance purposes.
To synchronize updated secret values with the actual service credentials, run the corresponding Artisan command.
MySQL:
php artisan docker:rotate-password mysql
Redis:
php artisan docker:rotate-password redis
Each command applies the updated secret value to the corresponding user inside the service, keeping the credentials in the secret files and the service authentication settings synchronized.
After successfully rotating the passwords, recreate the services so that the containers restart with the updated secret files.
Deployment Environment:
docker compose -f compose.yaml -f compose.dev.yaml up -d --force-recreate
Production Environment:
docker compose up -d --force-recreate
Important: Ensure that the Laravel application and any other dependent services use the updated credentials. Verify connectivity after completing the rotation.
If you find this package useful, please don't forget to give it a ⭐ on GitHub. ❤️
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy for information on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.