Logo

dev-resources.site

for different kinds of informations.

Using Docker with Laravel Sail

Published at
12/5/2024
Categories
laravel
docker
php
womenintech
Author
snehalkadwe
Categories
4 categories in total
laravel
open
docker
open
php
open
womenintech
open
Author
11 person written this
snehalkadwe
open
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
Enter fullscreen mode Exit fullscreen mode

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

composer require laravel/sail --dev
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Step 4: Access Your Laravel Application
Once the Sail environment runs, 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 seamlessly in the Docker environment. For example:

./vendor/bin/sail artisan migrate
Enter fullscreen mode Exit fullscreen mode

You can also simplify your workflow by aliasing Sail:

alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
Enter fullscreen mode Exit fullscreen mode

Now you can run commands like:

sail up
sail artisan migrate
Enter fullscreen mode Exit fullscreen mode

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

sail down
Enter fullscreen mode Exit fullscreen mode

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! 🦄 ❤️

womenintech Article's
30 articles in total
Favicon
Tech Interviews: The Hustle Behind Tech Interview Prep
Favicon
What’s it like working with men in programming?
Favicon
Build Bug-Free Applications using Laravel Unit Testing
Favicon
Breaking the Glass Ceiling: Women Architects Shaping Free and Inclusive Spaces
Favicon
2025, Please, Be Gentle
Favicon
Women Founders; the in between.
Favicon
Year in Review - 2024 Edition
Favicon
Conquering Node.js Hurdles
Favicon
Um pouco da minha experiência profissional e voluntária na área de TI
Favicon
Girl in Tech
Favicon
Mentoring for Women in IT: How to Get the Most Out of It
Favicon
From Bartender to Developer: My Self-Taught Journey in Tech
Favicon
Why Tracking Pain Matters: Launching Paindai for Chronic Pain Sufferers
Favicon
AI Fatigue Has Entered the Chat: How to Innovate Without Alienating Your Brand
Favicon
Using Docker with Laravel Sail
Favicon
My Struggle as a Self-Taught Developer
Favicon
Programming Inspiration & Motivation on LinkedIn
Favicon
The perfect blend: Coffee and Cyber
Favicon
Tech Made Simple: The Key to Understanding What Really Matters.
Favicon
Indicação Women Tech Global Awards 2024
Favicon
Becoming a Blockchain Baddie: Week 1 of My Shefi x Celo Journey 💅
Favicon
Calling All Women in STEM! Let’s Connect!
Favicon
Java Variables and Data Types
Favicon
Which Coding Course is Best?
Favicon
A technical poem- prehistory and looking to the future.
Favicon
🎉👩🏻‍💻Celebrando o Ada Lovelace Day
Favicon
How I Entered Web Development for the Wrong Reasons (and Stayed for the Right Ones)
Favicon
he Benefits of Using Organic and Natural Lip Glosses
Favicon
How My Friend Inspired Me to Reignite My Passion for AI Through Lifelong Learning😊
Favicon
Creating an Azure IoT Hub

Featured ones: