Docker deployment helps you run your Laravel app in a small, consistent container. This makes it easy to share and run your app anywhere without setup problems.
Docker deployment in Laravel
FROM php:8.2-fpm WORKDIR /var/www/html COPY . . RUN apt-get update && apt-get install -y libzip-dev zip unzip && docker-php-ext-install zip pdo pdo_mysql RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer install EXPOSE 9000 CMD ["php-fpm"]
This is a simple Dockerfile for Laravel using PHP 8.2 FPM.
It installs PHP extensions and Composer, then runs your app.
FROM php:8.2-fpm WORKDIR /var/www/html COPY . . RUN apt-get update && apt-get install -y libzip-dev zip unzip && docker-php-ext-install zip pdo pdo_mysql RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer install EXPOSE 9000 CMD ["php-fpm"]
version: '3.8' services: app: build: . ports: - "8000:9000" volumes: - .:/var/www/html depends_on: - db db: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: secret MYSQL_DATABASE: laravel MYSQL_USER: user MYSQL_PASSWORD: secret ports: - "3306:3306"
This example shows a Dockerfile to set up PHP and Laravel, and a docker-compose file to run the app with a MySQL database. You can run docker-compose up to start your Laravel app on port 8000.
# Dockerfile FROM php:8.2-fpm WORKDIR /var/www/html COPY . . RUN apt-get update && apt-get install -y libzip-dev zip unzip && docker-php-ext-install zip pdo pdo_mysql RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer install EXPOSE 9000 CMD ["php-fpm"] # docker-compose.yml version: '3.8' services: app: build: . ports: - "8000:9000" volumes: - .:/var/www/html depends_on: - db db: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: secret MYSQL_DATABASE: laravel MYSQL_USER: user MYSQL_PASSWORD: secret ports: - "3306:3306"
Remember to set correct permissions on storage and bootstrap/cache folders for Laravel.
Use volumes in docker-compose to sync your code changes without rebuilding the image.
Make sure your .env file is configured to connect to the MySQL service using the service name 'db'.
Docker deployment packages your Laravel app with PHP and dependencies in a container.
Use a Dockerfile to define the app environment and docker-compose to run app and database together.
This makes your app easy to run anywhere with one command.