run laravel application using Docker in windows

How To Run Laravel 8 Using Docker In Windows – For Beginners

If you are looking for a PHP framework and choose Laravel, you have chosen it correctly. It’s trendy among the other PHP Frameworks. Laravel is a well-documented framework. The laravel documentation provides all the concept one needs to understand it. But it can be a bit tricky for first-time users who started out using it.

Worry not; I will help you out. In this article, we are going to see how to run laravel 8 using Docker in Windows.

Now, for those new to laravel and docker, let’s talk about Laravel and Docker.

What is Laravel?

Laravel is a PHP web application framework. It is a scalable framework that follows the MVC ( Model-View-Controller ) Model. It’s an open-source web framework and has some amazing features such as dependency injection, queues, and scheduled jobs, unit testing, and more.

What is Laravel Sail?

Laravel Sail is a CLI ( Command-line Interface) for interacting with Laravel’s Docker environment. This means that you don’t need to use Docker to create different containers. With sail, you can build Laravel applications without you needing to have prior experience in Docker.

What is Docker?

Docker is an open-source containerization platform for building, running, and shipping applications in such a manner that it can work on your development machine and function the same way in other machines.

What is Docker good for?

Docker is a container-based tool to create, deploy, run applications and services in small, light-weight “containers” which do not interfere with your local computer’s installed software or configuration.

  1. It is lightweight and fast.
  2. It is portable. The Docker containers can run on a local laptop, on physical or virtual machines in a data center, on cloud providers, or in a mixture of environments.

Getting started on windows

Prerequisite

Now before you install docker and start using it. You must complete the following steps:-

  • Must have windows 10 installed and using version 1903 or higher.
  • Need to activate WSL ( Windows Subsystem for Linux ) for docker to work.
  • Download and install the Linux kernel update package.

If you are missing or haven’t done any of the above-mentioned steps, follow this link Step-by-Step Guide To Install WSL 2 On Windows 10.

Install Docker for Windows

Step 1: Install Docker Desktop

  1. Go to Docker Official website and download Docker Desktop for Windows
  2. Follow the installation wizard and when prompted, make sure to select the Enable Hyper-V Windows Features or the Install required Windows components for WSL option in the Configuration page.

Step 2: Set up Docker Settings

After installing the docker desktop, open the docker application and go to settings.

In Settings > General, make sure to enable the “Use the WSL based engine”. ( This option is enabled by default If you have installed Docker Desktop on a system that supports WSL 2 )

enable docker wsl 2 engine

Now, In Settings > Resources > WSL INTEGRATION, make sure to turn on the “Enable integration with additional distros”.

docker wsl 2 integration

Then click Apply & Restart.

Run the laravel application

Step1: Get into your Linux distribution

Open command prompt and run command – wsl

Take a closer look! The current path is pointing to your Windows User Directory.

wsl-2-starting-linux

Now, it is recommended to store source code and other bind-mounted data into Linux containers in the Linux file system rather than the Windows file system.

So, execute the command – ” cd ~ ” to get into the Linux User Directory.

WSL 2 changing to linux directory

Step 2: Create a new Laravel Project

To create a new laravel project, execute this command:

curl -s https://laravel.build/example-app | bash

You can change “example-app” to anything you like.

Choosing Your Sail Services – When you create a new Laravel application via Sail, you can use the “with" query string variable to choose which services should be configured in your new application’s file. Available services include mysqlpgsqlmariadbredismemcachedmeilisearchselenium, and mailhog.

curl -s "https://laravel.build/example-app?with=mysql,redis,selenium" | bash

If you do not specify which services you need, a default stack of mysqlredismeilisearchmailhog And selenium will be configured.

Step 3: Use the sail command to run the Application

First, go into your laravel app directory.

cd example-app

then enter this command

./vendor/bin/sail up -d
NOTE:
If it is a fresh install, docker requires downloading all dependency images.So, it will take some time to build your Container for the first boot up.

After running the above command, type “http://localhost” in your browser URL.

laravel- homepage using docker

To close the running application, enter this command:

./vendor/bin/sail down

To start working on the application and writing code using VS Code enter this command.

code .

Once the VS code opens, it will remind you to install the WSL extension. Install the extension and close VS code. Restart using the above mention command and start coding.

And that’s how you run a Laravel application using Docker in windows.


Tips you need to know while working with laravel and docker

Know where to locate and store your files.

Let us see where the files are when you create a new laravel project. Open your file explorer and type this— \\wsl$ and press enter.

NOTE:
For \\wsl$ command to work, make sure that your docker desktop is running.
access linus file by entering \\wsl$ in file explorer

And this will open the directory containing Linux files. To find your Linux project follow this path:

Linux distribution/ Home folder/ user directory/ laravel app name

kali-Linux > Home > weed > example-app

linux file location in windows

Using alias for long commands

Writing “./vendor/bin/sail” every time you want to run any sail command is inefficient. That’s where alias comes in. We use an alias in place of that long code. Let us see how it’s done.

alias command for sail

This is one way to do it, but it also has its drawbacks. If you close the terminal and restart it, you will have to create the alias for sail again. So every time you start the command prompt, you will have to create the alias command.

So let’s try a different approach and make the alias permanent.

Step1 – edit the bashrc file using this command – nano ~/.bashrc

open bash to edit bashrc

Now, edit the bashrc file. Go to the bottom of the file and add the command – alias sail=’bash vendor/bin/sail’ and save the file.

adding alias for sail in bash file

apply the changes

save and apply the bash changes

And You are done. You have successfully added the alias command into the bashrc file.

If you read it until last, you now know how to run a laravel application using Docker in windows.

Thank You for reading till the end. If you find this article useful, don’t forget to save it and share it with your friends. If you find any mistake or something didn’t work out quite right, do let me know. Thank You for reading!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.