How to Install and Setup Laravel with Docker Compose?

By Hardik Savani May 1, 2024 Category : Laravel

Hello Guys,

In this example, you will learn laravel docker setup. you'll learn run laravel with docker. This example will help you how to install and setup laravel with docker compose. let’s discuss about how to install and set up laravel with docker compose on ubuntu 22.04. Follow the below tutorial step of install laravel with docker compose.

Here, i will give you simple step by step example of how to install and set up laravel with docker compose on ubuntu 22.04 with apache. we will understand all the points in briefly.

What is Docker?

Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Containers are a form of lightweight, stand-alone, and executable software packages that include everything needed to run a piece of software, including the code, runtime, libraries, and system tools. Docker provides a consistent and reproducible environment for applications, making it easier to develop, deploy, and scale applications across different environments.

Key components of Docker include:

1. Docker Engine: This is the core component of Docker that manages containers. It consists of a server that runs in the background (daemon) and a command-line interface (CLI) that allows users to interact with the Docker daemon.

2. Docker Image: An image is a lightweight, standalone, and executable package that includes the application and its dependencies. Images can be used to create containers. Docker images are stored in repositories and can be versioned for easy sharing and distribution.

3. Container: A container is a running instance of a Docker image. Containers provide a consistent environment for applications, isolating them from the underlying system and ensuring that they run consistently across different environments.

4. Dockerfile: A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, adds application code and dependencies, and configures the environment within the container.

5. Docker Compose: Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the services, networks, and volumes in a docker-compose.yml file, making it easy to spin up complex applications with multiple interconnected containers.

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to use a YAML file to configure your application's services, networks, and volumes, and then use a single command to create and start all the services defined in that configuration. Docker Compose simplifies the process of orchestrating multiple Docker containers, making it easier to manage complex applications.

Now, we will start step by step setup laravel 10 project with docker compose.

Step 1: Install Laravel 10

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create a Dockerfile

Create a file named "Dockerfile" in the root of your Laravel project with 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" in the ".docker/apache" directory (you may need to create the .docker/apache directory) 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

Create a file named "docker-compose.yml" in the root of your Laravel project with 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

Run the following commands to build and run your Docker containers:

docker-compose up -d --build

You can check your port "8080" is running or not by using follow command:

docker-compose ps

Then you can check following url on browser and check it will works:

http://localhost:8080

Output:

I hope it can help you...

Tags :
Shares