Mastering Laravel Sail: Your Guide to Effortless Local Development #
Laravel Sail is a lightweight command-line interface for interacting with Laravel's default Docker development environment. It provides a simple way to build a robust local development setup for your Laravel applications without prior Docker expertise. By leveraging Docker, Sail ensures your development environment closely mirrors your production environment, reducing "it works on my machine" issues and simplifying team collaboration.
Why Choose Laravel Sail? #
- Consistency: Docker containers ensure your application runs in the same environment across all machines, from development to production.
- Isolation: Each project can have its own isolated environment with specific PHP versions, database types, and services.
- Simplicity: Sail abstracts away most Docker complexities, allowing you to focus on coding.
- Full-featured: Includes common services like MySQL, PostgreSQL, Redis, Mailpit, Memcached, MeiliSearch, and Selenium.
Prerequisites #
Before diving into Laravel Sail, ensure you have:
- Docker Desktop: For macOS, Windows (WSL2 recommended for Windows).
- Docker Engine & Docker Compose: For Linux.
- Basic understanding of Laravel.
- A fresh or existing Laravel project.
Getting Started with Laravel Sail #
1. Installing Laravel Sail
For New Laravel Projects: The easiest way to start with Sail is to create a new Laravel project and include Sail from the outset. This will automatically install Sail and all necessary Docker Compose files.
composer create-project laravel/laravel example-app --prefer-dist
cd example-app
php artisan sail:install
Alternatively, you can create a new project directly with Sail and specify the services you need:
curl -s "https://laravel.build/example-app?with=mysql,redis,meilisearch,mailpit,selenium" | bash
cd example-app
./vendor/bin/sail up
This command downloads the Laravel installer, creates your project, installs Sail, and brings up the Docker containers.
For Existing Laravel Projects: If you have an existing Laravel application, you can add Sail to it using Composer:
cd your-laravel-project
composer require laravel/sail --dev
php artisan sail:install
The sail:install command will publish a docker-compose.yml file to the root of your project and configure your .env file to use Sail's services.
2. Starting and Stopping Sail
Once Sail is installed, you can start your development environment using the up command:
./vendor/bin/sail up
This command will build the Docker images (if not already built) and start the containers defined in your docker-compose.yml file. Your application will typically be accessible at http://localhost.
For running containers in the background (daemonized), use the -d flag:
./vendor/bin/sail up -d
To view the status of your running containers:
./vendor/bin/sail ps
To stop all running Sail containers:
./vendor/bin/sail stop
To stop and remove all containers, networks, and volumes:
./vendor/bin/sail down
This is useful for a clean slate, but be aware it removes database data if not persisted outside the container.
3. Running Artisan, Composer, and NPM Commands
Sail provides a convenient way to execute commands within your Docker containers, ensuring they run in the correct environment. Just prefix your usual commands with ./vendor/bin/sail.
Artisan Commands:
./vendor/bin/sail artisan migrate
./vendor/bin/sail artisan tinker
./vendor/bin/sail artisan make:controller HomeController
Composer Commands:
./vendor/bin/sail composer require laravel/horizon
./vendor/bin/sail composer install
NPM Commands (Node.js/Yarn):
./vendor/bin/sail npm install
./vendor/bin/sail npm run dev
./vendor/bin/sail yarn add react
PHP Commands:
./vendor/bin/sail php --version
./vendor/bin/sail php artisan --version
./vendor/bin/sail test
4. Connecting to Database and Other Services
Sail configures your .env file with the necessary credentials to connect to its services.
MySQL/PostgreSQL:
DB_HOST=mysql(orDB_HOST=pgsqlfor PostgreSQL)DB_PORT=3306(or5432for PostgreSQL)DB_DATABASE=laravelDB_USERNAME=sailDB_PASSWORD=password
Redis:
REDIS_HOST=redisREDIS_PORT=6379REDIS_PASSWORD=null
Remember, mysql and redis are the service names defined in your docker-compose.yml file, which Docker resolves to the correct IP addresses within the Docker network.
5. Customizing Your Sail Environment
While Sail works great out-of-the-box, you might need to customize its environment.
Publishing Sail's Dockerfiles: To gain full control over the Docker images, you can publish Sail's Dockerfiles to your project's root using:
./vendor/bin/sail artisan sail:publish
This command will place Dockerfiles (e.g., docker/8.2/Dockerfile) and other configuration files in a docker directory in your project root. You can then modify these files to install additional PHP extensions, system dependencies, or change base images. After modifying, you'll need to rebuild your images:
./vendor/bin/sail build --no-cache
./vendor/bin/sail up -d
Modifying docker-compose.yml:
You can directly edit your docker-compose.yml file to:
- Add new services (e.g., another database, a specialized queue worker).
- Change port mappings.
- Adjust resource limits.
- Mount additional volumes.
Example: Adding a new service (e.g., a custom Nginx proxy):
# docker-compose.yml
version: '3.8'
services:
# ... existing services ...
my-nginx-proxy:
image: nginx:alpine
ports:
- '8080:80'
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./src:/var/www/html # Assuming your Laravel app is in src
networks:
- sail
After modifying docker-compose.yml, run sail up -d to bring up the new service.
Tips and Tricks for a Smoother Workflow #
-
Create a Bash Alias: Typing
./vendor/bin/sailcan be tedious. Add an alias to your shell's configuration file (.bashrc,.zshrc, etc.):alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'Then, you can simply run
sail up,sail artisan migrate, etc. -
Performance on Windows: For optimal performance on Windows, ensure you are using WSL2 (Windows Subsystem for Linux 2) with Docker Desktop. Configure Docker Desktop to use the WSL2 backend.
-
Persistent Data: For databases, the
docker-compose.ymlusually includes named volumes (e.g.,sail-mysql). These volumes persist data even if containers are removed. If you usesail down -v, these volumes will also be removed, deleting your data. Be mindful of this when cleaning up. -
Accessing the Container Shell: You can SSH into your application container (which runs PHP) using:
./vendor/bin/sail shellThis is useful for debugging or executing commands directly inside the container.
Conclusion #
Laravel Sail dramatically simplifies setting up and managing a local development environment for Laravel applications. By harnessing the power of Docker, it provides a consistent, isolated, and highly customizable environment that helps streamline your development workflow and collaboration. Embrace Laravel Sail to make your local development experience truly effortless.