Laravel Sail is a lightweight command-line interface for interacting with Laravel's default Docker development environment. It provides an easy way to set up and manage your Laravel application's development environment without requiring extensive Docker knowledge. In this post, we'll explore how to use Sail and some practical examples of its capabilities.
Getting Started with Laravel Sail
Laravel Sail comes pre-installed with new Laravel applications. To create a new Laravel project with Sail, use the following command:
curl -s "https://laravel.build/example-app" | bash
This command creates a new Laravel application with Sail configured. Once the installation is complete, navigate to your project directory and start Sail:
cd example-app
./vendor/bin/sail up
Key Features and Examples
Running Artisan Commands
With Sail, you can run Artisan commands using the sail
CLI:
./vendor/bin/sail artisan migrate
Executing PHP Commands
You can run PHP commands within your Sail environment:
./vendor/bin/sail php --version
Running Tests
Sail makes it easy to run your application's tests:
./vendor/bin/sail test
Interacting with the Database
You can access your MySQL database using Sail:
./vendor/bin/sail mysql
Running Composer Commands
Manage your Composer dependencies with Sail:
./vendor/bin/sail composer require laravel/sanctum
Suggested Usages
Local Development Environment: Use Sail as your primary local development environment for Laravel projects.
CI/CD Pipelines: Incorporate Sail into your CI/CD workflows for consistent testing environments.
Team Collaboration: Ensure all team members have the same development environment setup.
Multiple PHP Versions: Easily switch between PHP versions for testing compatibility:
sail up --build --no-deps sail
Database Management: Use Sail to manage your database migrations and seeders:
sail artisan migrate:fresh --seed
API Development: Leverage Sail's built-in services like MySQL and Redis for API development:
sail artisan make:controller API/UserController --api
Email Testing: Use Mailhog for local email testing:
sail artisan config:cache sail artisan make:mail WelcomeEmail
Best Practices
Use Sail Alias: Create an alias for the Sail command to simplify usage:
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
Customize Services: Modify the
docker-compose.yml
file to add or remove services as needed for your project.Version Control: Include your
docker-compose.yml
file in version control to ensure consistency across environments.Performance Optimization: Use Sail's caching features for improved performance:
sail artisan config:cache sail artisan route:cache
Regular Updates: Keep Sail and its dependencies updated to benefit from the latest features and security patches.
Laravel Sail simplifies the process of setting up and managing a Laravel development environment. By leveraging Docker, it provides a consistent and easily reproducible environment across different machines and operating systems. Whether you're a solo developer or part of a large team, Sail can streamline your Laravel development workflow and help you focus on building great applications.