Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker development environment. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience. Show
At its heart, Sail is the 8 file and the 9 script that is stored at the root of your project. The 9 script provides a CLI with convenient methods for interacting with the Docker containers defined by the 8 file.Laravel Sail is supported on macOS, Linux, and Windows (via WSL2). Installation & SetupLaravel Sail is automatically installed with all new Laravel applications so you may start using it immediately. To learn how to create a new Laravel application, please consult Laravel's installation documentation for your operating system. During installation, you will be asked to choose which Sail supported services your application will be interacting with. Installing Sail Into Existing ApplicationsIf you are interested in using Sail with an existing Laravel application, you may simply install Sail using the Composer package manager. Of course, these steps assume that your existing local development environment allows you to install Composer dependencies:
After Sail has been installed, you may run the 2 Artisan command. This command will publish Sail's 8 file to the root of your application:Finally, you may start Sail. To continue learning how to use Sail, please continue reading the remainder of this documentation: Using DevcontainersIf you would like to develop within a Devcontainer, you may provide the 4 option to the 2 command. The 4 option will instruct the 2 command to publish a default 8 file to the root of your application:
Configuring A Shell AliasBy default, Sail commands are invoked using the 9 script that is included with all new Laravel applications:However, instead of repeatedly typing 9 to execute Sail commands, you may wish to configure a shell alias that allows you to execute Sail's commands more easily:
To make sure this is always available, you may add this to your shell configuration file in your home directory, such as 1 or 2, and then restart your shell.Once the shell alias has been configured, you may execute Sail commands by simply typing 9. The remainder of this documentation's examples will assume that you have configured this alias:Starting & Stopping SailLaravel Sail's 8 file defines a variety of Docker containers that work together to help you build Laravel applications. Each of these containers is an entry within the 5 configuration of your 8 file. The 7 container is the primary application container that will be serving your application.Before starting Sail, you should ensure that no other web servers or databases are running on your local computer. To start all of the Docker containers defined in your application's 8 file, you should execute the 9 command:To start all of the Docker containers in the background, you may start Sail in "detached" mode: Once the application's containers have been started, you may access the project in your web browser at: http://localhost. To stop all of the containers, you may simply press Control + C to stop the container's execution. Or, if the containers are running in the background, you may use the 0 command:Executing CommandsWhen using Laravel Sail, your application is executing within a Docker container and is isolated from your local computer. However, Sail provides a convenient way to run various commands against your application such as arbitrary PHP commands, Artisan commands, Composer commands, and Node / NPM commands. When reading the Laravel documentation, you will often see references to Composer, Artisan, and Node / NPM commands that do not reference Sail. Those examples assume that these tools are installed on your local computer. If you are using Sail for your local Laravel development environment, you should execute those commands using Sail:
Executing PHP CommandsPHP commands may be executed using the 1 command. Of course, these commands will execute using the PHP version that is configured for your application. To learn more about the PHP versions available to Laravel Sail, consult the :Executing Composer CommandsComposer commands may be executed using the 2 command. Laravel Sail's application container includes a Composer 2.x installation:
Installing Composer Dependencies For Existing ApplicationsIf you are developing an application with a team, you may not be the one that initially creates the Laravel application. Therefore, none of the application's Composer dependencies, including Sail, will be installed after you clone the application's repository to your local computer. You may install the application's dependencies by navigating to the application's directory and executing the following command. This command uses a small Docker container containing PHP and Composer to install the application's dependencies:
When using the 3 image, you should use the same version of PHP that you plan to use for your application ( 4, 5, 6, or 7).Executing Artisan CommandsLaravel Artisan commands may be executed using the 8 command:Executing Node / NPM CommandsNode commands may be executed using the 9 command while NPM commands may be executed using the 0 command:If you wish, you may use Yarn instead of NPM: Interacting With DatabasesMySQLAs you may have noticed, your application's 8 file contains an entry for a MySQL container. This container uses a Docker volume so that the data stored in your database is persisted even when stopping and restarting your containers.In addition, the first time the MySQL container starts, it will create two databases for you. The first database is named using the value of your 2 environment variable and is for your local development. The second is a dedicated testing database named 3 and will ensure that your tests do not interfere with your development data.Once you have started your containers, you may connect to the MySQL instance within your application by setting your 4 environment variable within your application's 5 file to 6.To connect to your application's MySQL database from your local machine, you may use a graphical database management application such as TablePlus. By default, the MySQL database is accessible at 7 port 3306.RedisYour application's 8 file also contains an entry for a Redis container. This container uses a Docker volume so that the data stored in your Redis data is persisted even when stopping and restarting your containers. Once you have started your containers, you may connect to the Redis instance within your application by setting your 9 environment variable within your application's 5 file to 1.To connect to your application's Redis database from your local machine, you may use a graphical database management application such as TablePlus. By default, the Redis database is accessible at 7 port 6379.MeiliSearchIf you chose to install the MeiliSearch service when installing Sail, your application's 8 file will contain an entry for this powerful search-engine that is compatible with Laravel Scout. Once you have started your containers, you may connect to the MeiliSearch instance within your application by setting your 4 environment variable to 5.From your local machine, you may access MeiliSearch's web based administration panel by navigating to 6 in your web browser.File StorageIf you plan to use Amazon S3 to store files while running your application in its production environment, you may wish to install the MinIO service when installing Sail. MinIO provides an S3 compatible API that you may use to develop locally using Laravel's 7 file storage driver without creating "test" storage buckets in your production S3 environment. If you choose to install MinIO while installing Sail, a MinIO configuration section will be added to your application's 8 file.By default, your application's 9 configuration file already contains a disk configuration for the 7 disk. In addition to using this disk to interact with Amazon S3, you may use it to interact with any S3 compatible file storage service such as MinIO by simply modifying the associated environment variables that control its configuration. For example, when using MinIO, your filesystem environment variable configuration should be defined as follows:
In order for Laravel's Flysystem integration to generate proper URLs when using MinIO, you should define the 1 environment variable so that it matches your application's local URL and includes the bucket name in the URL path:
You may create buckets via the MinIO console, which is available at 2. The default username for the MinIO console is 9 while the default password is 4.
Running TestsLaravel provides amazing testing support out of the box, and you may use Sail's 6 command to run your applications feature and unit tests. Any CLI options that are accepted by PHPUnit may also be passed to the 6 command:The Sail 6 command is equivalent to running the 6 Artisan command:By default, Sail will create a dedicated 3 database so that your tests do not interfere with the current state of your database. In a default Laravel installation, Sail will also configure your 1 file to use this database when executing your tests:
Laravel DuskLaravel Dusk provides an expressive, easy-to-use browser automation and testing API. Thanks to Sail, you may run these tests without ever installing Selenium or other tools on your local computer. To get started, uncomment the Selenium service in your application's 8 file:
Next, ensure that the 7 service in your application's 8 file has a 5 entry for 6:Finally, you may run your Dusk test suite by starting Sail and running the 7 command:Selenium On Apple SiliconIf your local machine contains an Apple Silicon chip, your 6 service must use the 9 image: 0Previewing EmailsLaravel Sail's default 8 file contains a service entry for MailHog. MailHog intercepts emails sent by your application during local development and provides a convenient web interface so that you can preview your email messages in your browser. When using Sail, MailHog's default host is 1 and is available via port 1025:When Sail is running, you may access the MailHog web interface at: http://localhost:8025 Container CLISometimes you may wish to start a Bash session within your application's container. You may use the 2 command to connect to your application's container, allowing you to inspect its files and installed services as well execute arbitrary shell commands within the container:To start a new Laravel Tinker session, you may execute the 3 command:PHP VersionsSail currently supports serving your application via PHP 8.2, 8.1, PHP 8.0, or PHP 7.4. The default PHP version used by Sail is currently PHP 8.2. To change the PHP version that is used to serve your application, you should update the 4 definition of the 7 container in your application's 8 file: 1In addition, you may wish to update your 7 name to reflect the version of PHP being used by your application. This option is also defined in your application's 8 file:After updating your application's 8 file, you should rebuild your container images:Node VersionsSail installs Node 18 by default. To change the Node version that is installed when building your images, you may update the 00 definition of the 7 service in your application's 8 file:After updating your application's 8 file, you should rebuild your container images:Sharing Your SiteSometimes you may need to share your site publicly in order to preview your site for a colleague or to test webhook integrations with your application. To share your site, you may use the 04 command. After executing this command, you will be issued a random 05 URL that you may use to access your application:When sharing your site via the 04 command, you should configure your application's trusted proxies within the 07 middleware. Otherwise, URL generation helpers such as 08 and 09 will be unable to determine the correct HTTP host that should be used during URL generation: 2If you would like to choose the subdomain for your shared site, you may provide the 10 option when executing the 04 command: 3
Debugging With XdebugLaravel Sail's Docker configuration includes support for Xdebug, a popular and powerful debugger for PHP. In order to enable Xdebug, you will need to add a few variables to your application's 5 file to . To enable Xdebug you must set the appropriate mode(s) before starting Sail: 4Linux Host IP ConfigurationInternally, the 14 environment variable is defined as 15 so that Xdebug will be properly configured for Mac and Windows (WSL2). If your local machine is running Linux, you should ensure that you are running Docker Engine 17.06.0+ and Compose 1.16.0+. Otherwise, you will need to manually define this environment variable as shown below.First, you should determine the correct host IP address to add to the environment variable by running the following command. Typically, the 16 should be the name of the container that serves your application and often ends with 17: 5Once you have obtained the correct host IP address, you should define the 18 variable within your application's 5 file: 6Xdebug CLI UsageA 20 command may be used to start a debugging session when running an Artisan command: 7Xdebug Browser UsageTo debug your application while interacting with the application via a web browser, follow the for initiating an Xdebug session from the web browser. If you're using PhpStorm, please review JetBrain's documentation regarding zero-configuration debugging.
CustomizationSince Sail is just Docker, you are free to customize nearly everything about it. To publish Sail's own Dockerfiles, you may execute the 25 command:After running this command, the Dockerfiles and other configuration files used by Laravel Sail will be placed within a 26 directory in your application's root directory. After customizing your Sail installation, you may wish to change the image name for the application container in your application's 8 file. After doing so, rebuild your application's containers using the 4 command. Assigning a unique name to the application image is particularly important if you are using Sail to develop multiple Laravel applications on a single machine: |