l2t

Laravel8 Sail

What is Laravel Sail? How to use Laravel Sail?

What is Laravel Sail?

Laravel Sail is a command line interface which allows you to create and manage your Laravel app on docker environment. If you do not know anything or know bit about docker this command line interface allows you to manage your local docker environment.

How does Laravel Sail work?

Advantage of using sail is that all your project dependencies are managed by docker. For example: if your project requires:

  • PHP
  • MySQL
  • Redis
  • Other dependencies

Sail will install all of your project dependencies using docker behind the scene. You do not need to install any of above dependencies on your local machine in order to work with Laravel project.

Let's look at the following diagram to understand how Laravel Sail work:

In your Laravel Sail project you will find a file called docker-compose.yml. This file basically defines different services that your project requires. For example, if your Laravel project requires:

  • PHP
  • MySQL
  • Redis

Then you will see all of above dependencies under services in docker-compose.yml file. Basically, it tells sails that I want to create all containers mentioned under services in docker-compose.yml file.

When sails runs it will pull all the required images from dockerhub and then create container for each service defined in your docker-compose.yml file. You can add more services if you need based on your project we will learn about them in details later in this tutorial.

You save ton of your time figuring out local installation for php, redis or mysql or some other dependencies on your machine.

How to install Laravel Sail?

You need to have following dependencies installed on your mac or linux machine before you go ahead with Laravel Sail:

Once you have above dependencies installed let's start installing a new laravel project using Laravel Sail. Open your terminal window and run following commands one by one:

# install laravel using curl
# change example-app to your project name
curl -s "https://laravel.build/example-app" | bash

# once installed run docker containers using
./vendor/bin/sail up

# to destroy running container run
./vendor/bin/sail down​

So far, you have installed Laravel project using bash script provided by Laravel. Once project is installed locally you will find sails in following location:

./vendor/bin/sail​

How to install Laravel Sail in existing Laravel project?

If you already have Laravel project and you are not using Laravel Sail you can basically run following commands to install Sail in your existing project:

# install composer dependency for sail
composer require laravel/sail --dev

# publish Sail's docker-compose.yml file to the root of your application
php artisan sail:install​

How to run your Laravel app using Sail?

Once you have Sail installed in your Laravel project you need to know some of the following important commands in order to interect with your dockerized Laravel app.

Keep in mind that all your services run on different docker containers and therefore you wont be able to run Laravel commands directly from your terminal therefore we use Sail command line interface.

Laravel Sail basically allows you to run commands inside your running container without logging in to container using ssh. You will understand what I mean by that once we start running following commands:

# Run Laravel Project Locally
# Following command will run all containers
# Defined in docker-compose.yml file
./vendor/bin/sail up

# If you want to stop all running containers
./vendor/bin/sail stop

# To check what containers are running run
# It will list all containers if any of them running
./vendor/bin/sail

# To destroy your running laravel app run
# This command will destroy all of your docker containers
./vendor/bin/sail down

# If you want to run unit tests
./vendor/bin/sail test
./vendor/bin/sail test --group orders​

How to run Laravel artisan commands using Sail?

As I said earlier your services runs inside docker containers and therefore executing Laravel artisan command will not work if you are using Laravel Sail however using sail you can run all of your artisan commands.

Here is how you can run artisan or other commands using Laravel Sail:

# run artisan command using sail
# use following syntax for your artisan commands
./vendor/bin/sail artisan <command>

# example
./vendor/bin/sail artisan queue:work

# if you want to execute php command or script use
# following syntax for php commands
./vendor/bin/sail php <command>

# example
./vendor/bin/sail php --version

# to run composer commands using sail
# use following syntax
./vendor/bin/sail composer <command>

# exaamples
./vendor/bin/sail composer install
./vendor/bin/sail composer update
./vendor/bin/sail composer require laravel/sanctum

# if your project runs on npm or node or yarn use
# following sytaxes
./vendor/bin/sail npm <command>
./vendor/bin/sail yarn <command>
./vendor/bin/sail node <command>

# examples
./vendor/bin/sail yarn install
./vendor/bin/sail npm install
./vendor/bin/sail node --version

# if you want to ssh into container
./vendor/bin/sail bash

# if you want to check container logs
./vendor/bin/sail logs -f

# if you want to restart specific service
./vendor/bin/sail restart <service_name>
./vendor/bin/sail restart mysql​

I hope you like this tutorial stay tune for my upcoming articles on Laravel 8. Please like and share my article if you keep wanting me to write awesome articles. Thank you.