Begin New Project with Laravel Sail and Docker on Window 10
Laravel Sail . Laravel 8
You may already know the powerful features and benefits of using Docker in your development and production environment. Docker is quite popular these days in the world of developers, software engineers, and dev ops. There are many definitions of docker which have been defined by different sources. I will go with the simple one as per understanding "Docker is a tool help you to well pack your software with the support of OS virtualization technique".
Imagine you want to start your project, I mean any project like java, laravel, node js, python, etc. The very first step you have to do is to set up the necessary software which is used for supporting the development environment before you even start a single line of your code. For instance, you want to start a laravel project. you have to set up PHP, MySQL, Apache, etc. This is a step that everyone cannot avoid. Furthermore, working on a project with a team installing a different version of that software will lead to a very headache situation. "The code is working in my PC, but not yours" and "The code is working in your PC, but not mine". Docker is coming to solve the above problems. All you have to do is installing docker and docker will do all the rest of the works.
For laravel with the new release of version 8, they introduce a new feature called "Laravel Sail". This tool is used to help you bootstrap your laravel project setup with docker liked to describe in the official document "Sail is a built-in solution for running your Laravel project using Docker". As mentioned they claim you don't have to have experience with docker using Sail, but in my opinion "YES, you have to have at least some knowledge of docker before starting with laravel sail". Here is the youtube playlist you can try to start from the beginning with docker https://www.youtube.com/watch?v=CYyUCJad954&list=PLRAV69dS1uWTJLvDP4Veld5F05rJAmOcp
In this post, I am going to provide step by step how you can begin your project with the help of laravel sail in window 10 home edition.
STEP 1: Installing Docker Desktop
The very first is to install docker desktop from the official website of docker https://www.docker.com/products/docker-desktop. All you have to do is download and click next, next, and next until finish.
STEP 2: Installing Ubuntu into Your Window
Sound pretty crazy right, installing ubuntu into your window. Since docker is built based on Linux compatibility in mind, you have to have Linux distribution inside your window and you can do so thanks to the WSL (Window Subsystem for Linux) feature which allows you to run Linux binary executables natively on Windows 10. All you have to do is install and enable. Information on how to install and enable WSL2 can be found within Microsoft's developer environment documentation.
You have to follow 6 manual steps introduced by Microsoft to install and enable WSL2
- Enable the Windows Subsystem for Linux
- Check requirements for running WSL 2
- Enable Virtual Machine feature
- Download the Linux kernel update package
- Set WSL 2 as your default version
- Install your Linux distribution of choice
STEP 3: Configure Docker Desktop to Use WSL2 Backend
After successfully installed ubuntu in step 2, when searching in the program file you will see the program "Ubuntu 20.04 LTS" as below
- Open docker desktop, click on setting icon, and then on the left menu resources and then WSL INTEGRATION and finally enable integration as below image
STEP 4: Manage Docker as a Non-Root User
In order to make docker work properly with the user, you have created when you first launch the Linux distribution tool for the first time. You have to add the user to the group docker as below
$ sudo groupadd docker // Replace name of user you created with $USER $ sudo usermod -aG docker $USER
STEP 5: Let Start Your Laravel Sail
All of the above steps are just the installation and configuration to get docker working well in the window10 environment. Now let open linux and issue a command
curl -s https://laravel.build/example-app | bash
If your distraction doesn't recognize curl just install it and re-run the command again. Of course, you can change "example-app" to any name you want. The laravel application's directory will be created within the directory you execute the command from.
After the project has been created, you can navigate to the application directory and start Laravel Sail. Laravel Sail provides a simple command-line interface for interacting with Laravel's default Docker configuration
cd example-app ./vendor/bin/sail up
The first time you run the Sail
up command, Sail's application containers will be built on your machine. This could take several minutes. Don't worry, subsequent attempts to start Sail will be much faster.
Once the application's Docker containers have been started, you can access the application in your web browser at http://localhost and in docker desktop, you will see container running as below image
You can use the laravel sail command below to start/stop the docker containers
./vendor/bin/sail up ./vendor/bin/sail down
STEP 6: Start Your Coding with Visual Studio Code
Of course, you will need to be able to modify the Laravel application files that were created within your WSL2 installation. To accomplish this, you have to have Microsoft Visual Studio Code with the extension Remote Development enabled.
Once these tools are installed, you may cd to your project directory in Ubuntu and type
and it will open VS Code editor with your source code project inside as below. You will notice some of the highlights in the screenshot which is much different from the normal laravel project.
You can see that laravel sail automatically generates the docker-compose file for you and you can change it anytime you want. You also can see that the source code is in the remote Linux Distribution (Ubuntu 20.4) via WSL and it does not exist on your local PC.
THANK YOU !!!