Building and Deploying a Laravel App Using Docker

This tutorial guides you through building and deploying a Laravel application using Docker. The process includes setting up Docker, creating a Dockerfile, and configuring Docker Compose.

Prerequisites

  • Docker installed on your system
  • Basic understanding of Laravel and Docker

Step 1: Set Up Laravel Project

Create a New Laravel Project:

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

Step 2: Create a Dockerfile

Create a Dockerfile in the root directory of your Laravel project:

# Use official PHP image as base
FROM php:8.0-fpm

# Set working directory
WORKDIR /var/www

# Install 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

# Copy existing application directory contents
COPY . /var/www

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

# Change current user to www
USER www-data

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

Step 3: Set Up Docker Compose

Create a docker-compose.yml file in the root directory:

version: '3.8'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: laravel-app
    container_name: laravel-app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  webserver:
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  db:
    image: mysql:5.7
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root
      MYSQL_PASSWORD: root
      MYSQL_USER: root
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  dbdata:
    driver: local

Step 4: Configure Nginx

Create a nginx/conf.d/app.conf file with the following content:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root /var/www/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_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

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

Step 5: Create PHP Configuration File

Create a php/local.ini file with the following content:

memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300

Step 6: Build and Run Containers

Run Containers:

docker-compose up -d

Build Containers:

docker-compose build

Step 7: Access the Application

Open your web browser and navigate to http://localhost:8080. You should see the Laravel welcome page.

Step 8: Manage Containers

Stop Containers:

docker-compose down

Restart Containers:

docker-compose restart

Conclusion

You have successfully built and deployed a Laravel application using Docker. This setup allows for easy scalability and consistency across different environments.

Resources:

Keywords: Laravel, Docker, Docker Compose, PHP, Nginx, MySQL, containerization, web development, deployment








Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+