Laravel Sail & Docker

Laravel Sail is a light-weight properties interface for interacting with Laravel's default Docker development environment.


Introduction

Laravel Sail is a light-weight properties interface for interacting with Laravel's default Docker development environment. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience.

At its heart, Sail is the docker-compose.yml file and the sail script that is stored at the root of your project. The sail script provides a CLI with convenient methods for interacting with the Docker containers defined by the code docker-compose.yml file.

Laravel Sail is supported on macOS, Linux, and Windows (via WSL2).


What is the Docker?

Docker is a tool for running applications and services in small, light-weight containers which do not interfere with your local computer's installed software or configuration. This means you don't have to worry about configuring or setting up complicated development tools such as web servers and databases on your personal computer. To get started, you only need to install Docker.


Installation

Laravel Sail is automatically installed with all new Laravel applications so you may start using it immediately. To learn how to create a new Laravel application, please consult Laravel's installation documentation for your operating system.


Installing Sail Into Existing Applications

  1. If you are interested in using Sail with an existing Laravel application, you may simply install Sail using the Composer package manager. Of course, these steps assume that your existing local development environment allows you to install Composer dependencies:
  2. composer require laravel/sail --dev
  3. After Sail has been installed, you may run the sail:install Artisan command. This command will publish Sail's docker-compose.yml file to the root of your application:
  4. php artisan sail:install
  5. Finally, you may start Sail. To continue learning how to use Sail, please continue reading the remainder of this documentation:
  6. ./vendor/bin/sail up

Configuring A Bash Alias

  1. By default, Sail commands are invoked using the vendor/bin/sail script that is included with all new Laravel applications:
  2. ./vendor/bin/sail up
  3. However, instead of repeatedly typing vendor/bin/sail to execute Sail commands, you may wish to configure a Bash alias that allows you to execute Sail's commands more easily:
  4. alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
  5. Once the Bash alias has been configured, you may execute Sail commands by simply typing sail. The remainder of this documentation's examples will assume that you have configured this alias:
  6. sail up

Starting & Stopping Sail

Laravel Sail's docker-compose.yml file defines a variety of Docker containers that work together to help you build Laravel applications. Each of these containers is an entry within the services configuration of your docker-compose.yml file. The laravel.test container is the primary application container that will be serving your application.

Before starting Sail, you should ensure that no other web servers(xampp/wamp) or databases are running on your local computer. To start all of the Docker containers defined in your application's docker-compose.yml file, you should execute the up command:

sail up

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

sail up -d

Once the application's containers have been started, you may access the project in your web browser at: http://localhost.

To stop all of the containers, you may simply press Control + C to stop the container's execution. Or, if the containers are running in the background, you may use the stop command:

sail stop

To integrate with database migrations:

  1. Run the following command to generate migration tables with sail.(Make sure you have created alias for sail)
  2. sail artisan migrate
  3. Run the following command to generate fresh migration tables.(This will remove your current tables from Database and create new one.)
  4. sail artisan migrate:fresh
  5. Run following database seeding command to create fake users in database for CRUD app.
  6. sail artisan db:seed
  7. You are ready to use CRUD app with docker & sail👍🏻

How to run the Materialize with Sail?

  1. You can run our template in the docker environment using the sail. After, successfully installing docker in your system run the below command to install sail and composer in the template.
  2. docker run --rm \
      -u "$(id -u):$(id -g)" \
      -v "$(pwd):/var/www/html" \
      -w /var/www/html \
      laravelsail/php82-composer:latest \
      composer install --ignore-platform-reqs
  3. Find .env.example file at root folder and rename it to .env by running below command Or also can manually rename it:
  4. For Windows:

    ren .env.example .env
    For Max OS and Ubuntu:
    mv .env.example .env
  5. To serve the application, you need to run the following command.
  6. sail up -d
  7. Run the following command to generate the key:
  8. sail artisan key:generate
  9. By running the following command, you will be able to get all the dependencies in your node_modules folder:
  10. sail yarn
  11. When you run the dev scripts, all of your application's SCSS/CSS and JavaScript assets will be compiled and placed in your application's public directory:
  12. Once the application's containers have been started, you may access the project in your web browser at: http://localhost 🥳.
  13. To watch your template's Pages, SCSS and JavaScript, you need to run below sail command:
  14. sail npm run watch-poll

    And, All set!!

© 2017- Pixinvent, Hand-crafted & Made with ❤️