How to Install Laravel with Docker
Before we dive into the installation steps, make sure you have the necessary system requirements and Windows edition to run Docker Desktop. Here’s a simplified guide to get Laravel up and running with Docker.
System Requirements
- Windows 11 (64-bit): Home or Pro version 21H2 or higher, or Enterprise or Education version 21H2 or higher.
- Windows 10 (64-bit): Home or Pro 21H2 (build 19045) or higher, or Enterprise or Education 21H2 (build 19045) or higher.
- Ensure WSL 2 (Windows Subsystem for Linux 2) is enabled on Windows. Refer to Microsoft’s documentation for instructions.
Hardware Prerequisites
For Windows 10 or 11, you’ll need:
- 64-bit processor with Second Level Address Translation (SLAT)
- 4GB system RAM
- BIOS-level hardware virtualization support enabled
Important Note
Running Windows containers requires Windows 10 or 11 Professional or Enterprise edition. Windows Home or Education editions only support Linux containers.
Installation Steps
- Download Docker Desktop for Windows from their official website.
- Run the installer by double-clicking on “Docker Desktop Installer.exe.”
- During installation, select the “Use WSL 2 instead of Hyper-V” option if available.
- Note: If your system supports only one option, you won’t have a choice in the backend.
- Follow the installation wizard’s instructions to authorize the installer and complete the process.
- After successful installation, select “Close.” Docker will ask you to log out and back into your Windows session or restart your machine to finish the installation.
Starting Docker Desktop
After the restart:
- Start Docker Desktop by searching for it in the start menu.
- The first time you run Docker, it may take a few minutes to configure itself.
- Open a command prompt or PowerShell window.
- Run the command
docker --version
to ensure Docker is installed and running.
Setting Up Laravel with Docker
Dockerfile
Create a Dockerfile in your Laravel project’s root directory with the following content:
FROM php:8.1.0-apache
WORKDIR /var/www/html
# Install required dependencies
RUN a2enmod rewrite
RUN apt-get update && apt-get -y install gcc mono-mcs && rm -rf /var/lib/apt/lists/*
RUN apt-get update -y && apt-get install -y libicu-dev libmariadb-dev unzip zip zlib1g-dev libpng-dev libjpeg-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev default-libmysqlclient-dev gcc
RUN apt-get -y update; apt-get -y install curl
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Install PHP extensions
RUN docker-php-ext-install gettext intl pdo_mysql gd
RUN docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg && docker-php-ext-install -j$(nproc) gd
docker-compose.yml
Create a docker-compose.yml file in your Laravel project’s root directory:
services:
laravel-docker:
container_name: laravel-docker
build: .
volumes:
- ./laravel-app:/var/www/html
ports:
- 9000:80
extra_hosts:
- "192.168.0.188:9000"
mysql_db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel_docker
ports:
- 3306:3306
phpmyadmin:
image: phpmyadmin:latest
ports:
- 9001:80
environment:
- PMA_ARBITRARY=1
Running Docker Containers
- Navigate to your Laravel project’s base directory in the command prompt.
- Execute
docker-compose build
to build the Docker containers. - Launch the application with
docker-compose up
. - Access the Docker container with
docker exec -it laravel-docker bash
. Replace “laravel-docker” with your container’s name if different.
Install Laravel and Configure Database
- Install Laravel using the command:
composer create-project laravel/laravel .
- Once Laravel is installed, execute the
exit
command.
Modify the .env File
Update the database connection details in the .env file with these settings:
DB_CONNECTION=mysql
DB_HOST=mysql_db
DB_PORT=3306
DB_DATABASE=laravel_docker
DB_USERNAME=root
DB_PASSWORD=root
Execute Migrations
Run the migrations using the following command:
docker exec laravel-docker bash -c "php artisan migrate"
Now, your Laravel project should be accessible at http://localhost:9000/public/, and you can manage your database with PHPMyAdmin at http://localhost:9001/. Enjoy developing with Laravel and Docker!