php

Create Laravel application using sail and docker

PHP, MySQL, and Redis are required to install for building a Laravel project in the Linux operating system. Laravel developers can create any Laravel project without setting any configuration for PHP, MySQL, and local development servers using Sail with Docker. Docker tool is used to create a container for the application to run the application in any operating system without setting any configuration. The advantages of using sail and the basic uses of Sail with the Docker to create a Laravel application have been shown in this tutorial.

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.

$ sudo apt-get install docker.io
$ 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.

$ sudo apt-get intall 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.

$ sudo -i

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.

$ sudo chmod 666 /var/run/docker.sock

Run the following command to start the docker before downloading the Laravel builder script.

$ sudo systemctl start docker

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.

$ curl -s https://laravel.build/dockerProject | bash

Run the following command to go to the project folder.

$ cd dockerProject

Run the following command to check the content of the project folder.

$ ls

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.

vendor/bin/sail artisan <command>

The following sail command will display the installed version of the Laravel

$ vendor/bin/sail artisan  --version

The following sail command will perform the migrate operation and create tables in the database based on the migration files.

$ vendor/bin/sail artisan migrate

The following sail command will display the current PHP version of the Laravel project.

$ vendor/bin/sail php  --version

You can execute any composer command by using the sail command in the following way.

vendor/bin/sail composer <command>

The following sail commands will update the composer and download the breeze for the Laravel project.

$ vendor/bin/sail composer update
$ 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.

$ sudo chmod -R 777 storage

Now, run the following command to bring the sail development environment up and run the laravel project created before.

$ vendor/bin/sail up

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.

http://127.0.0.1

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.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.