Laravel is a popular PHP web framework that enables developers to create web applications efficiently. Docker, on the other hand, is a platform for building, shipping, and running applications using containers. In this guide, we will go through the steps to set up Laravel on Docker on macOS Ventura.
Table of Contents
Step 1: Install Docker
Before you can start using Docker, you need to install it on your machine. You can download Docker for macOS Ventura from the official Docker website. Once the download is complete, double-click on the downloaded file and follow the installation prompts.
Step 2: Create a new Laravel project
After installing Docker, you need to create a new Laravel project. You can use the following command to create a new Laravel project:
docker run --rm -v $(pwd):/app composer create-project --prefer-dist laravel/laravel my-project
This command creates a new Laravel project called my-project
in the current directory.
Step 3: Create a Dockerfile
A Dockerfile is a text file that contains instructions for building a Docker image. In this step, you need to create a new file called Dockerfile
in the root directory of your Laravel project and add the following contents to it:
FROM php:8.0-apache
RUN apt-get update && apt-get install -y \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install gd \
&& docker-php-ext-install mbstring \
&& docker-php-ext-install xml \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
COPY . /var/www/html
RUN composer install
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
This Dockerfile installs the necessary PHP extensions, installs Composer, sets the working directory to /var/www/html
, copies the Laravel project files to the container, installs the project dependencies, and sets the appropriate permissions for the storage and bootstrap directories.
Step 4: Build the Docker image
After creating the Dockerfile, you need to build the Docker image. You can use the following command to build the Docker image:
docker build -t my-project .
This command builds a new Docker image called my-project
using the Dockerfile in the current directory.
Step 5: Run the Docker container
After building the Docker image, you need to run the Docker container. You can use the following command to run the Docker container:
docker run --rm -p 8000:80 my-project
This command runs a new Docker container using the my-project
image and maps port 8000 on your machine to port 80 in the container.
Step 6: Test the Laravel application
After running the Docker container, you can test the Laravel application by opening a web browser and navigating to http://localhost:8000
. You should see the Laravel welcome page if everything was set up correctly.
In this guide, we went through the steps to set up Laravel on Docker on macOS Ventura. By following these steps, you should now have a working Laravel application running inside a Docker container.
Further Reading
- Laravel documentation: https://laravel.com/docs/8.x/installation
- Docker for Mac documentation: https://docs.docker.com/docker-for-mac/
- Dockerize your Laravel Application: https://dev.to/erickmuranod/dockerize-your-laravel-application-3kno
- Running Laravel in Docker: https://www.cloudreach.com/en/insights/blog/running-laravel-in-docker/
- Dockerizing Laravel with Nginx MySQL and Docker Compose: https://www.twilio.com/blog/dockerizing-laravel-with-nginx-mysql-and-docker-compose