A Comprehensive Guide to Dockerizing Laravel Applications

Welcome to Dockerize It! In this comprehensive guide, we will walk you through the process of Dockerizing Laravel applications. Laravel is one of the most popular PHP frameworks, known for its elegant syntax and robust features. Combining Laravel with Docker can greatly enhance your development workflow by providing consistent environments across development and production.

Why Dockerize Your Laravel Application?

Dockerizing your Laravel application offers several benefits:

Prerequisites

Before we begin, make sure you have the following installed on your system:

Setting Up the Laravel Application

If you already have a Laravel application, you can skip this step. Otherwise, let's create a new Laravel project:

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

Creating the Dockerfile

The Dockerfile defines the environment for our Laravel application. Create a file named Dockerfile in the root of your Laravel project with the following content:

FROM php:8.0-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# 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

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

# Set working directory
WORKDIR /var/www

COPY . /var/www

RUN composer install

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www

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

Explanation

Setting Up Nginx

We'll use Nginx as our web server. Create a file named default.conf in a new directory called nginx at the root of your project:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

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

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Creating the Docker Compose File

Create a file named docker-compose.yml at the root of your project:

version: '3.8'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: laravel-app
    container_name: laravel_app
    restart: unless-stopped
    working_dir: /var/www
    volumes:
      - ./:/var/www
    networks:
      - laravel

  webserver:
    image: nginx:alpine
    container_name: laravel_webserver
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - laravel

  db:
    image: mysql:5.7
    container_name: laravel_db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_PASSWORD: secret
      MYSQL_USER: laravel
    volumes:
      - dbdata:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - laravel

networks:
  laravel:

volumes:
  dbdata:

Explanation

Configuring Laravel Environment

Update your .env file to match the database settings:

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

Building and Running the Containers

Now, you can build and run your Docker containers:

docker-compose up -d --build

This command builds the images and starts the containers in detached mode.

Accessing the Application

Open your browser and navigate to http://localhost:8000 to see your Laravel application running inside Docker containers.

Running Migrations

To run Laravel Artisan commands inside the container, use the following command:

docker-compose exec app php artisan migrate

Conclusion

Congratulations! You've successfully Dockerized your Laravel application. This setup provides a consistent environment for development and can be easily adapted for production deployments. Dockerizing your application not only streamlines development but also simplifies the deployment process, making your applications more scalable and reliable.

Further Reading