Home > Backend Development > PHP Tutorial > Using Docker with Laravel Sail

Using Docker with Laravel Sail

Susan Sarandon
Release: 2024-12-24 15:45:17
Original
522 people have browsed it

Using Docker with Laravel Sail

Hello Artisans,

Laravel Sail is a lightweight command-line interface (CLI) used for interacting with Laravel's default Docker environment. It provides developers an easy way to get started with Laravel in a fully containerized development environment without the hassle of manually setting up services like MySQL, Redis, or a queue worker in docker container.

In this guide, I'll walk you through the process of setting up and using Laravel Sail, highlighting its key features and benefits.

What is Laravel Sail?
Laravel Sail is a Docker development environment for Laravel. It includes everything you need to start building Laravel applications, such as PHP, MySQL, Redis, and more. Sail is particularly useful because it abstracts Docker complexities, allowing developers to focus on building applications.

Prerequisites
Before we start how to use Laravel Sail, make sure you have these setup in your machine:

1. Docker installed: Download and install Docker from its official website.
2. Composer: Laravel Sail requires Composer to install dependencies. You can download Composer from getcomposer.org.

I hope you have installed all the requirements.

Guide to Using Laravel Sail

Step 1: Install a New Laravel Project
Run the following command to create a new Laravel application and include Sail in your project dependencies:

composer create-project laravel/laravel laravel-sail-app
cd laravel-sail-app
Copy after login

Step 2: The next step is to install Laravel Sail in your project.

composer require laravel/sail --dev
Copy after login

After the installation is finished, we need to publish its Docker configuration files. You can do it by running the following command.

php artisan sail:install
Copy after login

Note: When a prompt appears you need to select your required services such as MySQL, Redis, or PostgreSQL.

Step 3: Start the Sail Environment
To start the development environment, use the following command:

./vendor/bin/sail up
The sail up command will start all the necessary containers for your Laravel application. By default, Sail runs containers for PHP, MySQL, and Nginx.
Copy after login

Step 4: Access Your Laravel Application
Once the Sail environment is running, you can access your application by navigating to http://localhost in your browser.

Step 5: Run Artisan Commands with Sail
Laravel Sail allows you to run Artisan commands in the Docker environment seamlessly. For example:

./vendor/bin/sail artisan migrate
Copy after login

You can also simplify your workflow by aliasing Sail:

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

Now you can run commands like:

sail up
sail artisan migrate
Copy after login

Step 6: Manage Containers
You can stop the containers with:

sail down
Copy after login

This command shuts down the Docker containers, preserving data in volumes like your database.

Step 7: Customizing the Docker Configuration
The docker-compose.yml file generated by Laravel Sail is highly customizable. You can add or remove services, change environment variables, or tweak resource allocations.

Now you are ready with a Docker container to interact with your laravel application.

Benefits of Using Laravel Sail

1. Easy to Use: Sail simplifies the Docker experience with straightforward commands.
2. Preconfigured Environment: Sail provides a preconfigured setup, saving you time during development.
3. Cross-Platform Support: Works seamlessly on Linux, macOS, and Windows (via WSL 2).
4. Supports Multiple Services: Easily integrate databases, caching layers, and queue workers.

Commonly used Sail Commands:

  • sail up: Start the Docker containers
  • sail down: Stop the Docker containers
  • sail artisan: Run Artisan commands
  • sail npm install: Install npm dependencies
  • sail test: Run Laravel test suite
  • sail mysql: Access MySQL command-line interface

Conclusion:
It's the best for developers who want to have a hassle-free development environment. Using the Docker-based approach, you can ensure consistency across all development machines, thus making collaboration easier. You will be able to set up and start using Laravel Sail to boost your productivity and focus on building great applications through this guide.

Happy Reading! ? ❤️
Happy coding! ? ❤️

The above is the detailed content of Using Docker with Laravel Sail. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template