Configure Laravel Sail in any Laravel project

What is Laravel Sail?

Laravel Sail is a lightweight command line interface for interacting with the Docker environment which is now the default environment for Laravel development. Sail provides a great starting point for building a Laravel application with PHP, MySQL, and Redis, and best of all, no previous Docker experience is necessary.

At its core, Sail is the docker-compose.yml file and navigation script that is stored in the root of your project. The browse script provides a CLI with convenient methods to interact with the Docker containers defined by the docker-compose.yml file.

Laravel Sail is compatible with macOS, Linux, and Windows (via WSL2).

We can use Laravel Sail for any Laravel project; for this we are going to follow the official documentation in which we have the steps.

Laravel Sail is a lightweight 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.

Any project in Laravel that is recent can use Sail

The first thing we need is to have the project in Laravel, in my case, to put you in context, I have a project in Windows, which I work with Laragon (nothing from Sail) and on a Mac with Laravel Sail; they are the same projects but running in different environments.

Install Docker

Although we are not going to interact much with Docker, this has been a fundamental dependency to be able to work with Sail; download it from the official website and install it.

Installing Sail in existing applications

To install Sail, we typically install Sail as a normal package in Laravel. If you are interested in using Sail with an existing Laravel application, you can simply install Sail using the Composer package manager. Of course, these steps assume that your existing local development environment allows you to install the Composer dependencies:

composer require laravel/sail --dev

It is a development dependency, so the --dev

Install Sail in the project

At the moment, we have the dependency on our project, but we have an artisan command to make the necessary modifications to our project:

php artisan sail:install

Start Sail

We already have EVERYTHING ready, so the next thing we do is start Sail with the command:

./vendor/bin/sail up   

You can review your .env and you will see that Sail already creates a user by default in the database that you have selected:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=sail
DB_PASSWORD=password

In your Docker you can see that the project is running:

Docker ejecutandose
Docker ejecutandose

Extra: Run in detached mode

If you want to run your project in detached mode or what is the same, that the terminal process does not stay running, but rather that it runs in the background, you pass the -d option

./vendor/bin/sail up -d

Extra:

If you see an error like the following when trying to start Sail:

ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?
        Supported filenames: docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml

It is that you did NOT install it in your project:

php artisan sail:install

And follow the steps outlined above.

Basic commands in Sail

Regarding the execution of commands, you can use both php and artisan commands and, of course, those provided by Sail thanks to the package that we previously installed using docker-compose.

 

Regarding the execution of commands, you can use both php and artisan commands and, of course, those provided by Sail thanks to the package that we previously installed using docker-compose.

  • docker-compose ps | sail ps : view the running containers.
  • docker-compose down | sail down : View the containers and delete them.
  • docker-compose up | sail up: Run the containers.
  • docker-compose up -d | sail up -d: Run the containers in background mode.

If you want more information about Laravel, how to use it in other environments, remember that you have the Laravel course from scratch.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 4d 06:17!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!