Laravel Sail is a fantastic tool that simplifies the use of Docker in Laravel projects. It allows you to run your Laravel application and its required services like databases, caching systems, and more in Docker containers. However, using ./vendor/bin/sail
repeatedly can be cumbersome. This guide will walk you through how to set up Laravel Sail so that you can run commands directly, add services to your Docker configuration, and manage your development environment with ease.
Start by creating a new Laravel project if you haven’t done so:
composer create-project laravel/laravel your-project-name
To avoid typing ./vendor/bin/sail
every time, you can configure your environment to use sail
directly:
Option 1: Create a Shell Alias
Open your shell configuration file and add:
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
Option 2: Add Sail to Your PATH
Alternatively, add Sail to your PATH:
export PATH="$HOME/.composer/vendor/bin:$PATH"
Now that you’ve configured your environment, start your Laravel application with Sail:
sail up
To add new services or modify existing ones, edit the docker-compose.yml
file directly. After editing, restart your Docker containers:
sail up -d
Here are some useful Sail commands:
sail up
sail down
sail artisan migrate
sail composer install
sail npm install
Published By: Krishanu Jadiya
Updated at: 2024-08-18 00:18:08
Frequently Asked Questions:
1. Why use Laravel Sail with Docker?
Laravel Sail simplifies the use of Docker for Laravel projects, providing a pre-configured environment that includes services like databases, queues, and more. It helps ensure consistency between development and production environments.
2. Can I add custom services to my Sail setup?
Yes, you can modify the docker-compose.yml file to add any additional services you need. For example, you can add services like Elasticsearch, RabbitMQ, etc.
3. How do I run Sail commands without ./vendor/bin/?
You can either set up a shell alias or add Sail to your system’s PATH, allowing you to run commands like <code>sail up</code> directly.
4. Can I use Sail in a production environment?
While Sail is primarily intended for local development, it can be adapted for production use. You would typically create a separate docker-compose.prod.yml with production-specific settings.