This commit is contained in:
aaron 2025-08-10 12:49:53 +08:00
parent 5a6ac68390
commit 8e285a15f9
3 changed files with 7 additions and 18 deletions

View File

@ -13,20 +13,8 @@ RUN if [ -f package-lock.json ]; then npm ci --omit=dev; else npm install --prod
# 复制应用源代码
COPY . .
# 创建非root用户
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001
# 创建数据库目录并设置权限
RUN mkdir -p database && \
touch database/shop.db && \
chown -R nodejs:nodejs /app && \
chmod -R 755 /app && \
chmod 777 database && \
chmod 666 database/shop.db
# 切换到非root用户
USER nodejs
# 创建数据库目录
RUN mkdir -p /data/database
# 暴露端口
EXPOSE 3000

View File

@ -8,16 +8,16 @@ services:
environment:
- NODE_ENV=production
- PORT=3000
- DB_PATH=/data/database/shop.db
# UPay配置 - 生产环境时需要修改这些值
- UPAY_APP_ID=${UPAY_APP_ID:-M1C40DvS}
- UPAY_APP_SECRET=${UPAY_APP_SECRET:-a2nqkkqRb09LIe87}
volumes:
# 持久化数据库文件
- ./database:/app/database:rw
# 持久化数据库文件到专用目录
- ./database:/data/database:rw
# 持久化上传的图片(如果有)
- ./public/images:/app/public/images:rw
restart: unless-stopped
user: "1001:1001"
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000"]
timeout: 5s

View File

@ -16,7 +16,8 @@ app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// 数据库初始化
const db = new sqlite3.Database('./database/shop.db');
const dbPath = process.env.DB_PATH || './database/shop.db';
const db = new sqlite3.Database(dbPath);
// 创建订单表
db.serialize(() => {