How to setup Laravel with Dockerfile and Docker-Compose

1 feb 2024 3 min di lettura
How to setup Laravel with Dockerfile and Docker-Compose
Indice dei contenuti

Introduction

Laravel application development requires a stable and consistent development environment. Docker, a popular containerization platform, offers an efficient way to create such an environment. In this comprehensive tutorial, we'll explore how to create a robust Laravel development environment using Docker, a leading containerization technology. The guide includes steps for integrating MySQL and configuring Nginx, ensuring a seamless development process.

Prerequisites

  • Basic knowledge of Laravel, Docker and MySQL.
  • Docker and Docker Compose installed on your computer.

Step 1: Setting up your Laravel project

First, create a new Laravel project or go to the existing project directory. If you are creating a new project, use Composer:

composer create-project --prefer-dist laravel/laravel my-laravel-app
cd my-laravel-app

Step 2: Creating the Dockerfile

Develop a Dockerfile at the root of your Laravel project. This file uses the PHP 8.2 image and installs the necessary PHP extensions along with Composer. Outline the environment of your Laravel application.

FROM php:8.2-fpm

 # Install dependencies
 RUN apt-get update && apt-get install -y \
 libpng-dev \
 libonig-dev \
 libxml2-dev \
 zip \
 unzip

 # Clear cache
 RUN apt-get clean && rm -rf /var/lib/apt/lists/*

 # Install PHP extensions
 RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

 # Get latest Composer
 COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

 # Expose port 9000 and start php-fpm server
 EXPOSE 9000
 CMD ["php-fpm"]

Step 3: Configuring Docker Compose

Create a "docker-compose.yml" file in the root of your project. This file orchestrates your Docker containers, defining services for Laravel (app), MySQL (db), and Nginx (web). It establishes a network for these services and a volume for MySQL.

version: '3'
 services:
 app:
 build:
 context:.
 dockerfile: Dockerfile
 image: my-laravel-app
 container_name: my-laravel-app
 restart: unless-stopped
 tty: true
 environment:
 SERVICE_NAME: my-laravel-app
 SERVICE_TAGS: dev
 working_dir: /var/www/html
 volumes:
 -./:/var/www/html
 networks:
 - app-network

 db:
 image: mysql:8
 container_name: my-laravel-mysql
 restart: unless-stopped
 tty: true
 ports:
 - "13306:3306"
 environment:
 MYSQL_DATABASE: laravel
 MYSQL_USER: user
 MYSQL_PASSWORD: password
 MYSQL_ROOT_PASSWORD: password
 SERVICE_TAGS: dev
 volumes:
 - dbdata:/var/lib/mysql
 networks:
 - app-network

 # Nginx Service
 web:
 image: 'nginx:alpine'
 ports:
 - "8000:80"
 volumes:
 -./:/var/www/html
 -./nginx.conf:/etc/nginx/conf.d/default.conf
 depends_on:
 - app
 networks:
 - app-network

 networks:
 app-network:
 driver: bridge

 volumes:
 dbdata:
 driver: local

This file defines two services: app for your Laravel application and db for MySQL. It also configures a network and volume for MySQL data persistence.

Step 4: Deploy Nginx Configuration

Place an nginx.conf file in your project root to set up the Nginx server, which is essential for serving your application.

server {
 listen 80;
 index index.php index.html;
 error_log /var/log/nginx/error.log;
 access_log /var/log/nginx/access.log;
 root /var/www/html/public;

 location /{
 try_files $uri $uri//index.php?$query_string;
 }

 location ~ \.php$ {
 try_files $uri =404;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 fastcgi_pass app:9000;
 fastcgi_index index.php;
 include fastcgi_params;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param PATH_INFO $fastcgi_path_info;
 }

 location ~ /\.ht {
 deny all;
 }
 }

Step 5: Running Containers

With the Dockerfile and docker-compose.yml setup, you can launch your containers.

docker-compose up -d

Step 5: Setting up your Laravel database

Edit the .env file in your Laravel project to use the MySQL service:

DB_CONNECTION=mysql
 DB_HOST=db
 DB_PORT=3306
 DB_DATABASE=laravel
 DB_USERNAME=user
 DB_PASSWORD=password

Step 6: Migrate databases

Run Laravel migrations to setup your database:

docker-compose exec app php artisan migrate

Step 6: Log in to the application

Once the containers are up and running. You can access the Laravel application at http://localhost:8000.

Conclusion

You have successfully created a Docker-based Laravel development environment with MySQL integration and Nginx setup. This configuration improves consistency between development scenarios and facilitates team collaboration. To further refine your environment, delve into additional Docker features and advanced Laravel features.

Support us with a

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.