Skip to content

ijklim/docker-laravel

Repository files navigation

Docker Laravel 12

A modern Docker setup for Laravel 12 with PHP 8.3, Apache, and MySQL 8.0.

Overview

This project provides a complete Docker environment for developing Laravel 12 applications with:

  • PHP 8.3 - PHP version with full support for modern PHP features
  • Laravel 12 - Latest Laravel framework with modern structure
  • Apache 2.4 - Web server with mod_rewrite enabled
  • MySQL 8.0.23 - Database server
  • Docker Compose - Container orchestration

Requirements

  • Docker Desktop or Docker Engine
  • Docker Compose
  • Composer (for local development)

Quick Start

1. Clone the Repository

# Clone the repository
git clone <repository-url>
cd docker-laravel

Note: You do NOT need to run composer install locally. The Docker container is self-sufficient and will install all PHP dependencies during the build process with PHP 8.3.

2. Environment Configuration

# Copy example environment file
cp .env.example .env

3. Start Docker Containers

Option A: Using docker-run.sh (Recommended)

The docker-run.sh script provides an interactive way to manage containers:

bash docker-run.sh

Then select:

  1. Environment: Choose between dev (development) or prod (production)
  2. Action: Choose between build, start, or shutdown

This script handles all docker compose commands with proper environment variable configuration.

Option B: Manual Docker Compose Commands

If you prefer manual commands, you need to source the .env file first:

# Source environment variables and use the compose file in docker/ directory
set -a
. .env
set +a
export ENVIRONMENT=dev  # or 'prod'

# Build images (first time, use --no-cache for clean build)
docker compose -f docker/docker-compose.override.yml build --no-cache

# Start containers
docker compose -f docker/docker-compose.override.yml up -d

# View logs
docker compose -f docker/docker-compose.override.yml logs -f

# Stop containers
docker compose -f docker/docker-compose.override.yml down

Tip: The docker-run.sh script handles all of this automatically, so Option A is recommended.

4. Access the Application

Open your browser and visit: http://localhost:8080

Laravel 12 Architecture

This project uses Laravel 12's modern structure:

├── bootstrap/
│   └── app.php              # Application bootstrapping (fluent configuration)
├── app/
│   ├── Http/
│   │   ├── Middleware/      # HTTP middleware (TrustHosts, etc.)
│   │   ├── Controllers/     # Application controllers
│   ├── Models/              # Eloquent models
│   └── Providers/           # Service providers
├── routes/
│   ├── web.php             # Web routes
│   ├── api.php             # API routes
├── resources/
│   ├── views/              # Blade templates
│   ├── css/                # CSS files
│   └── js/                 # JavaScript files
├── storage/
│   ├── app/                # Application storage
│   ├── logs/               # Application logs
│   └── framework/          # Framework cache
├── tests/                  # Test files
├── docker-compose.yml      # Container configuration
└── composer.json           # PHP dependencies

Key Changes from Laravel 8

  • No Kernel.php: Middleware configuration moved to bootstrap/app.php
  • Fluent Configuration: Application bootstrapping uses fluent API
  • Modern PHP: Requires PHP 8.1+ with support for recent language features
  • Updated Packages: All dependencies updated to Laravel 12 compatible versions

Docker Commands

Development

# Start containers in background
docker compose up -d

# View running containers
docker compose ps

# View logs
docker compose logs web     # Web container logs
docker compose logs db      # Database container logs
docker compose logs -f      # Follow logs

# Execute commands in containers
docker exec docker-laravel-web php artisan migrate
docker exec docker-laravel-web php artisan tinker

# Access container shell
docker exec -it docker-laravel-web bash
docker exec -it docker-laravel-mysql bash

Database

# Run migrations
docker exec docker-laravel-web php artisan migrate

# Seed database
docker exec docker-laravel-web php artisan db:seed

# Access MySQL console
docker exec -it docker-laravel-mysql mysql -uroot -p

Troubleshooting

# Restart containers
docker compose restart

# Remove and rebuild containers
docker compose down
docker compose up -d --build

# Clear Laravel cache
docker exec docker-laravel-web php artisan cache:clear
docker exec docker-laravel-web php artisan config:clear
docker exec docker-laravel-web php artisan view:clear

Configuration

Environment Variables (.env)

Key Docker-related variables:

Variable Default Description
DOCKER_WEB_PORT_NUMBER 8080 Port to access the website
DOCKER_WEB_CONTAINER_NAME docker-laravel-web Web container name
DOCKER_DB_CONTAINER_NAME docker-laravel-mysql Database container name
DOCKER_BUILD_OS ubuntu Operating system for web container
DOCKER_BUILD_OS_VERSION 24.04 Ubuntu version (24.04 includes PHP 8.3)
DOCKER_BUILD_MYSQL_VERSION 8.4 MySQL version

Database Connection

DB_CONNECTION=mysql
DB_HOST=db              # Service name from docker-compose.yml
DB_PORT=3306
DB_DATABASE=fff
DB_USERNAME=root
DB_PASSWORD=<your-password>

File Structure

docker/
├── apache/
│   └── apache.conf      # VirtualHost configuration
├── mysql/
│   ├── Dockerfile       # MySQL image configuration
│   ├── my.cnf          # MySQL configuration
│   └── docker-entrypoint-initdb.d/
│       └── *.sql       # SQL scripts to run on startup
├── php/
│   └── php.ini         # PHP configuration (if needed)
└── os/
    └── Dockerfile      # Web server base image

Known Ports

Service Port URL
Web (Apache) 8080 http://localhost:8080
MySQL 3306 localhost:3306

Volume Mounts

Web Container:
  - Current directory → /var/www/html
  - ./docker/apache/apache.conf → /etc/apache2/sites-available/000-default.conf
  - ./storage/logs → /var/log

Database Container:
  - ./database/mysql → /var/lib/mysql
  - ./storage/logs/mysql → /var/log/mysql

Notes

  • PHP Version: Ubuntu 24.04 includes PHP 8.3 by default, which is required for Laravel 12
  • Database Storage: MySQL data is persisted in ./database/mysql/
  • Logs: Application and server logs are available in ./storage/logs/
  • Git Ignored: Docker MySQL data is in .gitignore to avoid committing database files

Resources

License

The Laravel framework is open-sourced software licensed under the MIT license.