Docker: Dockerize Laravel Application

Laravel Project Root
    |
    |-docker
    |   |
    |   |-nginx
    |   |   |
    |   |   |-nginx.conf
    |   |
    |   |-node
    |   |   |
    |   |   |-node.Dockerfile
    |   |   
    |   |-php
    |   |   |
    |   |   |-php.Dockerfile
    |
    |-docker-compose.yml
# docker/php/php.Dockerfile

# Pull the composer image
FROM composer:latest as composer

# Pull the PHP fpm image
FROM php:8.3-fpm

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    curl \
    unzip \
    git \
    libzip-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libpq-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pgsql pdo_pgsql pdo_mysql mbstring exif pcntl bcmath gd zip intl opcache

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

# Enable the following 2 commands if imagick extension is required
# RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*

# RUN mkdir -p /usr/src/php/ext/imagick; \
#     curl -fsSL https://github.com/Imagick/imagick/archive/06116aa24b76edaf6b1693198f79e6c295eda8a9.tar.gz | tar xvz -C "/usr/src/php/ext/imagick" --strip 1; \
#     docker-php-ext-install imagick;


# Remove default server definition
RUN rm -rf /var/www/html

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

# Copy existing application directory contents
COPY . .

# Install application dependencies
RUN composer install

# Copy existing application directory contents
RUN chown -R 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"]
# docker/nginx/nginx.conf

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/public;

    send_timeout 300s;
    proxy_read_timeout 300s;
    fastcgi_read_timeout 300s;

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

    location ~ \.php$ {
        fastcgi_pass php:9000; #name of the sevice that runs the laravel app in docker-compose.yml file
        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;
    }
}
# docker-compose.yml

services:
    php:
        build:
            context: .
            dockerfile: ./docker/php/php.Dockerfile
        container_name: bigboxcode-php
        restart: unless-stopped
        working_dir: /var/www
        volumes:
            - ./:/var/www
            - ./.env:/var/www/.env
            - /var/www/vendor
        environment:
            - APP_ENV=development
        networks:
            - bigboxcode-network
        depends_on:
            - mysql

    nginx:
        image: nginx:alpine
        container_name: bigboxcode-nginx
        ports:
            - 8000:80
        volumes:
            - ./:/var/www
            - ./docker/nginx/log:/var/log/nginx
            - ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
        depends_on:
            - php
            - mysql
        networks:
            - bigboxcode-network

    mysql:
        image: mysql:8.0
        container_name: bigboxcode-mysql
        ports:
            - 3306:3306
        volumes:
            - mysql_data:/var/lib/mysql
        environment:
            MYSQL_DATABASE: bigboxcode
            MYSQL_ROOT_PASSWORD: root
            MYSQL_PASSWORD: bigboxpass
            MYSQL_USER: bigboxuser
        networks:
            - bigboxcode-network

    node:
        # platform: linux/arm64/v8 #this line is optional if you are using Mac Silicon chip (M1/M2/M3)
        build:
            context: .
            dockerfile: ./docker/node/node.Dockerfile
        container_name: bigboxcode-node
        ports:
            # - 3000:3000
            - 5173:5173
        restart: unless-stopped
        working_dir: /var/www
        volumes:
            - ./:/var/www
            - /var/www/node_modules
        environment:
            - NODE_ENV=development
            # - CHOKIDAR_USEPOLLING=true # required for file change reflection
            # - CHOKIDAR_INTERVAL=3000
        networks:
            - bigboxcode-network
        depends_on:
            - php

volumes:
    "mysql_data":


networks:
    "bigboxcode-network":
        driver: bridge
Dockerize Laravel Application
Dockerize Laravel Application
# docker-compose.yml

services:
    
    # ---- Other services(PHP, Nginx, MySQL) here

    node:
        # platform: linux/arm64/v8 #this line is optional if you are using Mac Silicon chip (M1/M2/M3)
        build:
            context: .
            dockerfile: ./docker/node/node.Dockerfile
        container_name: bigboxcode-node
        ports:
            # - 3000:3000
            - 5173:5173
        restart: unless-stopped
        working_dir: /var/www
        volumes:
            - ./:/var/www
            - /var/www/node_modules
        environment:
            - NODE_ENV=development
            # - CHOKIDAR_USEPOLLING=true # required for file change reflection
            # - CHOKIDAR_INTERVAL=3000
        networks:
            - bigboxcode-network
        depends_on:
            - php

# docker/node/node.Dockerfile

# Set the base image
FROM node:latest

# Set working directory
WORKDIR /var/www

# Copy `package.json` and `package-lock.json`
COPY package*.json ./

# Install project dependencies
RUN npm install

# Copy project files into the docker image
COPY . .

# Expose the port Vite runs on
EXPOSE 3000

# Start the Vite server
CMD ["npm", "run", "dev"]
# Makefile

PHP_CONTAINER = bigboxcode-php
NODE_CONTAINER = bigboxcode-node

# Run the application
default:
	docker compose up

# Build docker containers and prepare the environment
build:
	docker compose build --no-cache --progress=plain

# Access PHP container
access-php:
	docker exec -it $(PHP_CONTAINER) sh

# Run migration in the PHP container
migrate:
	docker exec -it $(PHP_CONTAINER) php artisan migrate

# Sync vendor directory from PHP container to the host
vendor-sync:
	docker cp $(PHP_CONTAINER):/var/www/vendor ./
	
# Sync node_modules directory from node container to the host
node-modules-sync:
	docker cp $(NODE_CONTAINER):/var/www/node_modules ./

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.