How to Deploy Laravel Using Docker: Step-by-Step Guide
To deploy Laravel using
Docker, create a Dockerfile to define the PHP environment and a docker-compose.yml to manage services like PHP, MySQL, and Nginx. Build and run containers with docker-compose up -d to serve your Laravel app in isolated, reproducible environments.Syntax
The deployment setup typically involves two main files:
- Dockerfile: Defines the PHP environment, installs dependencies, and sets up the Laravel app.
- docker-compose.yml: Defines services like PHP, database, and web server, and how they connect.
Commands used:
docker build: Builds the Docker image from the Dockerfile.docker-compose up -d: Starts containers in detached mode.
Dockerfile
FROM php:8.2-fpm # Install system dependencies RUN apt-get update && apt-get install -y \ libpng-dev \ libonig-dev \ libxml2-dev \ zip \ unzip \ git \ curl # Install PHP extensions RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd # Install Composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer # Set working directory WORKDIR /var/www/html # Copy existing application directory contents COPY . /var/www/html # Install Laravel dependencies RUN composer install --no-dev --optimize-autoloader # Set permissions RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache EXPOSE 9000 CMD ["php-fpm"]
Example
This example shows a basic docker-compose.yml file to deploy Laravel with PHP-FPM, MySQL, and Nginx. It demonstrates how to link services and mount your app code.
yaml
version: '3.8' services: app: build: context: . dockerfile: Dockerfile image: laravel-app container_name: laravel-app restart: unless-stopped working_dir: /var/www/html volumes: - ./:/var/www/html networks: - laravel mysql: image: mysql:8.0 container_name: laravel-mysql restart: unless-stopped environment: MYSQL_ROOT_PASSWORD: rootpassword MYSQL_DATABASE: laravel MYSQL_USER: laraveluser MYSQL_PASSWORD: laravelpass volumes: - mysql-data:/var/lib/mysql networks: - laravel nginx: image: nginx:latest container_name: laravel-nginx restart: unless-stopped ports: - "8080:80" volumes: - ./:/var/www/html - ./nginx.conf:/etc/nginx/conf.d/default.conf depends_on: - app networks: - laravel volumes: mysql-data: networks: laravel:
Output
Containers laravel-app, laravel-mysql, and laravel-nginx start running; Laravel app is accessible at http://localhost:8080
Common Pitfalls
Common mistakes when deploying Laravel with Docker include:
- Not setting correct file permissions for
storageandbootstrap/cache, causing runtime errors. - Forgetting to link the database service or misconfiguring environment variables, leading to connection failures.
- Not exposing or mapping ports properly, so the app is unreachable from the host.
- Running
composer installinside the container without caching dependencies, slowing builds.
Dockerfile
Wrong Dockerfile snippet: RUN composer install Right Dockerfile snippet: RUN composer install --no-dev --optimize-autoloader
Quick Reference
Tips for smooth Laravel Docker deployment:
- Use
php:8.2-fpmas base image for PHP 8.2 support. - Mount your app code as a volume for live development.
- Use
docker-composeto manage multi-container apps easily. - Configure Nginx with a proper server block to serve Laravel's
publicdirectory. - Set environment variables in
.envand pass them to containers securely.
Key Takeaways
Create a Dockerfile to define PHP and Laravel environment with necessary extensions.
Use docker-compose.yml to orchestrate PHP, MySQL, and Nginx services together.
Set correct permissions on Laravel storage and cache folders to avoid errors.
Map ports and volumes properly to access the app and enable live code updates.
Run composer install with optimization flags inside the Docker build for faster startup.