How to Set Up Laravel in Docker?

How to Set Up Laravel in Docker

Laravel is one of the most popular PHP frameworks for web development. Docker is a platform offering tools for building, distributing, and running applications inside containers across environments. It’s great for both development and deployment because it allows you to make an environment that runs equally well on any system or operating system. Thus, Laravel + Docker provides portable, consistent development environments with which you can develop better.

We are going to learn how to set up Laravel with Apache and MySQL in Docker so you can keep working efficiently while developing.

Why use Laravel with Docker?

Before we dive into the setup process, let’s quickly highlight the advantages of using Docker for Laravel development:

  • Consistency: Docker keeps a consistent development environment, removing from your life that nasty little expression “it works on my machine.”
  • Isolation: Every Docker container encapsulates dependencies and configurations, so it cannot conflict with other projects or system configurations.
  • Scalability: Docker makes it very easy to scale your Laravel application by easily replicating containers for local development, testing, or production deployment.
  • Portability: The Docker container can easily move across various platforms. Thus, it’s quite easy to shift your Laravel application from your development environment to your production environment.

Setting Up Laravel in Docker with Apache and MySQL:

Let’s dive into the step-by-step process of setting up Laravel in Docker with Apache and MySQL:

Step 1: Install Docker

Ensure Docker is installed on your system. If not, you can download and install it from the official Docker website ( https://www.docker.com/get-started ).

Step 2: Create Laravel Project

Begin by creating a new Laravel project or use an existing one. You can create a new Laravel project using Composer by running the following command:

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

Step 3: Dockerfile

Create a `Dockerfile` in the root directory of your Laravel project to define the environment for your Docker container. Here’s an example tailored for Apache and PHP:

Dockerfile

FROM php:8.1-apache // Your project sutable veriosn specify

WORKDIR /var/www/html

RUN docker-php-ext-install pdo pdo_mysql mysqli

COPY . .

EXPOSE 80

CMD ["apache2-foreground"]

Step 4: Docker Compose

Create a `docker-compose.yml` file in the same directory to define services for Apache, MySQL, and volumes for data persistence. Here’s an example:

docker-compose.yml

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html

  db:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel_user
      MYSQL_PASSWORD: secret

Step 5: Environment Configuration

Ensure your Laravel `.env` file is configured to use MySQL. Update the following fields:

dotenv

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=secret

Step 6: Build and Run Docker Containers

Navigate to your project directory in the terminal and run the following command to build and run your Docker containers:

docker-compose up --build

Step 7: Access Laravel Application

Once the Docker containers are up and running, you can access your Laravel application by visiting `http://localhost:8000` in your web browser.

Conclusion:

In short, with Docker, developing in Laravel using Apache and MySQL will have the advantage of consistency, scalability, and portability. In the end, it will only take a few steps for you to quickly set up your environment within Docker for working collaboratively and efficiently in developing. You will be able to get your Laravel projects off to a flying start and go beyond the bounds of productivity and scalability with the help of Docker.

Happy Coding!

Previous Article

How to Delete an Existing Foreign Key in Magento 2 Installer?

Next Article

What is B2B Sales Experience? Everything You Need to Know

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨