Setting Up Laravel on Docker MacOS Ventura: Step-by-Step

laravel,php

If you’re a Laravel developer using macOS Ventura, setting up Docker can make your workflow smoother, more portable, and consistent. Docker provides a containerized environment that eliminates “it works on my machine” issues and makes sharing your development setup simple.

This guide walks you through setting up Laravel on Docker, with clear instructions and compatible formatting for WordPress.


Step 1: Install Homebrew, PHP, and Composer

Before you start, install the following tools:

Install Homebrew

Run the following command in your Terminal to install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installation, verify that Homebrew was installed successfully:

brew --version

If it’s installed correctly, you’ll see a version number like:

Homebrew 4.x.x

Install PHP

Next, use Homebrew to install PHP:

brew install php

To confirm PHP is installed, run:

php --version

You should see something similar to:

PHP 8.x.x (cli) (built: ...)

Install Composer

Download Composer by running:

curl -sS https://getcomposer.org/installer | php

Move the composer.phar file to a global directory for system-wide usage:

sudo mv composer.phar /usr/local/bin/composer

Finally, verify the Composer installation:

composer --version

The output should look like:

Composer version 2.x.x

Step 2: Create a New Laravel Project

Now that the required tools are installed, you can create a new Laravel project.

Navigate to the directory where you want to create the project:

cd ~/Projects

Create a Laravel application using Composer:

composer create-project --prefer-dist laravel/laravel my-laravel-app

Replace my-laravel-app with your preferred project name.

See also  Deploy a Laravel Application on AWS EKS: A Step-By-Step Guide

Navigate to the newly created project directory:

cd my-laravel-app

Step 3: Set Up Docker

Docker requires two files for configuration: Dockerfile and docker-compose.yml.

Create the Dockerfile

The Dockerfile defines the environment for your Laravel application.

Create the file in the project root:

touch Dockerfile

Add the following content to the file:

# Use the official PHP image with Apache
FROM php:8.1-apache

# Install necessary extensions
RUN apt-get update && apt-get install -y \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    git \
    && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Enable Apache mod_rewrite
RUN a2enmod rewrite

# Set the working directory
WORKDIR /var/www/html

# Copy application files into the container
COPY . /var/www/html

# Install Composer
COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer

# Set permissions for Laravel
RUN chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html/storage

Create docker-compose.yml

The docker-compose.yml file manages Laravel services like PHP and MySQL.

Create the file in the project root:

touch docker-compose.yml

Add the following content to the file:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
    environment:
      - APP_ENV=local
      - APP_DEBUG=true
      - APP_KEY=

  db:
    image: mysql:8.0
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret

Step 4: Build and Run Docker Containers

Once the configuration files are ready, you can build and run your Docker containers.

Build the containers:

docker-compose build

Start the containers:

docker-compose up -d

Check if the containers are running:

docker ps

Access your Laravel application in the browser at:

http://localhost:8000

You should see the Laravel welcome page.


Step 5: Managing Laravel Inside Docker

You can run Artisan and Composer commands directly inside the Docker container.

Run Artisan Commands

Open a shell session in the app container:

docker exec -it <container_name> bash

Replace <container_name> with the name of your container (find it with docker ps).

See also  Laravel Forge: A Comprehensive Guide

Run Artisan commands like these:

php artisan migrate
php artisan key:generate

Run Composer Commands

You can also run Composer commands inside the container:

docker exec -it <container_name> composer install

Step 6: Configure the Database

Laravel’s .env file must match the database configuration in docker-compose.yml.

Open the .env file in your project and update the database configuration:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret

Run Laravel migrations to set up the database schema:

docker exec -it <container_name> php artisan migrate

Conclusion

There you have it! You’ve successfully set up a Laravel application on Docker using macOS Ventura. Docker offers a powerful way to create a consistent development environment that eliminates many common configuration headaches.

Photo of author
As Editor in Chief of HeatWare.net, Sood draws on over 20 years in Software Engineering to offer helpful tutorials and tips for MySQL, PostgreSQL, PHP, and everyday OS issues. Backed by hands-on work and real code examples, Sood breaks down Windows, macOS, and Linux so both beginners and power-users can learn valuable insights.

Leave a Comment