Laravel Daily's

Docker Made Simple: Getting Started with Laravel Sail

hero image

Local development used to mean manual installations. You needed PHP, MySQL, Redis, and various extensions on your host machine. One version mismatch between teammates could stall a project for hours. Laravel Sail removes this friction. It provides a lightweight command-line interface for interacting with Docker. You get a complete, reproducible environment without ever touching a Dockerfile.

The Sail Philosophy: Containerization for Everyone

Laravel Sail is a built-in solution for Laravel. It serves as a wrapper for Docker Compose. You do not need to be a DevOps expert to use it. It handles the orchestration of your web server, database, and cache.

This approach ensures consistency across your team. Every developer runs the exact same environment. It eliminates the "works on my machine" excuse. When you ship to Laravel Cloud, the transition is seamless.

Setting the Stage: Prerequisites

You only need Docker Desktop installed on your machine. Sail supports macOS, Linux, and Windows via WSL2.

Windows users must use WSL2 for optimal performance. The Linux kernel integration ensures your file system stays fast. Once Docker is running, you are ready to begin. No local PHP or Composer installation is required for new projects.

Launching a New Project

Creating a fresh Laravel application with Sail is a single command. Open your terminal and run the following:

curl -s "https://laravel.build/example-app" | bash

This script downloads the latest Laravel installer. It configures a new directory with a docker-compose.yml file. You can customize services by adding a query string. For example, to include MariaDB and Redis:

curl -s "https://laravel.build/example-app?with=mariadb,redis" | bash

After the installation finishes, navigate to your project directory.

Integrating Sail into Existing Apps

You can add Sail to an existing project just as easily. First, require the package via Composer:

composer require laravel/sail --dev

Once installed, use the Artisan command to generate your Docker configuration:

php artisan sail:install

A prompt will ask which services you need. Select your database, cache, and search engines. The command creates a docker-compose.yml file in your project root. You now have a portable development environment.

A terminal window showing a successful 'sail up' command with tech icons in a high-energy cartoony style.

Navigating the Environment: Basic Commands

Starting your environment is the most common task. Use the up command to launch your containers:

./vendor/bin/sail up

Run it in detached mode with the -d flag to keep your terminal free. To stop the containers, use sail stop.

Defining an Alias

Typing ./vendor/bin/sail is tedious. Most developers define a shell alias. Add this to your .zshrc or .bashrc file:

alias sail='[ -f sail ] && sh sail || sh vendor/bin/sail'

Now you can simply type sail for any command.

Executing App Commands

Sail proxies commands to the appropriate Docker container. You don't need to install tools on your host machine. Everything happens inside the sandbox.

Running Artisan

Artisan is the heartbeat of Laravel. Execute migrations or generate controllers through Sail:

sail artisan migrate
sail artisan make:controller UserController

Managing Dependencies

Use Sail to run Composer or NPM. It ensures your dependencies are installed using the PHP or Node version defined in your container.

sail composer require laravel/sanctum
sail npm install
sail npm run dev

An illustration of the Laravel logo, Artisan hammer, and Composer icon working together in a vibrant, high-energy style.

Expanding Your Horizons: Adding Services

Projects evolve. You might start with a simple database and later need Laravel Reverb for WebSockets or Horizon for queue monitoring. Sail makes it easy to add these.

Run the sail:add command to include new services:

php artisan sail:add

Select the services you want to add, such as Mailpit or Meilisearch. Sail updates your docker-compose.yml and environment variables. Restart your containers to apply the changes.

A cartoony toolbox featuring the Laravel logo and icons for MySQL, Redis, and Mailpit on a bright background.

Advanced Customization

Sail is flexible. You can publish its Dockerfiles to customize the environment further:

php artisan sail:publish

This creates a docker directory in your project. You can now modify the PHP version or install specific OS packages. Sail also supports a docker-compose.override.yml file. Use this for local-only changes that you don't want to commit to version control.

A Better Way to Build

Laravel Sail represents the modern standard for local development. It lowers the barrier to entry for Docker. It provides a clean, predictable stack that grows with your application.

Whether you are a solo builder or part of a large agency, Sail keeps you focused on shipping features. Stop managing local servers and start building. We’d love to see what you create with it.

Previous
Write Clean Code Automatically with Laravel Pint
Next
Lightweight API Authentication with Laravel Sanctum