Create a new directory for your Laravel project:
mkdir my-laravel-projectcd my-laravel-project
Inside your project directory, create a docker-compose.yml
file with the following content:
Dockerfile
for LaravelCreate a Dockerfile
in the same directory with the following content:
FROM php:8.1-fpm# Set working directoryWORKDIR /var/www/html# Install dependenciesRUN apt-get update && apt-get install -y \build-essential \libpng-dev \libjpeg62-turbo-dev \libfreetype6-dev \libonig-dev \libxml2-dev \zip \curl \unzip \git \&& docker-php-ext-configure gd \--with-freetype \--with-jpeg \&& docker-php-ext-install -j$(nproc) gd \&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath opcache \&& pecl install xdebug \&& docker-php-ext-enable xdebug# Install ComposerCOPY --from=composer:latest /usr/bin/composer /usr/bin/composer# Copy existing application directory contentsCOPY . /var/www/html# Copy existing application directory permissionsCOPY --chown=www-data:www-data . /var/www/html# Expose port 8000 and start the serverEXPOSE 8000CMD php artisan serve --host=0.0.0.0 --port=8000
Now, you can build and run the Docker containers:
docker-compose up -d --build
Enter the app container:
docker exec -it laravel_app bash
Once inside the container, install Laravel via Composer:
composer create-project --prefer-dist laravel/laravel .
After Laravel is installed, update the .env
file in the root of your project to match the database configuration:
DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laravel DB_PASSWORD=laravel
Run the Laravel migrations to set up the database:
php artisan migrate
http://localhost:8000
.
Share This News