How to Set Up Laravel 10 with Docker and Apache using Docker Compose?

24-Jan-2024

.

Admin

How to Set Up Laravel 10 with Docker and Apache using Docker Compose?

Hello Dev,

Now, let's see a tutorial on how to set up Laravel 10 with docker and Apache using docker-compose. this example will help you how to set up Laravel in Docker. if you want to see an example of Laravel with php-apache + mysql in a docker-compose.yml then you are in the right place. this example will help you how to install and set up Laravel with docker-compose.

In this tutorial, we'll guide you through the straightforward process of setting up Docker Compose for your Laravel application. We'll create a default.conf configuration file for Apache, ensuring proper path configuration and permissions. Follow these simple steps to streamline the setup.

Embark on a step-by-step journey to set up a Laravel 10 project using Docker Compose. Follow these sequential steps for a smooth and efficient setup process.

Step 1: Install Laravel 10


While this step is optional, if you haven't created the Laravel app yet, feel free to proceed by executing the following command.

composer create-project laravel/laravel example-app

Step 2: Create a Dockerfile

Create a file named Dockerfile at the root of your Laravel project and add the following content.

Dockerfile

FROM php:8.2-apache

WORKDIR /var/www/html

RUN apt-get update && apt-get install -y \

libzip-dev \

unzip \

&& docker-php-ext-install zip pdo_mysql

COPY . /var/www/html/

RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

RUN chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

EXPOSE 80

Step 3: Create a VirtualHost Configuration

Generate a file named default.conf in the .docker/apache directory (create the directory if necessary) and include the following content.

.docker/apache/default.conf

<VirtualHost *:80>

DocumentRoot /var/www/html/public

<Directory /var/www/html>

AllowOverride All

Require all granted

</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Step 4: Create a docker-compose.yml File

Generate a docker-compose.yml file at the root of your Laravel project and include the following content.

docker-compose.yml

version: '3'

services:

web:

build:

context: .

dockerfile: Dockerfile

image: my-laravel-app

ports:

- "8080:80"

volumes:

- .:/var/www/html

- ./.docker/apache/default.conf:/etc/apache2/sites-enabled/000-default.conf

Step 5: Build and Run Docker Containers

Execute the following commands to build and run your Docker containers.

docker-compose up -d --build

To verify if port '8080' is operational, use the following command.

docker-compose ps

Afterwards, open your web browser and navigate to the following URL to confirm its functionality.

http://localhost:8080

I hope it can help you...

#Laravel 10