Create Laravel Project Through Docker — No Need to Install PHP, MySQL, or Apache on Your Local Machine
In this tutorial, I’ll show you how to create and run a full Laravel project using Docker containers.
That means you won’t have to install PHP, MySQL, or Apache locally on your computer.
By the end of this guide, you’ll have a fully functional Laravel development environment running inside Docker —
isolated, portable, and ready for professional development.
We’ll also integrate Mailpit to test outgoing emails without sending them for real.
Step 1: Install Docker and Composer Image
First, ensure you have Docker installed on your system. Docker is the foundation of this setup — it allows you to run
multiple lightweight containers instead of installing software directly on your computer.
You’ll also need the official Composer Docker image to install Laravel without having to install Composer locally.
Step 2: Create a Project Folder
Create a new directory for your Laravel project and navigate into it using your terminal:
mkdir laravel-docker && cd laravel-docker
Step 3: Create Laravel Project Using Docker Composer
Run the following Docker command to create a fresh Laravel project in your current directory:
docker run --rm -v $(pwd):/app composer create-project --prefer-dist laravel/laravel .
This command launches a temporary Composer container that downloads and installs Laravel inside your folder.
After completion, you’ll have a ready-to-use Laravel project structure.
Step 4: Create the Docker Compose File
Next, we’ll define the containers for PHP, Nginx, MySQL, and Composer.
Create a file named docker-compose.yml and paste the following configuration:
version: '3.9'
services:
app:
image: php:8.4-fpm
container_name: laravel_app
working_dir: /var/www/html
volumes:
- ./:/var/www/html
depends_on:
- db
networks:
- laravel
build:
context: .
dockerfile: Dockerfile
ports:
- "2222:22"
command: bash -c "php-fpm"
composer:
image: composer:latest
container_name: laravel_composer
working_dir: /var/www/html
volumes:
- ./:/var/www/html
networks:
- laravel
web:
image: nginx:stable-alpine3.21-perl
container_name: laravel_nginx
ports:
- "80:80"
volumes:
- ./:/var/www/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
networks:
- laravel
db:
image: mysql:8.4
container_name: laravel_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: root
ports:
- "3306:3306"
volumes:
- dbdata:/var/lib/mysql
networks:
- laravel
mailpit:
image: axllent/mailpit
container_name: mailpit
ports:
- "8025:8025"
- "1025:1025"
networks:
- laravel
networks:
laravel:
volumes:
dbdata:
This configuration defines five services — app for PHP, web for Nginx, db for MySQL,
composer for dependency management, and mailpit for capturing emails.
Step 5: Create Nginx Configuration File
Create a file named nginx.conf and add this configuration:
server {
listen 80;
server_name myapp.local;
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
Step 6: Create the Dockerfile
Create a Dockerfile to define how your PHP container will be built and configured:
FROM php:8.4-fpm
RUN apt-get update && apt-get install -y \
openssh-server \
sudo \
&& docker-php-ext-install pdo pdo_mysql \
&& mkdir -p /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
Step 7: Run the Docker Environment
Finally, build and start all containers by running:
docker compose up -d
Docker will automatically build all services. Once complete, visit
http://localhost
to see your Laravel welcome page.
Step 8: Set Up Mailpit for Email Testing
Mailpit is a powerful tool to catch all outgoing emails from your Laravel application.
It’s perfect for testing registration, password resets, and notifications without sending real emails.
In your .env file, update the mail configuration as follows:
MAIL_MAILER=smtp MAIL_HOST=mailpit MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null MAIL_FROM_ADDRESS="[email protected]" MAIL_FROM_NAME="${APP_NAME}"
After saving, any email sent via Laravel’s Mail::send() will appear inside Mailpit’s web interface.
Open your browser and go to
http://localhost:8025
to view all test emails.
🎉 Final Result
Congratulations! You now have a complete Laravel development setup with PHP, MySQL, Nginx, Composer, and Mailpit —
all running inside Docker. You don’t need to install or configure any of these tools on your local machine.
This setup ensures a clean, consistent, and portable environment for all developers.
With just a few commands, you can spin up or shut down your environment,
making Laravel development simpler and faster than ever.
0 Comments