docker.recipes

Laravel + MySQL

intermediate

PHP Laravel framework with MySQL and Redis.

Overview

Laravel is an elegant PHP web framework that follows the Model-View-Controller (MVC) architectural pattern, created by Taylor Otwell in 2011. Built on the Symfony framework components, Laravel emphasizes developer productivity through expressive syntax, built-in features like Eloquent ORM, Blade templating engine, and Artisan command-line tool. It has become one of the most popular PHP frameworks due to its clean architecture, comprehensive documentation, and vibrant ecosystem of packages through Composer and Laravel-specific tools like Nova and Forge. This stack combines Laravel's rapid development capabilities with MySQL's proven relational database performance and Redis's lightning-fast caching and session storage. NGINX serves as the high-performance web server handling static assets and reverse proxying to PHP-FPM, while MySQL manages persistent data with ACID compliance and complex relationships that Laravel's Eloquent ORM excels at handling. Redis accelerates the application by caching database queries, storing user sessions, and managing Laravel's queue system for background job processing. This configuration targets web development teams building content management systems, e-commerce platforms, SaaS applications, and API backends that require robust data relationships, user authentication, and high-performance caching. Laravel developers working on applications with complex business logic, multi-user systems, or projects requiring rapid prototyping will benefit from this stack's balance of development speed and production reliability.

Key Features

  • Laravel Eloquent ORM with MySQL for complex database relationships and migrations
  • Redis-powered session storage and cache driver for sub-millisecond response times
  • NGINX FastCGI integration with PHP-FPM for optimal Laravel application performance
  • Laravel Queue system backed by Redis for asynchronous job processing
  • MySQL InnoDB storage engine with full ACID compliance for data integrity
  • Laravel Artisan command-line tool integration for migrations and maintenance
  • Redis Pub/Sub messaging for real-time Laravel broadcasting and WebSocket support
  • NGINX static asset serving with Laravel Mix optimization support

Common Use Cases

  • 1E-commerce platforms requiring complex product catalogs, inventory management, and order processing
  • 2Content management systems with hierarchical data structures and media libraries
  • 3SaaS applications needing multi-tenant architecture with user authentication and authorization
  • 4RESTful API backends for mobile applications with token-based authentication
  • 5Educational platforms with course management, user progress tracking, and payment integration
  • 6Real-time applications using Laravel Echo with Redis for chat systems or live updates
  • 7Enterprise web applications requiring role-based permissions and audit logging

Prerequisites

  • Minimum 2GB RAM (1GB for MySQL, 512MB for Redis, 512MB for Laravel/NGINX)
  • PHP and Laravel framework knowledge including Eloquent ORM and Blade templating
  • MySQL database design experience with understanding of indexes and relationships
  • Port 80 available for web traffic and familiarity with NGINX configuration
  • Composer dependency manager knowledge for Laravel package management
  • Basic understanding of Redis data structures for caching and session implementation

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 app:
3 build: .
4 container_name: laravel
5 volumes:
6 - .:/var/www/html
7 networks:
8 - laravel
9
10 nginx:
11 image: nginx:alpine
12 container_name: nginx
13 volumes:
14 - .:/var/www/html:ro
15 - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
16 ports:
17 - "80:80"
18 depends_on:
19 - app
20 networks:
21 - laravel
22
23 mysql:
24 image: mysql:8.0
25 container_name: mysql
26 environment:
27 MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
28 MYSQL_DATABASE: ${MYSQL_DATABASE}
29 MYSQL_USER: ${MYSQL_USER}
30 MYSQL_PASSWORD: ${MYSQL_PASSWORD}
31 volumes:
32 - mysql_data:/var/lib/mysql
33 networks:
34 - laravel
35
36 redis:
37 image: redis:alpine
38 container_name: redis
39 networks:
40 - laravel
41
42volumes:
43 mysql_data:
44
45networks:
46 laravel:
47 driver: bridge

.env Template

.env
1MYSQL_ROOT_PASSWORD=rootpassword
2MYSQL_DATABASE=laravel
3MYSQL_USER=laravel
4MYSQL_PASSWORD=changeme

Usage Notes

  1. 1Docs: https://laravel.com/docs
  2. 2Create Dockerfile with PHP-FPM + composer
  3. 3Run migrations: docker compose exec app php artisan migrate
  4. 4Access at http://localhost
  5. 5Generate app key: docker compose exec app php artisan key:generate
  6. 6Queue worker: docker compose exec app php artisan queue:work

Individual Services(4 services)

Copy individual services to mix and match with your existing compose files.

app
app:
  build: .
  container_name: laravel
  volumes:
    - .:/var/www/html
  networks:
    - laravel
nginx
nginx:
  image: nginx:alpine
  container_name: nginx
  volumes:
    - .:/var/www/html:ro
    - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
  ports:
    - "80:80"
  depends_on:
    - app
  networks:
    - laravel
mysql
mysql:
  image: mysql:8.0
  container_name: mysql
  environment:
    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    MYSQL_DATABASE: ${MYSQL_DATABASE}
    MYSQL_USER: ${MYSQL_USER}
    MYSQL_PASSWORD: ${MYSQL_PASSWORD}
  volumes:
    - mysql_data:/var/lib/mysql
  networks:
    - laravel
redis
redis:
  image: redis:alpine
  container_name: redis
  networks:
    - laravel

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 app:
5 build: .
6 container_name: laravel
7 volumes:
8 - .:/var/www/html
9 networks:
10 - laravel
11
12 nginx:
13 image: nginx:alpine
14 container_name: nginx
15 volumes:
16 - .:/var/www/html:ro
17 - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
18 ports:
19 - "80:80"
20 depends_on:
21 - app
22 networks:
23 - laravel
24
25 mysql:
26 image: mysql:8.0
27 container_name: mysql
28 environment:
29 MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
30 MYSQL_DATABASE: ${MYSQL_DATABASE}
31 MYSQL_USER: ${MYSQL_USER}
32 MYSQL_PASSWORD: ${MYSQL_PASSWORD}
33 volumes:
34 - mysql_data:/var/lib/mysql
35 networks:
36 - laravel
37
38 redis:
39 image: redis:alpine
40 container_name: redis
41 networks:
42 - laravel
43
44volumes:
45 mysql_data:
46
47networks:
48 laravel:
49 driver: bridge
50EOF
51
52# 2. Create the .env file
53cat > .env << 'EOF'
54MYSQL_ROOT_PASSWORD=rootpassword
55MYSQL_DATABASE=laravel
56MYSQL_USER=laravel
57MYSQL_PASSWORD=changeme
58EOF
59
60# 3. Start the services
61docker compose up -d
62
63# 4. View logs
64docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/laravel-mysql/run | bash

Troubleshooting

  • Laravel key not set error: Run 'docker compose exec app php artisan key:generate' to generate application encryption key
  • MySQL connection refused: Verify MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD environment variables match Laravel's .env configuration
  • Redis connection timeout: Ensure Redis container is running and Laravel REDIS_HOST is set to 'redis' (container name)
  • NGINX 502 Bad Gateway: Check that PHP-FPM is running in the Laravel container and NGINX upstream configuration points to correct FastCGI socket
  • Laravel storage permissions error: Run 'docker compose exec app chown -R www-data:www-data storage bootstrap/cache' to fix file permissions
  • MySQL data persistence lost: Verify mysql_data volume is properly mounted and not being recreated between container restarts

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space