Laravel Sail is a lightweight command-line interface for interacting with Laravel’s default Docker development environment. It provides a simple way to get a Laravel application up and running with Docker without requiring prior Docker experience. Here’s a detailed guide on how to use Laravel Sail for local development:
Step-by-Step Guide
- Creating a New Laravel Project
To create a new Laravel project with Sail, use the following command:
curl -s "https://laravel.build/example-app" | bash
Replace example-app
with your desired project name. This command will set up a new Laravel project with Sail configured.
- Navigating to Your Project Directory
cd example-app
Replace example-app
with your project directory name.
- Starting Laravel Sail
Sail provides a convenient way to start Docker containers with the necessary services for Laravel development, such as MySQL, Redis, and more.
./vendor/bin/sail up
This command will start the Docker containers. The first time you run this command, it might take a while as Docker images are being pulled.
- Running Sail Commands
You can use Sail to run various Laravel Artisan commands and other utilities. For example:
./vendor/bin/sail artisan migrate
./vendor/bin/sail artisan tinker
./vendor/bin/sail npm install
./vendor/bin/sail npm run dev
Prefix any command with ./vendor/bin/sail
to run it within the Docker environment.
- Configuring Services
Thedocker-compose.yml
file in your Laravel project directory allows you to configure various services. You can add or modify services as needed. For example, to add a PostgreSQL service, you would modify thedocker-compose.yml
as follows:
services:
pgsql:
image: postgres:13
environment:
POSTGRES_DB: example
POSTGRES_USER: example
POSTGRES_PASSWORD: secret
ports:
- "5432:5432"
After making changes, restart Sail to apply the new configuration:
./vendor/bin/sail up -d
- Using Custom Sail Aliases
To avoid typing./vendor/bin/sail
every time, you can create a shell alias. Add the following line to your.bashrc
,.zshrc
, or equivalent file:
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
After adding the alias, you can simply use sail
instead of the full path:
sail up
sail artisan migrate
- Stopping Sail
When you’re done working on your project, you can stop the Docker containers with:
./vendor/bin/sail down
Benefits of Using Laravel Sail
- Simplified Setup: Laravel Sail abstracts away the complexity of setting up a Docker environment.
- Consistency: Ensures that the development environment is consistent across different machines.
- Scalability: Easily add and configure services like databases, cache, and queues.
Conclusion
Laravel Sail makes local development with Docker straightforward and efficient. By following the steps outlined above, you can quickly set up a new Laravel project and start developing with a robust Docker environment. Whether you’re new to Docker or an experienced user, Sail provides a seamless development experience that can help you focus more on building your application rather than managing your environment.