diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d9c74dc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +# Dockerfile +# 基礎 PHP + Node + Composer +FROM php:8.3-fpm + + +# 安裝系統套件與 PHP 擴充 +RUN apt-get update && apt-get install -y \ + git unzip curl libzip-dev zip libpng-dev libonig-dev cron supervisor \ + && docker-php-ext-install pdo pdo_mysql zip + +# 安裝 Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# 安裝 Node.js 與 npm +RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \ + && apt-get install -y nodejs + +# 建立 Laravel 專案目錄 +WORKDIR /var/www/html + +# 複製 supervisord 設定與啟動腳本 +COPY ./docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf +COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh + +# 啟動腳本 +CMD ["/usr/local/bin/entrypoint.sh"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 78bd93d..bb46ae2 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,9 +1,10 @@ services: app: - image: php:8.3-fpm + build: + context: . + dockerfile: Dockerfile container_name: KTVCentral - working_dir: /var/www/html volumes: - ./:/var/www/html depends_on: @@ -15,7 +16,6 @@ services: DB_DATABASE: Karaoke-Kingpin DB_USERNAME: Karaoke-Kingpin DB_PASSWORD: ESM7yTPMnavFmbBH - command: php-fpm mariadb: image: mariadb:10.6 diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..8898429 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# 啟動前置作業 +cd /var/www/html + +composer install +cp .env.example .env +php artisan key:generate +npm install && npm run build +php artisan migrate --force + +# 加入 Laravel 排程至 cron +echo "* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1" | crontab - + +# 啟動 Supervisor +exec supervisord -n \ No newline at end of file diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf index f313856..af0a2ce 100644 --- a/docker/nginx/default.conf +++ b/docker/nginx/default.conf @@ -1,10 +1,10 @@ server { listen 80; - index index.php index.html; - server_name localhost; - + server_name ktvcentral.localhost; root /var/www/html/public; + index index.php index.html; + location / { try_files $uri $uri/ /index.php?$query_string; } @@ -13,7 +13,8 @@ server { include fastcgi_params; fastcgi_pass app:9000; fastcgi_index index.php; - fastcgi_param SCRIPT_FILENAME /var/www/html/public$fastcgi_script_name; + fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; } location ~ /\.ht { diff --git a/docker/supervisord.conf b/docker/supervisord.conf new file mode 100644 index 0000000..d28b86b --- /dev/null +++ b/docker/supervisord.conf @@ -0,0 +1,28 @@ +[supervisord] +nodaemon=true + +[program:php-fpm] +command=docker-php-entrypoint php-fpm +autostart=true +autorestart=true +priority=1 + +[program:queue-worker] +command=php /var/www/html/artisan queue:work --daemon --timeout=3600 --tries=1 --queue=default +autostart=true +autorestart=true +priority=2 +stdout_logfile=/var/www/html/storage/logs/queue.log +stderr_logfile=/var/www/html/storage/logs/queue_error.log + +[program:schedule-run] +command=cron -f +autostart=true +autorestart=true +priority=3 + +[program:sqlite-sync] +command=php /var/www/html/artisan transfer:sqlite sqlite/tempUser.sqlite --sync +autostart=true +autorestart=false +priority=4 \ No newline at end of file diff --git a/開發手冊.ini b/開發手冊.ini index bf40894..8fa77bf 100644 --- a/開發手冊.ini +++ b/開發手冊.ini @@ -88,3 +88,5 @@ php artisan make:job TransferSqliteTableJob php artisan migrate:rollback php artisan migrate php artisan transfer:sqlite sqlite/tempUser.sqlite --sync + +php artisan queue:work --daemon --timeout=3600 --tries=1 --queue=default \ No newline at end of file