Deploy Laravel with Docker on Windows: From Scratch

Hi everyone! this is Esar from GeekFeed.

You may have heard people say “make sure to take a day off or go on a staycation after you install Docker on Windows”.  Well, we got the jokes, installing Docker can be stressful. Some developers even clearly mentioned that installing Docker on a Windows computer is not the best option compared to Mac or Linux computers – and we haven’t even talked about deploying Laravel yet.

Here, let me guide you through a less-stressful way of building Docker environment on your Windows computer and deploy Laravel project from scratch without digging into documentations.

 

Installing Docker on Windows

Without the help from WSL 2 Docker can’t work independently on Windows 10. So, before installing Docker  let’s install WSL 2 on Windows.

WSL 2 Installation

Step 1 – Enable the Windows Subsystem for Linux

Open PowerShell as Administrator and run:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

 

How to open PowerShell as Administrator:
Start-Process powershell.exe -Verb runas
OR
Right click on PowerShell icon and click ‘open as administrator’

 

Step 2 – Enable Virtual Machine feature

Open PowerShell as Administrator and run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Make sure to Restart  your computer after this step to complete the WSL installation and update to WSL 2.

 

Step3 – Download the linux kernel update package

You can download from this link:

WSL2 Linux kernel update package for x64 machines

 

Step 4 – Set WSL 2 as default version

Open PowerShell (not as Administrator) and run:
wsl --set-default-version 2

 

Step 5 – Install Linux distribution

There’s a range of option of Linux distributions that you can choose. For this tutorial, let’s choose Ubuntu 20.04 LTS. The following link will direct you to Microsoft Store’s Ubuntu 20.04 LTS installation page :

Ubuntu 20.04 LTS

When the installation completed, open ubuntu and set new UNIX username and password.

 

Docker Installation

Click the following link and download and install “Docker Desktop for Windows” package.
Download Docker Desktop

After installation completed, let’s check if Docker is working properly.

Open Docker Desktop application and it should appear like this:

 

Hover your mouse on docker mark and it should say ”Docker Desktop is running”.

 

Open Ubuntu-20.04 terminal and run:
Docker --version

And it should show the Docker version like this:

 

That’s it for Docker on Windows!

 

Deploy Laravel 6 with Docker

Some of you might think version 6 is outdated and why would we choose that. The reason is that Laravel 6 has Long Term Support (LTS) and version 7 or 8 doesn’t.

So, let’s deploy Laravel 6 project with Docker.

Requirements:

  1. PHP 7
  2. NGINX
  3. MySQL
  4. Redis (optional)

Create New Laravel Project

Assuming that Composer is already installed in your computer,

Open Ubuntu 20.04 terminal and create new Laravel 6 project and name the project “laravel-docker”.

composer create-project --prefer-dist laravel/laravel="6.*" laravel-docker

Quick check and go to the project directory.

Make sure Docker and docker-compose are already installed.
docker --version

docker-compose --version

 

Adding Required Files

 There are 4 files required to be added to our project folder:

  1. docker-compose.yml
  2. app.dockerfile
  3. web.dockerfile
  4. vhost.conf

 

Structure in the project folder:

Let’s create the files.

docker-compose.yml

note: we can change MySQL version to mysql:latest  if needed.

app.dockerfile

 

web.dockerfile

 

vhost.conf

 

Run docker-compose and Check the Container

Don’t forget to check if there’s typos and save. Then run on terminal:
docker-compose up -d
This command will pull all needed images and it will create a container. Images that will be created include NGINX, PHP 7, MySQL, and Redis.

Next, check images and container and make sure they are running.
Run this command:
docker ps
OR
View on Docker Desktop that we installed earlier:

 

Now, we should be able to access Laravel 6 homepage by accessing localhost:8080 on the browser.

この記事が気に入ったら
いいね ! しよう

Twitter で
The following two tabs change content below.
アバター
Esar Suwandi

【採用情報】一緒に働く仲間を募集しています

採用情報
ページトップへ