Docker has revolutionized the way developers set up and manage their local development environments, especially for Laravel applications. This guide will walk you through the process of creating a streamlined Docker setup for your Laravel projects, offering flexibility and ease of use.
Why Docker?
Docker allows you to package your application and its dependencies into containers, ensuring consistency across different environments. For Laravel developers, this means:
Easy management of multiple projects with different PHP versions
Isolation of environments to prevent conflicts
Quick setup and teardown of development environments
Replication of production environments locally
Setting Up Your Docker Environment
To get started, you'll need to install Docker on your machine. Once installed, create a project structure like this:
my-project.com/
├── nginx/
│ └── default.conf
├── src/
│ └── (Laravel app files)
├── docker-compose.yml
└── Dockerfile
Creating the Docker Compose File
The docker-compose.yml
file is the heart of your Docker setup. It defines the services that make up your application stack. Here's a basic structure:
version: '3'
networks:
laravel:
services:
# Services will be defined here
Adding Nginx
Add the following Nginx service to your docker-compose.yml:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
This sets up an Nginx container using the Alpine Linux image, maps ports, and links volumes.
Adding MySQL
Next, add the MySQL service:
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
This sets up a MySQL 5.7 container with default Laravel credentials.
Adding PHP
For PHP, we'll create a custom image. Add this to your docker-compose.yml
:
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www
ports:
- "9000:9000"
networks:
- laravel
Then, create a Dockerfile
in your project root:
FROM php:7.2-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
This sets up a PHP-FPM container with necessary extensions for Laravel.
Configuring Nginx
In your nginx/default.conf
file, add a basic Nginx configuration for Laravel:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/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;
}
}
Starting Your Docker Environment
To start your Docker environment, run:
docker-compose build
docker-compose up -d
This builds the custom PHP image and starts all containers in detached mode.
Configuring Laravel
Update your Laravel .env
file:
DB_HOST=mysql
APP_URL=http://localhost:8080
Accessing Your Application
Your Laravel application should now be accessible at http://localhost:8080
.
Running Laravel Commands
To run Laravel commands, use:
docker-compose exec php php /var/www/artisan migrate
This executes the command within the PHP container.
Conclusion
Docker provides a powerful, flexible environment for Laravel development. While it may have a slight performance trade-off compared to bare-metal setups, the benefits of consistency, isolation, and ease of use make it an excellent choice for many developers.