Laravel Sail: A guide to using Laravel and Docker
5 mins read

Laravel Sail: A guide to using Laravel and Docker

Docker containers are used in the Laravel Sail development environment for Laravel applications. It offers a practical and standardized method for configuring and overseeing your Laravel applications, guaranteeing a smooth development process in many contexts. You will learn about Laravel Sail, its advantages, and how to apply it to optimize your workflow for Laravel development in this tutorial.

A Comprehensive Guide to Laravel Sail

Prerequisites

Before embarking on this guide, ensure you have the following prerequisites:

  • Docker Desktop installed
  • A Laravel project
  • A text editor or IDE

Laravel Sail is a development environment for Laravel applications that leverages Docker to provide a consistent and isolated development experience. It simplifies the setup and management of your Laravel environment, allowing you to focus on building your application.

Getting Started with install Laravel

To get started with Laravel Sail, you’ll need to a new Laravel application. you can install new Laravel using installer

composer global require laravel/installer
laravel new my-app

Next, navigate to your application directory and initialize Laravel Sail:

cd my-app

Secondly, You need to setup / install the Docker desktop Application for setting up the Sail. For setup / Install the Docker desktop refer to this link. This link provides you with a complete guide to how to install Docker to Ubuntu.

Installing Sail Into Existing Applications

To integrate Sail with an already-existing Laravel application, all you need to do is install Sail using the Composer package manager. install the necessary Composer dependencies. Here is the command to install

composer require laravel/sail --dev

After Sail has been installed, you may run the sail:install Artisan command. Sail will create a Dockerfile and docker-compose.yml file in your application directory. These files define the Docker containers and their configurations.

php artisan sail:install

Finally, your sail has been install.

Running Your Application with Laravel Sail

./vendor/bin/sail up

To start all of the Docker containers in the background, you may start Sail in “detached” mode:

./vendor/bin/sail up -d

Finally, you may start Sail. To continue learning how to use Sail, please continue reading the remainder of this documentation:

To run your Laravel application in the Docker environment, simply run the following command:

sail up -d
Laravel Sail: A guide to using Laravel and Docker - TechnologiesPosts

This command will start the Docker containers and run your application on port 80. You can access your application by visiting http://localhost in your browser. Before run this command you will get an error

Command ‘sail’ not found, but can be installed with:
sudo apt install bsdgames

Laravel Sail: A guide to using Laravel and Docker - TechnologiesPosts

. To resolve this error. You need to create the alias. Alias is the short name of the command. Below is the command for an alias.

alias sail='bash ./vendor/bin/sail'

Above

Laravel Sail | Alias command

Managing Your Environment

Laravel Sail provides a number of commands to help you manage your development environment. These commands include:

  • sail build: Builds the Docker images for your application.
  • sail down: Stops and removes the Docker containers.
  • sail logs: Shows the logs for the Docker containers.
  • sail restart: Restarts the Docker containers.

Managing the Container

Sail provides several commands for managing the Docker container:

  • sail stop: Stops the container
  • sail restart: Restarts the container
  • sail logs: Displays the container logs
  • sail shell: Opens a shell within the container

Customizing Your Environment

You can customize your Laravel Sail environment by modifying the Dockerfile and docker-compose.yml files. For example, you can add additional PHP extensions, install specific software packages, or configure your database settings.

Conclusion

Laravel Sail is a powerful tool that can greatly simplify the setup and management of your Laravel development environment. By leveraging Docker, Sail provides a consistent and isolated environment that allows you to focus on building your application. Whether you’re a beginner or an experienced Laravel developer, Laravel Sail is a valuable tool that can enhance your development workflow.

Additional Features

Laravel Sail offers additional features to enhance the development experience:

Database seeding: Seed your database with sample data using `sail artisan db:seed`.
Artisan commands: Run Artisan commands within the container using `sail artisan `.
Mailhog: A mailcatcher for testing email functionality.

Benefits of Using Laravel Sail

Laravel Sail provides numerous advantages for Laravel developers:

Easy Docker integration: Laravel Sail provides an easy way to integrate Docker into your Laravel project. Docker containers encapsulate the development environment, ensuring consistency across different environments and reducing the “runs on my machine” issue.

Development environment separation: Using Sail, each Laravel project can have an independent development environment to avoid conflict between the project and configuration. This isolation increases security and reproducibility during development.

Cross-platform compatibility: Docker containers built with Sail are cross-platform in nature, allowing developers to work on Laravel projects using different operating systems (e.g. Windows, macOS, Linux) and achieve better results. This change allows teams with multiple locations to work together efficiently.

Share your Love