Creating a Laravel 10 Development Environment Using Docker

22-Nov-2023

.

Admin

Creating a Laravel 10 Development Environment Using Docker

Hello Dev,

"Containerization is rapidly shaping web development, and Docker stands out as an essential tool for developers. Its ability to streamline dependency and environment management empowers developers to concentrate on their code. This guide is dedicated to walking you through the process of establishing a Laravel 10 development environment using Docker."

"Before you begin, ensure your system meets these prerequisites:

1. Docker: Head to Docker’s official website to download and install Docker Desktop.

2. Docker Compose: While typically included with Docker Desktop, install it separately if not available.

3. Composer: This PHP dependency manager is essential for initiating a new Laravel project."

Step 1: Setting Up the PHP Docker Environment


1. Create a file named Dockerfile in your project's root directory.

2. Input the following content into the Dockerfile:

Dockerfile

FROM php:8.0-fpm-alpine

ADD ./php/www.conf /usr/local/etc/php-fpm.d/www.conf

RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel

RUN mkdir -p /var/www/html

ADD ./src/ /var/www/html

RUN docker-php-ext-install pdo pdo_mysql

RUN chown -R laravel:laravel /var/www/html

This Dockerfile configures a container with PHP 8.2 and necessary extensions for a Laravel project.

Step 2: Creating the Docker Compose File

1. Create a file named docker-compose.yml in your Laravel project's root directory.

2. Insert the following content into the docker-compose.yml file:

YAML

version: "3.9"

networks:

laravel:

name: laravel

services:

nginx:

[nginx configuration]

php:

[PHP configuration]

mysql:

[MySQL configuration]

composer:

[Composer configuration]

artisan:

[Artisan configuration]

npm:

[NPM configuration]

This file defines services for Nginx, PHP, MySQL, Composer, Artisan, and NPM, allowing multi-container Docker applications to work together within a defined network.

Step 3: Configuring Nginx

Create an nginx directory at your Laravel project's root.

Inside nginx, create a conf.d directory.

Within conf.d, create an app.conf file.

Insert the following Nginx server configuration into app.conf:

[nginx server configuration]

This configuration sets up Nginx to serve your Laravel application.

Step 4: Building and Running Docker Containers

Execute the following command from your Laravel project's root directory:

docker-compose up -d --build

This command builds and starts the defined Docker containers.

Step 5: Creating a New Laravel 10 Project

1. Navigate to your preferred directory for the new project.

Run this command:

docker-compose run --rm composer create-project laravel/laravel.

This command creates a new Laravel 10 project within the designated directory.

Congratulations! Your Laravel 10 development environment, powered by Docker and PHP 8.2

Now accessible at http://localhost:8000.

#Laravel 10