How to setup Laravel (PHP 8, Postgres, Nginx, Redis and etc) with Docker & WSL2
By: Nikko Remulla
Tags: Postgres, PHP, Docker, Wsl2, Laravel

In this article I will teach you how to setup Fresh Laravel development environment in Docker & WSL 2

Stacks/Services

Postgresql

PHP 8

Laravel

Mailhog

Redis

Composer

Artisan

Installation

1. Enable your VIRTUALIZATION in BIOS

Reboot your computer and press DELETE or F2 to enter BIOS .

or to enter . If you are using I ntel CPU — Go to Advanced -> CPU Configuration -> Intel Virtualization Technology -> Enable

— Go to -> -> -> If you are using AMD CPU — Go to Advanced -> CPU Configuration -> SVM Mode -> Enable

— Go to -> -> -> Once you’ve enabled virtualization you must Save & Exit BIOS

BIOS Press Windows key ⊞ and search for “ Turn Windows features on or off” by typing windows feature.

2. Turn on WSL in Windows Features

Enable/check Virtual Machine Platform and Windows Subsystem for Linux .

After enabling both features you will need to reboot your windows to finish the install.

To install WSL2 completely, download and install the latest package at WSL2 Linux kernel update package for x64 machines.

3. Install a Linux Distribution

Go to Microsoft Store and search for Linux, you may use any Linux distro you want but in this case I will be installing Ubuntu.

After installing Ubuntu we need to set our Linux distro and version, to do that we must run PowerShell terminal as administrator.

To double check which Linux distro has been installed on your system

wsl -l

Now lets set the installed Linux distro to use wsl2, on our PowerShell terminal.

wsl --set-version *distributionName* 2

So, in my case:

wsl --set-version Ubuntu-20.04 2

To check if you have successfully set your linux distro to wsl version 2

wsl -l -v

4. Install and Configure Docker Desktop WSL2

Go to https://www.docker.com/products/docker-desktop and download docker for windows.

Once installed, we can now setup our docker to use WSL2. — Launch Docker Desktop and select the gear/settings icon.

Go to General and enable “Use the WSL 2 based engine”.

Go to Resources -> WSL Integration and enable your preferred / installed Linux distro, so in my case Ubuntu-20.04.

5. Install and Setup fresh Laravel project with Docker and WSL2

Lets open our WSL2 Linux distro terminal by typing wsl on our PowerShell or press Windows key ⊞ and search for Ubuntu

Lets update our Linux distro by running this command

sudo apt-get update

If you are experiencing temporary failure / internet connection on your wsl2 then run this

sudo rm /etc/resolv.conf && sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf' && sudo chattr +i /etc/resolv.conf

Lets install composer and PHP to create our Laravel projects

sudo apt install php-cli php openssl php-common php-curl php-json php-mbstring php-mysql php-xml php-zip

Additional extensions required for our PHP to check current installed php version run

php -v

As of today i am using php 7.4 on my wsl2 system, please note that this doesnt affect our docker php 8 service

sudo apt install php7.4-common php7.4-bcmath openssl php7.4-json php7.4-mbstring

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

To verify if composer is installed successfully run

composer -v

You may create your own docker development environment but in this article I will be using my own ready-for-development docker environment.

Lets make a new directory for our fresh laravel app using ubuntu / wsl2 terminal.

mkdir /projects/example-app

cd /projects/example-app -

By default my wsl2 Linux distro has git installed so I'm gonna proceed directly to cloning

git clone https://github.com/ndarproj/docker-laravel-php-postgres .

After successful clone, lets now create/install a fresh Laravel app inside example-app/src folder

cd /src

composer create-project laravel/laravel .

Lets download and install VSCODE for windows to access our files from WSL2

Go back to our WSL2/Ubuntu Terminal and make sure we’re still inside /projects/example-app/src and run

code .

A VSCODE window will appear and lets install “Remote — WSL” extension

After installing the required extension we need to reopen our folder in WSL. To do that click the WSL Icon on the bottom left corner then choose “Remote-WSL: Reopen Folder in WSL”

Before we deploy our fresh Laravel app to our docker we need to update and add some fields on our Laravel .env.

If you don’t wanna use your Laravel .env variables then you may manually update the docker-compose.yml file however in this tutorial I will be using the laravel .env variables

DB_CONNECTION=pgsql

DB_DATABASE=your_desired_db_database_name

DB_USERNAME=your_desired_db_username

DB_PASSWORD=your_desired_db_password

DB_PORT=your_desired_db_port

NGINX_PORT=80

— — Please note that this will be your docker db postgre credentials.

Finally! we have everything setup and configured so lets deploy our app to docker now! — Open built-in vscode terminal (ctrl + ` ) then run:

docker-compose up -d --build site

We can see that all of our containers are created and running in our docker desktop window, which means we have successfully deployed our fresh laravel app to docker.

Now on our final step, lets check whether our Laravel app is up and running by going to localhost:NGINX_PORT, so in my case it would be localhost:80

If you see this welcome window then that means your laravel-docker app is up and running

If you have any suggestion or issues you may contact me at ndarproj@gmail.com or submit an issue on this repository https://github.com/ndarproj/docker-laravel-php-postgres/issues