Setting up a Laravel project using Docker has become an increasingly popular approach to streamline development processes and ensure consistent environment setups. In this guide, we'll walk you through the steps to set up a Laravel project with Docker in 2025, ensuring an efficient and smooth development experience.
Why Use Docker for Laravel?
Docker provides a standardized environment for your Laravel applications, eliminating the "it works on my machine" problem. By using Docker, you can define your development and production environments with consistent configurations, making deployments more reliable and repeatable. Let's dive into the setup process!
Step 1: Prerequisites
Before creating a Dockerized Laravel environment, ensure you have the following installed on your development machine:
- Docker: Download and install Docker Desktop
- Docker Compose: Included with Docker Desktop
- Laravel Installer or Composer: To create a new Laravel project
Step 2: Create a Laravel Project
If you haven't created a Laravel project yet, create one using Composer:
composer create-project --prefer-dist laravel/laravel my-laravel-app
Navigate into your project directory:
cd my-laravel-app
Step 3: Create a Dockerfile
In your project root, create a file named Dockerfile
and add the following content:
FROM php:8.1-fpm
WORKDIR /var/www
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
zip \
unzip
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd \
&& docker-php-ext-install pdo pdo_mysql
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . /var/www
RUN composer install
RUN chown -R www-data:www-data /var/www \
&& chmod -R 755 /var/www
EXPOSE 9000
CMD ["php-fpm"]
Step 4: Configure Docker Compose
Create a docker-compose.yml
file in the root of your project:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: laravel_app
ports:
- "9000:9000"
volumes:
- .:/var/www
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
mysql:
image: mysql:8.0
container_name: laravel_mysql
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: laravel
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
nginx:
image: nginx:alpine
container_name: laravel_nginx
ports:
- "80:80"
volumes:
- .:/var/www
- ./docker/nginx/conf.d:/etc/nginx/conf.d
depends_on:
- app
Step 5: Configure Nginx
Create a directory named docker/nginx/conf.d
and within it, create a file named default.conf
:
server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Step 6: Spin Up Your Docker Containers
With everything configured, it's time to build and run your Docker containers:
docker-compose up -d
Your Laravel application should now be running in a Dockerized environment, accessible at http://localhost.
Additional Resources
- Learn more about Laravel's powerful features from this Laravel tutorial.
- Want to explore database interactions? Check out this guide on Laravel database fields.
- If you're dealing with Excel files, here's how to validate multiple sheets in Laravel Excel.
- If you are still learning PHP & Laravel: 8 Best Laravel Books.
Setting up a Laravel project with Docker can significantly optimize your development workflow. With this setup, you can focus more on development rather than environment configuration, paving the way for efficient, productive Laravel application development.
Top comments (0)