Trying Out Laravel Sail

Posted on 12 December 2020 Reading time: 4 min read
Trying Out Laravel Sail

A few days ago, Laravel Sail was released as a lightweight docker environment for a Laravel application. This comes in the form of a docker-compose file created with new Laravel applications (From Laravel 8) and is supported on MacOs, Linux and Windows.

The Laravel documentation is an excellent resource for learning how to use Sail but this article is my step by step experience of trying out Laravel Sail.

I decided to create a new project to test this so I ran the command;

laravel new sail_testing

I ran into my first error;

    Server error: `GET http://cabinet.laravel.com/latest.zip` resulted in a `522 Origin Connection Time-out` response:
    <html>
    <head><title>522 Origin Connection Time-out</title></head>
    <body bgcolor="white">
    <center><h1>522 Origin Conne (truncated...)

This was because I was using an old version of the Laravel installer. I simply removed and re-installed the installer.

composer global remove laravel/installer
composer global require laravel/installer

I was now able to proceed with creating the new Laravel application. The first thing I noticed that a docker-compose file was added into the project. It was quite simple to understand. There were blocks for the services; Webserver, MySQL, Redis, Mailhog and optional ones like Selenium and Memcached.

If requiring Sail to be used in an existing application. You simply just have to install the composer dependency;

composer require laravel/sail --dev

I am not sure but it is possible the Sail will only work on Laravel application from version 8 since it requires, illuminate/contracts v8 and illuminate/support v8.0 but I might be wrong since for the purpose of this test I only tried it with a fresh Laravel 8 application.

Once installed, the next step is to publish Sail’s docker-composer.yml by running

php artisan sail:install

Now, the moment we have been waiting for. Getting Sail up and running!

./vendor/bin/sail up

Running the above command downloads all the images and creates all containers in the docker-compose.yml file. You can configure a bash alias to the sail command so you don’t have to type ./vendor/bin/sail up repeatedly.

alias sail='bash vendor/bin/sail

Useful commands

sail up     // start sail
sail up -d` // start in "detached" mode
sail down   // stop sail

When using Sail, some of the commands we run using Artisan, Composer and Npm will change slightly;

sail artisan run_command

You can also run php commands while using Sail

sail php --version
sail php index.php

Composer Laravel Sail container includes a Composer 2.x installation so new dependencies can be installed as follows;

sail composer require laravel/sanctum

NPM You can run node and npm commands with Sail

sail node --version
sail npm run prod

Services

MySQL - Once your containers have been started, the MySQL instance will be available to your application and can be accessed using a MySQL client to connect on localhost port 3306. A volume is created to ensure that the your data is persisted even when you shut down your containers. The database will use the same name as what was configured in your env file.

Redis: Like MySQL, a volume is also created for Redis to ensure that your redis data is persisted even when you shut down your containers. The Redis instance is configured using the REDIS_HOST configuration in your .env file.

There is a whole lot more you can do with Sail including running unit and feature tests, browser testing with Laravel Dusk and Previewing emails with Mailhog. Details on how to do this is clearly outlined in the Laravel documentation.

My Conclusions

Sail was quite simple to install and work with. This will no doubt provide a low barrier of entry for developers who have been reluctant to switch to Docker for their development environments.

I have tried out several Docker packages and libraries for Laravel development but Sail feels a lot cleaner without the need to add unnecessary configuration files within my applications.

As soon as started my docker environment with Sail, I was able to load the home page of my application. I initially had an issue with port 80 but that was because I was running Laravel Valet which uses Nginx on port 80. Once that was stopped however and I restarted my containers, I was able to view my application home page, connect to MySQL and connect to Redis.

For more on Laravel Sail, visit the documentation https://laravel.com/docs/8.x/sail