In the previous post, we learned how to Install the Laravel dev environment using Docker, easily with Sail , in this article we will go deeper into which Sail to use.
Alias configuration for Sail
By default, Sail calls the vendor/bin/sail
script to launch the command line, for example
1 2 | ./vendor/bin/sail up |
However, as you can see, it is quite cumbersome. We will create an alias for it by typing this in the terminal
1 2 | <span class="token builtin class-name">alias</span> <span class="token assign-left variable">sail</span> <span class="token operator">=</span> <span class="token string">'bash vendor/bin/sail'</span> |
So, the command sail
is the whole phrase above and we can quickly type sail up
To avoid having to type in the alias every time we boot, we should insert it into the profile. Like I use zsh, I put it in ~/.zshrc
Start and shut down the Sail
Laravel Sail’s docker docker-compose.yml
file identifies a Docker container that works together to help you build Laravel applications. Each of these containers is an item in the services
configuration within your docker-compose.yml
.
Before starting Sail, you should make sure that there is no webserver (Nginx, Apache, …) or any other database (MariaDB, MongoDB, …) running on your local. To start all the Docker containers declared in the docker-compose.yml
file, simply execute the up command:
1 2 | sail up |
This command works almost like php artisan serve
, but carries a lot more things like redis
, php8
, mysql8
, …
I prefer it to run in the background, so I use the command
1 2 | sail up -d |
After the project has started, we can access our product website right at http: // localhost.
Just like php artisan serve
, to stop Sail, we just need to press Ctrl + C, if you are like me, run in the background, you need to use the command
1 2 | sail down |
Command line interface (CLI) with Sail
When we use Laravel Sail, our application is executed inside a separate Docker container, a private virtual operating system, independent of our own. But okay, it’s hard to let Laravel worry. Laravel Sail provides shortcuts to execute commands directly on your project, without having to run into the Docker container.
Throughout your Documentation , you will see most of the command line examples, which are the need to manipulate Composer, Artisan, Node / NPM. And Sail provides available shortcuts for you to perform operations like this. Instead of composer
, php artisan
, npm
we have sail composer
, sail artisan
, sail npm
respectively.
Container CLI
Sometimes we still need to get into the Docker container, just type:
1 2 | sail shell |
If we want to open the Tinker to manipulate, we have
1 2 3 4 5 6 | sail artisan tinker <span class="token comment"># hoặc</span> sail tinker |
Above are some basic operations with Sail, in the next post I will introduce more about the config files of Docker. See you again