fix: docker permission again two.
Some checks failed
Build and Push Docker Image / buildx (push) Has been cancelled
Some checks failed
Build and Push Docker Image / buildx (push) Has been cancelled
This commit is contained in:
29
Dockerfile
29
Dockerfile
@@ -1,34 +1,19 @@
|
|||||||
FROM node:25-alpine AS base
|
FROM node:25-alpine
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Dependencies stage
|
# Install dependencies
|
||||||
FROM base AS deps
|
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install --production
|
RUN npm install --production
|
||||||
|
|
||||||
# Production stage
|
# Copy application code
|
||||||
FROM base AS production
|
|
||||||
COPY --from=deps /app/node_modules ./node_modules
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Copy init script
|
# Create directory for database with open permissions
|
||||||
COPY scripts/init-data-dir.sh /usr/local/bin/init-data-dir.sh
|
RUN mkdir -p /app/data && chmod 777 /app/data
|
||||||
RUN chmod +x /usr/local/bin/init-data-dir.sh
|
|
||||||
|
|
||||||
EXPOSE 3000
|
|
||||||
|
|
||||||
# Create a non-root user
|
|
||||||
RUN addgroup -g 1001 -S nodejs
|
|
||||||
RUN adduser -S nextjs -u 1001
|
|
||||||
|
|
||||||
# Create directory for database
|
|
||||||
RUN mkdir -p /app/data
|
|
||||||
|
|
||||||
USER nextjs
|
|
||||||
|
|
||||||
# Set environment variable for database path
|
# Set environment variable for database path
|
||||||
ENV DB_PATH=/app/data/data.db
|
ENV DB_PATH=/app/data/data.db
|
||||||
|
|
||||||
# Use init script as entrypoint wrapper
|
EXPOSE 3000
|
||||||
ENTRYPOINT ["/usr/local/bin/init-data-dir.sh"]
|
|
||||||
CMD ["node", "server.js"]
|
CMD ["node", "server.js"]
|
||||||
|
|||||||
@@ -12,15 +12,6 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
- PORT=3000
|
- PORT=3000
|
||||||
- JWT_SECRET=${JWT_SECRET:-change-this-secret-key-in-production}
|
- JWT_SECRET=${JWT_SECRET:-change-this-secret-key-in-production}
|
||||||
- DB_PATH=/app/data/data.db
|
|
||||||
volumes:
|
volumes:
|
||||||
- ./data:/app/data
|
- ./data:/app/data
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
depends_on:
|
|
||||||
- init-data-dir
|
|
||||||
|
|
||||||
init-data-dir:
|
|
||||||
image: alpine:latest
|
|
||||||
command: chown -R 1001:1001 /app/data
|
|
||||||
volumes:
|
|
||||||
- ./data:/app/data
|
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
# Create data directory if it doesn't exist
|
# Create data directory if it doesn't exist
|
||||||
mkdir -p /app/data
|
mkdir -p /app/data
|
||||||
|
|
||||||
# Try to set permissions for the data directory
|
# Try to create the database file if it doesn't exist
|
||||||
# This might fail in some environments, but that's okay
|
touch /app/data/data.db 2>/dev/null || true
|
||||||
chown -R 1001:1001 /app/data 2>/dev/null || true
|
|
||||||
chmod 755 /app/data 2>/dev/null || true
|
|
||||||
|
|
||||||
echo "Data directory initialized"
|
# Set permissions for the data directory and database file
|
||||||
|
chmod 777 /app/data 2>/dev/null || true
|
||||||
|
chmod 666 /app/data/data.db 2>/dev/null || true
|
||||||
|
|
||||||
|
echo "Data directory initialized with open permissions"
|
||||||
|
|
||||||
# Execute the main command
|
# Execute the main command
|
||||||
exec "$@"
|
exec "$@"
|
||||||
|
|||||||
@@ -1,9 +1,32 @@
|
|||||||
const sqlite3 = require('sqlite3').verbose();
|
const sqlite3 = require('sqlite3').verbose();
|
||||||
const { DB_PATH } = require('./config');
|
const { DB_PATH } = require('./config');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
// Ensure the directory exists
|
||||||
|
const dbDir = path.dirname(DB_PATH);
|
||||||
|
if (!fs.existsSync(dbDir)) {
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(dbDir, { recursive: true });
|
||||||
|
console.log('Created database directory:', dbDir);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to create database directory:', err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to create the database file if it doesn't exist
|
||||||
|
try {
|
||||||
|
fs.closeSync(fs.openSync(DB_PATH, 'a'));
|
||||||
|
console.log('Database file is accessible:', DB_PATH);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to access database file:', err.message);
|
||||||
|
}
|
||||||
|
|
||||||
const db = new sqlite3.Database(DB_PATH, (err) => {
|
const db = new sqlite3.Database(DB_PATH, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('数据库连接失败:', err.message);
|
console.error('数据库连接失败:', err.message);
|
||||||
|
console.error('数据库路径:', DB_PATH);
|
||||||
|
console.error('当前工作目录:', process.cwd());
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
console.log('已连接到 SQLite 数据库');
|
console.log('已连接到 SQLite 数据库');
|
||||||
|
|||||||
Reference in New Issue
Block a user