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.
Table of Contents
Step 1: Install Homebrew, PHP, and Composer
Alright, let’s kick things off by getting a few tools installed on your Mac. First up is Homebrew, which is basically a package manager for macOS. It makes installing and managing software a breeze. Next, we’ll need PHP because Laravel runs on it, and we’ll also grab Composer, a tool that handles PHP dependencies. This step will set you up with everything you need to get started with Docker and Laravel, so let’s jump in!
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 we’ve got everything set up, it’s time to create a brand new Laravel project. Don’t worry, it’s pretty simple. We’ll use Composer to quickly get a fresh Laravel app up and running. Once that’s done, we’ll have a clean slate to work with, and you’ll be ready to dive into customizing your app however you like.
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.
Navigate to the newly created project directory:
cd my-laravel-app
Step 3: Set Up Docker
Now that your Laravel project is all set up, let’s get Docker running. Docker will help us create a containerized environment for our app, making sure everything runs smoothly no matter where you’re working. Setting it up might seem like a lot, but we’ll take it step by step to make sure everything is configured correctly. Let’s get Docker up and running so we can move forward with the fun part!
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
Now that Docker is set up, it’s time to actually build and run our containers. This is where the magic happens! We’ll use Docker to package everything—your Laravel app, PHP, and all the dependencies—into a container, making it easy to run anywhere. Don’t worry if it sounds complicated; we’ll walk through each step so you can get everything up and running without a hitch. Let’s build and start those 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
Now that your containers are up and running, it’s time to manage your Laravel app inside Docker. This step is all about making sure you can interact with your app, run commands, and keep everything running smoothly in the containerized environment. We’ll cover how to access your app and manage its various components without leaving the Docker container. Let’s dive in and get you comfortable with managing Laravel inside Docker!
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
).
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
Next up, we need to set up the database for your Laravel app inside Docker. Don’t worry, we’ll guide you through configuring everything so your app can talk to the database without any issues. We’ll set up the connection and make sure everything is running smoothly, so your app can store and retrieve data without a hitch. Let’s get that database sorted!
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
Final Thoughts
And that’s it! You’ve successfully set up Laravel in Docker on macOS Ventura. Now you have a smooth, isolated environment to build and run your Laravel projects with ease. With Docker handling the heavy lifting, you can focus more on developing your app without worrying about setup or configuration issues. I hope this guide helped you get everything up and running—happy coding!
Bonus – Learn how to deploy your Laravel application to AWS EKS with this comprehensive guide.
FAQs
Why should I use Docker for setting up Laravel on macOS Ventura?
Docker ensures your Laravel app runs consistently across different environments. It eliminates configuration issues and provides a streamlined process by isolating dependencies, making it easier to manage your development setup.
Do I need to install PHP and Composer separately when using Docker?
No. Docker containers for Laravel include PHP and Composer, so you don’t need to install them separately. This makes setup simpler and ensures compatibility with all necessary dependencies.
How do I access my Laravel application running in Docker?
Once your containers are running, you can access your Laravel app by visiting http://localhost
in your browser. Just ensure Docker’s ports are properly configured to allow this connection.
How do I configure the database in Docker for Laravel?
You can easily configure a database by setting up the connection in your .env
file. Docker allows you to use any database, like MySQL or PostgreSQL, by adjusting the container settings accordingly.
Can I use Docker for Laravel development without installing Homebrew or Composer?
Yes! Docker encapsulates all dependencies, including PHP and Composer, within the container, meaning you don’t need to install them directly on your Mac. This keeps your system clean and avoids potential conflicts.
What should I do if I encounter permission errors during setup?
Permission errors may occur due to file ownership differences. Ensure the correct file permissions and ownership are set for the files in your Laravel project directory, matching the Docker container’s user.