Laravel Sail

Laravel Sail: How To Containerize Your Application

Spread the love

Last Updated on June 8, 2022

Introduction

In any modern web development, a developer is required to bundle their source code and deploy it to a server so that it can be accessible to the public. One way is through virtualization. This concept allows a developer to rent a virtual machine from a cloud provider and configure it by installing all the necessary dependencies such as the web server, the firewall and a database server. This introduces the complexities of knowing all the various steps required to configure a successful virtual machine to host an application. What if there is a better way of reducing these complexities? In comes Laravel Sail to help us Containerize.

Containerization abstracts the need of knowing how to efficiently configure a virtual machine and host an application. It allows a developer to use their set of dependencies specific to their application and isolate it into a container. A container is just an isolated instance containing all the application dependencies. Think of it as a bucket. In this bucket, we can have our Laravel 8.0 application, a MYSQL database and a default PHP 7 instance. Another bucket could have a Laravel 9 application, a Mysql database, and PHP 8 instance. if we were to virtualize them, it would be a big headache to know which PHP version is the default and would cause one of the applications to break. To mitigate this, we can containerize both the applications and have them run as isolated instances on the same virtual machine. Laravel Sail allows us to containerize our Laravel application.

Containerization allows us to manage versioning in our development and production environments with ease.

What is Laravel Sail

Laravel Sail is a command-line interface that allows a developer to create and manage your Laravel application on a docker environment. Docker allows us to create isolated containers and automate the configurations and scaling of the application without the need to physically configure a server. Laravel Sail provides a developer with a local development environment consisting of PHP, MySQL, and Redis. This means that a developer is not tasked with creating any major configurations(PHP, MySQL, a local server etc) allowing one to hit the ground running with their development. Sail also allows one to ship their application with ease.

Prerequisites

Sail only requirement is to have Docker installed on your system.

If you are using windows, you will need to install and enable Windows Subsystem for Linux(WSL). It allows us to run Linux binary executables natively on Windows. Also, make sure to configure Docker Desktop to use WSL.

Installation and Setup

Laravel Sail is automatically installed with all new Laravel applications.

Installing Laravel Sail into Existing Application

If your application does not have Sail installed, you can install it using the Composer Package manager. We can use the following command:

composer require laravel/sail --dev

Once Sail has been installed, you may run the sail:install Artisan command. This command publishes the docker-compose.yml file to the root of your application. The docker-compose.yml file contains the docker configurations that will help containerize our application.

php artisan sail:install

The docker-compose.yml file generated is as shown below

Laravel Sail
docker-compose file

To start Sail, we can use the following command:

./vendor/bin/sail up

We can configure a Bash alias that allows you to execute Sail Commands with ease without having to specify the path for the sail executable file. The following command can be used to set the alias.

alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'

We can now execute the command using the sail command.

sail up

Starting and Stopping Laravel Sail

Docker-compose.yml file contains a variety of Docker containers that are part of the Laravel application. Each of these containers is an entry within the services configuration in the file. Note: Before starting sail, ensure that no web server or database server is currently running on your system.

sail up

To start all the Docker containers in the background, you may use the command:

sail up -d

Once your application is up and running, you can visit http://localhost to view it.

To stop the containers, press Control + C on your keyboard. If they are running in the background, then run:

sail down

Running Laravel Sail Commands

An application that runs on the container, by default, is isolated from the system and as a result, cannot access main system hardware. We cannot run the default artisan commands directly. If we want to run the commands, we can use sail to run the commands directly on the container.

For example, if I wanted to migrate the database, we would use

sail artisan migrate

The default command syntax is

# run artisan command using sail
sail artisan <command>

If we want to run composer commands, we will need to append the sail command before the actual composer command. For example

sail composer install
sail composer update
sail composer require laravel/passport

As we can see above, we are able to install the application dependencies using composer, update the dependencies and also install Laravel Passport as a dependency.

We can also run third party commands directly on our container using sail. For example, npm, yarn, or node commands.

sail yarn install
sail npm install
sail node --version

Conclusion

Sail provides an easy way for containerizing a Laravel application. We understood why containers are essential and also how to build a container from the docker-compose.yml file. We also leant how to run a container and execute commands directly on the container using the sail command. Sail provides a very easy way to interact with docker and containers for beginners with a Laravel application. I hope this guide is able to shed some light on what Laravel Sail is and how to use Laravel Sail. To find out more about Sail, you can check out the official documentation. If you have any questions, feel free to drop them in the comment section and I will try and answer them. Thank you for reading.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *