How to Install and Setup Laravel with Docker Compose - TechvBlogs

How to Install and Setup Laravel with Docker Compose

Discover the seamless integration of Laravel with Docker Compose. Follow our step-by-step guide for a hassle-free installation and setup. Elevate your development workflow with the power of Laravel and Docker today.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

4 months ago

TechvBlogs - Google News

In this tutorial, you'll gain insights into setting up Laravel with Docker, allowing you to seamlessly run Laravel using Docker. This example serves as a guide on installing and configuring Laravel with Docker Compose. Let's delve into the steps of installing Laravel with Docker Compose on Ubuntu 22.04. Follow the tutorial below for a step-by-step walkthrough of the installation process.

Here, I'll provide you with a straightforward, step-by-step example of installing and setting up Laravel with Docker Compose on Ubuntu 22.04, utilizing Apache. Each step will be explained in detail to ensure a clear understanding of the process.

What is Docker?

Docker serves as a revolutionary platform that automates the deployment of applications through lightweight, portable containers. These containers function as self-contained software packages, encompassing everything required to execute a piece of software—code, runtime, libraries, and system tools. Docker offers a consistent and reproducible environment for applications, streamlining the development, deployment, and scalability processes across diverse environments.

Key components of Docker include:

  1. Docker Engine: At the heart of Docker is its core component, which oversees the management of containers. This pivotal element comprises a background server, known as the daemon, and a command-line interface (CLI) that empowers users to engage with the Docker daemon.
  2. Docker Image: An image serves as a compact, self-contained, and executable package encompassing both the application and its dependencies. These images act as the foundation for creating containers. Docker stores these images in repositories, facilitating easy versioning for seamless sharing and distribution.
  3. Container: A container is a live instantiation of a Docker image. Containers offer a uniform environment for applications, isolating them from the host system to ensure consistent operation across diverse environments.
  4. Dockerfile: A Dockerfile is a text file outlining instructions for constructing a Docker image. It defines the base image, incorporates application code and dependencies, and configures the container's environment.
  5. Docker Compose: Docker Compose serves as a tool dedicated to defining and executing multi-container Docker applications. By enabling the definition of services, networks, and volumes in a docker-compose.yml file, it simplifies the process of launching intricate applications comprising multiple interconnected containers.

What is Docker Compose?

Docker Compose is a utility designed for defining and executing multi-container Docker applications. By utilizing a YAML file, you can configure your application's services, networks, and volumes. With a single command, you can create and initiate all the services outlined in the configuration. This streamlined approach simplifies the orchestration of multiple Docker containers, enhancing the management of complex applications.

Let's initiate the step-by-step setup for a Laravel 10 project using Docker Compose.

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

Generate a file named 'Dockerfile' at the root of your Laravel project and include 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

Create a file named 'default.conf' within the '.docker/apache' directory (create the directory if necessary) and populate it with the following content:

.docker/apache/default.conf

<VirtualHost *:80>
    DocumentRoot /var/www/html/public
     
    <Directory /var/www/html>
        AllowOverride All
    </Directory>
</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 running, use the following command:

docker-compose ps

Afterwards, open your web browser and navigate to the following URL to confirm whether it is functioning:

http://localhost:8080

Thank you! If you need any more assistance or have further questions, feel free to ask.

Comments (0)

Comment


Note: All Input Fields are required.