为 Laravel 提供生产模式下的容器化环境:打造现代开发环境的终极指南

为 Laravel 提供生产模式下的容器化环境:打造现代开发环境的终极指南

在现代开发中,容器化已经成为一种趋势。使用 Docker 可以让我们轻松地管理和部署应用程序。本文将带你一步步构建一个高效的 Laravel 容器化环境,确保你的应用程序在开发和生产环境中都能顺畅运行。

功能:

  1. 拆分队列运行容器和web服务容器
  2. 开发环境和生产环境分离
  3. 日志监控
  4. 方便升级
  5. 可推送K8s环境,实现弹性伸缩.

最终文件结构,laravel 源代码在 src中.

一、Docker Compose 文件配置

首先,我们需要创建一个 docker-compose.yml 文件,这个文件定义了我们的服务及其配置。

yaml 复制代码
version: '3'
services:      
  app:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - COMPOSER_PROCESS_TIMEOUT=6000      
    ports:
      - 8290:80
    volumes:
      - /etc/hosts:/etc/hosts      
      - ../src:/var/www/html
    logging: # 可以使用loki将log采集,并在grafana中设置监控和告警 
      driver: loki
      options:
        loki-url: "http://localhost:3100/loki/api/v1/push"
        loki-retries: "5"
        loki-batch-size: "100"
        no-file: "true"
    command: bash -c "chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache && service nginx start && php-fpm"

  worker:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - COMPOSER_PROCESS_TIMEOUT=6000      
    command: php artisan horizon
    volumes:
      - /etc/hosts:/etc/hosts    
      - ../src:/var/www/html
    # laravel 的源代码我们可以放在上层文件夹中
    logging:
      driver: loki
      options:
        loki-url: "http://localhost:3100/loki/api/v1/push"
        loki-retries: "5"
        loki-batch-size: "100"
        no-file: "true"      
    links:
      - app
二、Dockerfile 配置

接下来,我们需要编写 Dockerfile 文件,定义应用程序的运行环境。这样可以很容易的拆分开开发和生产环境

Dockerfile 复制代码
# Use the base PHP image with the specified version
FROM php:8.2.14-fpm

# Set the working directory inside the container
WORKDIR /var/www/html

# Update package lists and install necessary dependencies
RUN apt-get update && apt-get install -y \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    libzip-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libssl-dev \
    libcurl4-openssl-dev 

# Install Nginx
RUN apt-get install -y nginx

# Install PHP extensions required by your application
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip 
RUN docker-php-ext-install soap
RUN docker-php-ext-install ctype 
RUN docker-php-ext-install curl
RUN docker-php-ext-install dom
RUN docker-php-ext-install fileinfo 
RUN docker-php-ext-install filter
RUN docker-php-ext-install session
RUN docker-php-ext-install xml
RUN docker-php-ext-install ftp

# Install and enable the Redis extension
RUN pecl install redis && docker-php-ext-enable redis

# Install Xdebug extension
# RUN pecl install xdebug && docker-php-ext-enable xdebug

# Copy the application files into the container
# COPY . .

# Change ownership of directories used by the application
# RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# RUN composer install --optimize-autoloader --no-dev

COPY nginx.conf /etc/nginx/sites-available/default

RUN rm /var/www/html/index.nginx-debian.html

# Expose ports 80 and 443 to the outside world
EXPOSE 80
EXPOSE 443

# Set the default command to run when the container starts
CMD service nginx start && php-fpm
三、Nginx 配置

创建一个 nginx.conf 文件,配置 Nginx 以服务我们的 Laravel 应用程序。

nginx 复制代码
server {
    listen 80;
    server_name localhost;
    root /var/www/html/public;

    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 3000;
        fastcgi_connect_timeout 3000;
        fastcgi_send_timeout 3000;
    }

    location ~ /\.ht {
        deny all;
    }
}
四、Makefile 配置

为了更方便地管理 Docker 容器,我们可以使用 Makefile 定义常用的命令。

Makefile 复制代码
install: build 
		docker compose run --rm app composer install 
		docker compose run --rm app cp -f .env.prod .env

update:
		docker compose run --rm app composer update

cache:
		docker compose run --rm app php artisan route:cache
		docker compose run --rm app php artisan config:cache
		docker compose run --rm app composer dump-autoload  -o

build: build-app build-work 

build-app:
		docker compose -f docker-compose.yml build app

build-work:
		docker compose -f docker-compose.yml build worker

start:
		docker compose -f docker-compose.yml up -d

stop:
		docker compose -f docker-compose.yml down

restart: 
		docker compose -f docker-compose.yml restart

exec:
		docker compose exec app /bin/bash

info:
		docker compose run --rm app php -i

通过以上配置,我们可以轻松地为 Laravel 应用提供一个容器化环境。这不仅提高了开发效率,还确保了生产环境的稳定性。赶快试试吧!喜欢这篇文章的话,不要忘了点赞、收藏和分享哦!


希望这篇文章对你有帮助!如果有任何疑问或建议,欢迎在评论区留言。你的支持是我继续创作的动力!

相关推荐
凯子坚持 c14 小时前
Docker存储卷深度解析:机制、管理与数据持久化实战
运维·docker·容器
蟑螂恶霸14 小时前
使用docker安装windows 11
运维·docker·容器
MicoZone14 小时前
docker
docker
JaguarJack15 小时前
PHP 8.6 新增 clamp() 函数
后端·php
BingoGo15 小时前
PHP 8.6 新增 clamp() 函数
后端·php
真正的醒悟15 小时前
图解网络39
网络·智能路由器·php
hhhjjjj1 天前
docker安装postgreSQL
docker·postgresql·容器
阿里巴巴P8资深技术专家1 天前
docker容器启动报错
运维·docker·容器
无名修道院1 天前
DVWA 靶场搭建:Windows11(phpstudy 搭建)(步骤 + 截图 + 常见问题)
数据库·网络安全·渗透测试·靶场·php·dvwa·phpstudy
元气满满-樱1 天前
docker网络模式详解
网络·docker·容器