Docker with PHP & Laravel
Set up a complete Laravel development environment using Docker with PHP-FPM, Nginx, and MySQL — without installing anything locally.
Project Structure
my-laravel-app/
├── docker/nginx/default.conf
├── Dockerfile
├── docker-compose.yml
└── (Laravel project files)
Step 1: Create the Dockerfile
FROM php:8.2-fpm
RUN apt-get update && apt-get install -y git curl zip unzip libpng-dev
RUN docker-php-ext-install pdo_mysql mbstring gd
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
COPY . .
RUN composer install --no-dev --optimize-autoloader
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
EXPOSE 9000
CMD ["php-fpm"]
Step 2: Nginx Config
docker/nginx/default.conf:
server {
listen 80;
root /var/www/html/public;
index index.php;
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 $document_root$fastcgi_script_name;
}
}Step 3: docker-compose.yml
version: "3.9"
services:
app:
build: .
container_name: laravel-app
volumes:
- .:/var/www/html
networks:
- laravel-net
nginx:
image: nginx:latest
container_name: laravel-nginx
ports:
- "8080:80"
volumes:
- .:/var/www/html
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- laravel-net
mysql:
image: mysql:8.0
container_name: laravel-mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: laravel_db
MYSQL_USER: laravel
MYSQL_PASSWORD: laravel123
volumes:
- mysql-data:/var/lib/mysql
networks:
- laravel-net
networks:
laravel-net:
volumes:
mysql-data:Step 4: Laravel .env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel
DB_PASSWORD=laravel123
Step 5: Start and Run
docker compose up -d
docker compose exec app php artisan migrate
docker compose exec app php artisan key:generate
docker compose exec app php artisan config:clear
Visit http://localhost:8080 — your Laravel app is running in Docker!
Summary
- Use PHP-FPM + Nginx + MySQL containers for a full Laravel stack
- Use
docker compose exec appto run artisan commands - Set
DB_HOST=mysql(container name) in.envfor database connection - Mount your project with volumes so code changes reflect instantly
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
