Setup laravel project with docker

Created at 15-Mar-2024 , By samar

Here we will learn about how to create laravel project using docker on window system. You have to first install the docker desktop application and install the wsl (window subsystem linux) on your window machine.

Install wsl if you are using windows operating system

https://learn.microsoft.com/en-us/windows/wsl/install

To install wsl on windows system open terminal and run below command

wsl --install

Run wsl --list --online command to get the list of all available distro.

Now run wsl --install -d Ubuntu-22.04 to install the ubuntu-22.04 version.

Now set the version using command wsl --set-version Ubuntu-22.04 2.

To enable wsl on system you have to click on the Start menu and type "Windows features" into the search bar and click on the option "Turn Windows Features On or Off". Now Tick the "Windows Subsystem for Linux" checkbox and press the OK button.

Now you have to restart the system.

Download docker installer

https://www.docker.com/products/docker-desktop/

Run docker desktop installer.exe to install the docker desktop app on your system (after installation you have to create account and login to docker to run docker desktop application)

Checked Checkbox Use the WSL 2 based engine avaible in General section under settings option in docker desktop application.

In docker desktop you have to reach the setting > Resources > wsl integration and make changes as per below

Click on checkbox Enable integration with my default WSL distro and switch on the option ubuntu available in list.

Restart the docker

Start a new terminal session of ubuntu terminal

Now run below command to create the project

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

Move to project directory cd example-app command and run below command

./vendor/bin/sail up

Run database migration using below command

./vendor/bin/sail artisan migrate

Create a file named Dockerfile and paste below code in it and save the file.

FROM php:7.4-apache

# Install dependencies
RUN apt-get update && apt-get install -y \
    git \
    unzip \
    libzip-dev \
    && docker-php-ext-install zip pdo pdo_mysql \
    && a2enmod rewrite

# Copy application files
COPY . /var/www/html

# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

# Expose port
EXPOSE 80

Change ports under laravel.test: to 8000:80 in docker-compose.yml file

services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.3
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.3/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - "8000:80"

Start the container using below command from project root directory

docker-compose up -d

To stop the docker you can use below command

docker-compose down

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.