0
0
Laravelframework~5 mins

Docker deployment in Laravel

Choose your learning style9 modes available
Introduction

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.

You want to share your Laravel app with teammates who use different computers.
You need to run your Laravel app on a server without installing PHP and other tools manually.
You want to test your Laravel app in the same environment as production.
You want to avoid "it works on my machine" problems.
You want to package your app with all its dependencies for easy deployment.
Syntax
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.

Examples
Basic Dockerfile to run Laravel with PHP 8.2 and required extensions.
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"]
docker-compose.yml to run Laravel app and MySQL database together.
Laravel
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"
Sample Program

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.

Laravel
# 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"
OutputSuccess
Important Notes

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'.

Summary

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.