Dive into the world of containerization with this comprehensive, beginner-friendly guide to setting up Docker for your Laravel projects. Whether you're a seasoned developer or just starting out, this step-by-step tutorial will walk you through creating a robust, scalable, and portable Laravel development environment using Docker. With a practical example, you'll have your Laravel app running in no time, complete with PHP, Nginx, MySQL, and Composer, all orchestrated seamlessly with Docker Compose. Let’s get started and supercharge your Laravel workflow! 🎉
Why Use Docker for Laravel? 🤔
Docker simplifies the setup of consistent development environments, eliminates "works on my machine" issues, and makes scaling a breeze. By containerizing your Laravel app, you ensure portability across development, testing, and production environments. This guide assumes you have basic knowledge of Laravel and Docker, but don’t worry—we’ll keep it clear and engaging!
Prerequisites 📋
Before we dive in, make sure you have:
- Docker and Docker Compose installed on your machine (Install Docker).
- A basic understanding of Laravel and PHP.
- A code editor (e.g., VS Code).
- A terminal (or command prompt).
Step-by-Step Guide to Setting Up Docker for Laravel 🛠️
Step 1: Create a New Laravel Project
Step 2: Set Up Docker Files
We’ll create a Docker environment with:
- PHP: To run Laravel.
- Nginx: As the web server.
- MySQL: For the database.
- Composer: To manage PHP dependencies.
1. Create a Dockerfile
In the root of your Laravel project, create a file named Dockerfile
with the following content:
FROM php:8.2-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www
# Copy existing application directory contents
COPY . /var/www
# Install Laravel dependencies
RUN composer install
# Set permissions
RUN chown -R www-data:www-data /var/www
RUN chmod -R 755 /var/www/storage
EXPOSE 9000
CMD ["php-fpm"]
This Dockerfile
sets up a PHP 8.2 environment, installs necessary PHP extensions, and includes Composer for dependency management.
2. Create a docker-compose.yml
In the project root, create a docker-compose.yml
file to orchestrate the services:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: laravel-app
container_name: laravel_app
working_dir: /var/www
volumes:
- .:/var/www
networks:
- laravel-network
webserver:
image: nginx:latest
container_name: laravel_webserver
ports:
- "8080:80"
volumes:
- .:/var/www
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
networks:
- laravel-network
db:
image: mysql:8.0
container_name: laravel_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
volumes:
- dbdata:/var/lib/mysql
networks:
- laravel-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: laravel_phpmyadmin
environment:
PMA_HOST: db
PMA_USER: laravel
PMA_PASSWORD: secret
ports:
- "8081:80"
depends_on:
- db
networks:
- laravel-network
volumes:
dbdata:
networks:
laravel-network:
driver: bridge
This file defines four services: the Laravel app, Nginx web server, MySQL database, and phpMyAdmin for database management.
3. Create an Nginx Configuration
Create a file named nginx.conf
in the project root:
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/public;
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 / {
try_files $uri $uri/ /index.php?$query_string;
}
}
This configures Nginx to serve the Laravel app and route PHP requests to the PHP-FPM service.
Step 3: Configure Laravel’s Environment
Update the .env
file in your Laravel project to connect to the MySQL service:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
Step 4: Build and Run the Docker Containers
Step 5: Access Your Laravel App
- Open your browser and navigate to
http://localhost:8080
. You should see the Laravel welcome page! 🎉 - Access phpMyAdmin at
http://localhost:8081
to manage your database (login with usernamelaravel
and passwordsecret
).
Tips for Success 🌟
- Stop Containers: To stop the containers, run:
docker-compose down
- View Logs: Check container logs for debugging:
docker-compose logs
- Clear Cache: If you encounter issues, clear Laravel’s cache:
docker-compose exec app php artisan config:cache docker-compose exec app php artisan cache:clear
- Scaling: Add more services (e.g., Redis) to the
docker-compose.yml
as needed.
What’s Next? 🚀
You now have a fully functional Laravel environment running in Docker!
Experiment by:
- Adding more routes and controllers.
- Integrating Redis or other services.
- Deploying to a production server using Docker.
Happy coding, and enjoy your containerized Laravel journey! 😎
Social Plugin