Dockerize Your Laravel Application

Share this post on:

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

  1. Download Docker Desktop for Windows from their official website.
  2. Run the installer by double-clicking on “Docker Desktop Installer.exe.”
  3. 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.
  4. Follow the installation wizard’s instructions to authorize the installer and complete the process.
  5. 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:

  1. Start Docker Desktop by searching for it in the start menu.
  2. The first time you run Docker, it may take a few minutes to configure itself.
  3. Open a command prompt or PowerShell window.
  4. 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:

docker-compose.yml

Create a docker-compose.yml file in your Laravel project’s root directory:

Running Docker Containers

  1. Navigate to your Laravel project’s base directory in the command prompt.
  2. Execute docker-compose build to build the Docker containers.
  3. Launch the application with docker-compose up.
  4. 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

  1. Install Laravel using the command: composer create-project laravel/laravel .
  2. Once Laravel is installed, execute the exit command.

Modify the .env File

Update the database connection details in the .env file with these settings:

Execute Migrations

Run the migrations using the following command:

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!

Share this post on: