Advantages of using sail:
The sail’s main advantage is that the laravel docker manages all the project dependencies. The developer doesn’t require installing any project dependencies locally needed in the Laravel project. The sail installs all project dependencies with the docker in the background. The Laravel sail project contains the docker-compose.yml file that defines the different services required by the project. It makes the Laravel developer task easier. If any version compatibility error appears in the project, the sail command can easily remove the old container. Another important advantage of the sail is that the developers of the different operating systems can work together easily by using the sail environment.
Pre-requisites:
You have to install the docker and curl packages in the Linux operating system for creating the Laravel application using Sail.
Run the following commands to install Docker in Linux and check the install version of the docker.
$ docker --version
The following output shows the installed version of the docker.
Run the following commands to install the curl in Linux and check the installed version of the curl.
$ curl --version
The following output shows the installed version of the curl.
Create Laravel application with the builder script:
You have to log in as the root user or set the necessary permission for the docker.sock before downloading the builder script from the Laravel official site and executing it with the bash to create a Laravel application.
Run the following command to become a root user if you want to create the Laravel application from the root user account.
Or, run the following command to set the necessary permission of docker.sock for the current user. This tutorial uses this command to create a new Laravel application using docker and run using the sail.
Run the following command to start the docker before downloading the Laravel builder script.
Run the following command to pull the necessary Docker container images and bootstrap for the new Laravel application. The Laravel project named dockerProject will be created after executing the command successfully.
Run the following command to go to the project folder.
Run the following command to check the content of the project folder.
The following image shows the content of the project folder.
Laravel Sail basic commands:
Laravel Sail has many commands to start and stop the development environment of the Laravel project. The uses of different sail commands have been explained below.
Commands | Purpose |
---|---|
vendor/bin/sail up | It is used to bring the sail development environment up. |
vendor/bin/sail up -d | It is used to bring the sail development environment up in the background. |
vendor/bin/sail start | It is used to bring the sail development environment up that was previously stopped by using the sail stop command. |
vendor/bin/sail stop | It is used to stop the sail development environment previously initiated in the background. |
vendor/bin/sail down | It is used not only to stop the sail development environment but also to delete all associated resources created when the development environment was up. |
Run Laravel commands using sail:
All commands of the Laravel can be executed by using the sail command. The ways of executing some commonly used Laravel commands have been shown below.
You can execute any laravel artisan command by using the sail command in the following way.
The following sail command will display the installed version of the Laravel
The following sail command will perform the migrate operation and create tables in the database based on the migration files.
The following sail command will display the current PHP version of the Laravel project.
You can execute any composer command by using the sail command in the following way.
The following sail commands will update the composer and download the breeze for the Laravel project.
$ vendor/bin/sail composer require laravel/breeze –dev
Test the `sail up` command:
You have to set the necessary permission for the storage folder of the Laravel project before executing the ‘sail up’ command; otherwise, a permission error will appear. Run the following command to set permission for all users to the storage folder.
Now, run the following command to bring the sail development environment up and run the laravel project created before.
If the above command is executed properly, then type the following URL in the browser and check whether the Laravel project’s welcome page appears or not.
If the following page appears, the sail development environment is up, and the Learavel project runs successfully.
Conclusion:
The purpose and advantages of using the Laravel sail command have been explained in this tutorial. Using the sail command with the docker to create a Laravel project and running the Laravel project from the sail development environment has been explained to clear the Laravel sail concept for the readers.