Developing Laravel applications requires a stable and consistent development environment. Docker, a popular containerization platform, offers an efficient way to create such an environment. In this comprehensive tutorial, we’ll explore how to establish a robust Laravel development environment using Docker—a leading containerization technology. The guide includes steps for integrating MySQL and configuring Nginx, ensuring a seamless development process.
Prerequisites
- Basic knowledge of Laravel, Docker, and MySQL.
- Docker and Docker Compose installed on your machine.
Step 1: Setting Up the Laravel Project
First, create a new Laravel project or navigate to your existing project directory. If you’re creating a new project, use Composer:
composer create-project --prefer-dist laravel/laravel my-laravel-app
cd my-laravel-app
Step 2: Creating the Dockerfile
Develop a Dockerfile at the root of your Laravel project. This file employs PHP 8.2 image and installs necessary PHP extensions along with Composer. It outlines your Laravel application’s environment.
FROM php:8.2-fpm
# Install dependencies
RUN apt-get update && apt-get install -y \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Step 3: Setting Up Docker Compose
Create a ‘docker-compose.yml’ file in your project’s root. This file orchestrates your Docker containers, defining services for Laravel (app), MySQL (db), and Nginx (web). It establishes a network for these services and a volume for MySQL.
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: my-laravel-app
container_name: my-laravel-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: my-laravel-app
SERVICE_TAGS: dev
working_dir: /var/www/html
volumes:
- ./:/var/www/html
networks:
- app-network
db:
image: mysql:8
container_name: my-laravel-mysql
restart: unless-stopped
tty: true
ports:
- "13306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
volumes:
- dbdata:/var/lib/mysql
networks:
- app-network
# Nginx Service
web:
image: 'nginx:alpine'
ports:
- "8000:80"
volumes:
- ./:/var/www/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
dbdata:
driver: local
This file defines two services: app for your Laravel application and db for MySQL. It also sets up a network and a volume for MySQL data persistence.
Step 4: Implementing Nginx Configuration
Place an nginx.conf file in your project’s root to configure the Nginx server, essential for serving your application.
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
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 app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
Step 5: Running Your Containers
With your Dockerfile and docker-compose.yml set up, you can start your containers.
docker-compose up -d
Step 5: Configuring Laravel Database
Modify the .env
file in your Laravel project to use the MySQL service:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=user
DB_PASSWORD=password
Step 6: Migrating Databases
Run Laravel migrations to set up your database:
docker-compose exec app php artisan migrate
Step 6: Access Application
Once the containers are up and running. You can access Laravel application on http://localhost:8000.
Conclusion
You’ve successfully established a Docker-based Laravel development environment with MySQL integration and Nginx configuration. This setup enhances consistency across development scenarios and facilitates team collaboration. To further refine your environment, delve into additional Docker features and Laravel’s advanced capabilities.