feature: add docker

This commit is contained in:
antv
2026-06-30 14:03:05 +07:00
parent c5016489ac
commit 96eac88f36
19 changed files with 211 additions and 36 deletions
+17
View File
@@ -0,0 +1,17 @@
FROM php:8.3-fpm
WORKDIR /var/www/html
RUN apt-get update && apt-get install -y \
git unzip zip curl \
libpng-dev libjpeg-dev libfreetype6-dev \
libonig-dev libxml2-dev libzip-dev \
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
COPY docker/php/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm"]
+113
View File
@@ -0,0 +1,113 @@
# Laravel Docker Setup - thankcard-system
## 1. Cấu trúc thư mục
```txt
thankcard-system/
├─ .env
├─ artisan
├─ composer.json
├─ public/
├─ storage/
├─ bootstrap/
├─ doc/
└─ docker/
├─ docker-compose.yml
├─ Dockerfile
├─ README.md
├─ env/
│ └─ .env.app
├─ nginx/
│ └─ default.conf
└─ php/
└─ entrypoint.sh
```
---
## 2. Khởi chạy dự án (Docker)
Đứng tại thư mục root của dự án `thankcard-system` và chạy:
```bash
docker compose -f docker/docker-compose.yml up -d --build
```
Hoặc di chuyển vào thư mục `docker/` rồi chạy:
```bash
cd docker
docker compose up -d --build
```
Sau khi các container chạy thành công, truy cập hệ thống tại trình duyệt:
```txt
http://localhost:8888
```
---
## 3. Cơ chế cấu hình môi trường (.env)
* File `.env` chính của Laravel vẫn nằm ở thư mục root để phục vụ máy local.
* Khi chạy trong container Docker, hệ thống sẽ sử dụng file **`docker/env/.env.app`** để ghi đè các cấu hình cần thiết (ví dụ: `DB_HOST=host.docker.internal` giúp PHP container kết nối ra cơ sở dữ liệu trên máy thật).
---
## 4. Các lệnh cần chạy thủ công khi cập nhật code
### 4.1. Khi thêm/cập nhật thư viện mới (`composer.json`)
Khi cập nhật hoặc cài đặt gói package PHP mới, bạn cần chạy lệnh cài đặt **bên trong container**:
```bash
docker compose -f docker/docker-compose.yml exec app composer install
```
### 4.2. Khi cập nhật giao diện (CSS / JS / Tailwind classes mới)
Do Docker container không cài Node.js, bạn cần chạy các lệnh compile frontend trực tiếp **ở máy thật (host)**:
* **Chạy chế độ phát triển (Hot Reload):**
```bash
npm run dev
```
* **Build production tĩnh:**
```bash
npm run build
```
### 4.3. Khi cập nhật Database (Migrations)
Để cập nhật cơ sở dữ liệu khi có migrations mới:
```bash
docker compose -f docker/docker-compose.yml exec app php artisan migrate
```
### 4.4. Khi chỉnh sửa file `.env` hoặc `.env.app`
Bạn cần khởi động lại container để cập nhật biến môi trường mới và tiến hành xóa cache cấu hình:
```bash
# 1. Restart container app
docker compose -f docker/docker-compose.yml restart app
# 2. Xóa cache cấu hình trong container
docker compose -f docker/docker-compose.yml exec app php artisan config:clear
```
---
## 5. Các lệnh tiện ích khác
* **Truy cập vào Bash của container app:**
```bash
docker compose -f docker/docker-compose.yml exec app bash
```
* **Xem log Laravel thời gian thực:**
```bash
docker compose -f docker/docker-compose.yml exec app tail -f storage/logs/laravel.log
```
* **Dọn dẹp toàn bộ cache của Laravel:**
```bash
docker compose -f docker/docker-compose.yml exec app php artisan optimize:clear
```
* **Dừng các container:**
```bash
docker compose -f docker/docker-compose.yml down
```
+33
View File
@@ -0,0 +1,33 @@
services:
app:
build:
context: ..
dockerfile: docker/Dockerfile
image: thankcard-system-app
container_name: thankcard-system-app
working_dir: /var/www/html
volumes:
- ..:/var/www/html
env_file:
- ./env/.env.app
networks:
- thankcard-system-network
extra_hosts:
- "host.docker.internal:host-gateway"
nginx:
image: nginx:1.25-alpine
container_name: thankcard-system-nginx
ports:
- "8888:80"
volumes:
- ..:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
networks:
- thankcard-system-network
networks:
thankcard-system-network:
name: thankcard-system-network
View File
+23
View File
@@ -0,0 +1,23 @@
server {
listen 80;
server_name localhost;
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ /\.ht {
deny all;
}
}
+18
View File
@@ -0,0 +1,18 @@
#!/bin/sh
set -e
cd /var/www/html
if [ ! -f "vendor/autoload.php" ]; then
composer install
fi
mkdir -p storage/logs \
storage/framework/cache \
storage/framework/sessions \
storage/framework/views \
bootstrap/cache
chmod -R 777 storage bootstrap/cache
exec "$@"