Setting Up a Laravel 10 Development Environment with Docker
--
With the growing trend of containerization in web development, Docker has become an indispensable tool for developers. Docker allows for seamless management of dependencies and environments, enabling developers to focus on coding. In this article, we’ll guide you through setting up a Laravel 10 development environment using Docker.
Prerequisites
Before starting, make sure the following are installed on your system:
- Docker: Visit Docker’s official website to download and install Docker Desktop.
- Docker Compose: Usually included with Docker Desktop, install separately if it’s not.
- Composer: PHP’s dependency manager, necessary to create a new Laravel project.
Step 1: Setup PHP Docker Environment
In the root of your project, create a file named Dockerfile
. This file defines the configuration for our Docker container. Add the following content to your 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 creates a Docker container with PHP 8.2 and all the necessary extensions to run a Laravel project.
Step 2: Create a Docker Compose File
In the root directory of your Laravel project, create a docker-compose.yml
file. This file allows us to define and manage multi-container Docker applications. Insert the following content:
version: "3.9"
networks:
laravel:
name: laravel
services:
nginx:
build:
context: .
dockerfile: nginx.dockerfile
depends_on:
- php
- mysql
container_name: nginx
ports:
- 80:80
- 443:443
volumes:
- ./src:/var/www/html
networks:
- laravel
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./src:/var/www/html
networks:
- laravel
mysql:
image: mysql:8.0.27
container_name: mysql
ports:
- 3306:3306
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: laraveldb
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
networks:
- laravel
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['php', 'artisan']
networks:
- laravel
npm:
image: node:current-alpine
container_name: npm
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
This docker-compose.yml
file creates two services: one for PHP and one for Nginx. It also defines a Docker network that the two services use to communicate.
Step 3: Configure Nginx
Create an nginx
directory at the root of your Laravel project and within it, create a conf.d
directory. Inside conf.d
, create an app.conf
file and insert the following Nginx server configuration:
server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Step 4: Build and Run the Docker Containers
Now, we’re ready to build and run our Docker containers. From the root directory of your Laravel project, run the following command:
docker-compose up -d --build
Step 5: Create a New Laravel 10 Project
We need to create a new Laravel 10 project. Navigate to the directory where you wish to create your project and run the following command:
docker-compose run --rm composer create-project laravel/laravel .
Congratulations! You’ve now successfully set up a Laravel 10 development environment with Docker and PHP 8.2. Your Laravel application is now accessible at http://localhost:80
.
Access the Full Project
For the full project, feel free to visit my GitHub repository: https://github.com/vshloda/docker-laravel
Happy coding!