Blog Detail

20

Oct
Laravel Sail - A Docker Based Development Environment cover image

arrow_back Laravel Sail - A Docker Based Development Environment

Docker is an open-source containerization platform. It allows developers to package applications into containers and standardized executable components to combine application source code with the operating system (OS) libraries and dependencies, So they can run that code smoothly in any environment.

Laravel Sail is a lightweight command-line interface for interacting with Laravel’s default Docker development environment. It offers a great starting point for building a Laravel application utilizing PHP, MySQL, and Redis without requiring prior Docker experience.

Sail provides a Docker-powered local development experience for Laravel that is fit with macOS, Windows (WSL2), and Linux. Other than Docker, no software or libraries are needed to be installed on your local computer before using Sail. Sail’s simple CLI means you can start building your Laravel application without any previous Docker experience. Another benefit of Sail is that developers do not have to suffer about having the correct versions of the config software installed on their system. With a Sail container, if there is a break due to compatibility, you can just delete the container and create a new one, all carried away from your local environment and leaving no footprints on it.

This package includes two files. One is the docker-compose.yml file that holds your application’s Docker containers, and the other is a Sail script that offers you a CLI for interacting with these containers. The docker-compose.yml file is located in the root directory of your project, while the Sail script is located in your vendor/bin directory.

Requirements

Docker should be installed on the operating system on which it has to run.

If you use Windows, you’ll need to install and enable Windows Subsystem for Linux 2 (WSL2). It will permit you to run Linux binary executables natively on your Windows OS.

Make sure to configure Docker Desktop to use the WSL2 backend. Otherwise, no Docker experience is required.

Installation & Setup

Laravel Sail is automatically installed with all new Laravel applications, so you may start utilizing it immediately. During installation, you will be asked to choose which Sail-supported services your application will be interacting with.

Installing Sail Into Existing Applications

If you wanted to use Sail with an existing Laravel application, you can directly install Sail using the Composer package manager.

composer require laravel/sail --dev

After Sail has been installed, you can run the sail:install Artisan command. This command will publish Sail’s docker-compose.yml file to the root of your application:

php artisan sail:install

Lastly, to kickstart Sail, run the following command:

./vendor/bin/sail up

Using Devcontainers

If you wanted to develop within a Devcontainer, you can provide the –devcontainer option to the sail:install command. The –devcontainer option will tell the sail:install command to publish a default .devcontainer/devcontainer.json file to the root of your application:

php artisan sail:install --devcontainer

Analyzing some Sail commands

By default, Sail commands are invoked utilizing the vendor/bin/sail script that is included with all new Laravel applications, However, you can configure a bash alias that’s just one word to make our commands shorter. Once the Bash alias has been configured, you can execute Sail commands by simply typing sail.

We’ll replace the line vendor/bin/sail with a word sail:

alias sail='bash vendor/bin/sail'

To run all the containers in our docker-compose.yml file and get our application started, we employ the following command:

sail up

To start the containers in the background, we use:

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

When running artisan, composer, and npm commands, the sail alias must precede the
commands.

For example instead of running:

php artisan migrate

You should run:

sail artisan migrate

And instead of running:

//Running Artisan commands locally...
php artisan queue:work

You should run:

//Running Artisan commands within Laravel Sail...
sail artisan queue:work

Instead of this:

composer require laravel/sanctum

You should run:

sail composer require laravel/sanctum

Closing Notes:

By following the above process, you will be able to create and set up a new Sail Application. You analyzed Sail’s docker-compose.yml file and discovered some essential commands for running our Sail application and Container Setup. But there’s a lot more about this package. The More You Know The More You Realize You Don’t Know. So if you want to dig more about Sail, you can check its official documentation here.

Published at : 20-10-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project