File size: 2,070 Bytes
ddce7e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const rootDir = path.resolve(__dirname, '..');
const envFile = path.join(rootDir, '.env');
const configFile = path.join(rootDir, 'config.json');
const envExample = path.join(rootDir, '.env.example');
const configExample = path.join(rootDir, 'config.json.example');

console.log('🐳 开始构建 Docker 镜像...\n');

// 检查并复制 .env
if (!fs.existsSync(envFile)) {
  if (fs.existsSync(envExample)) {
    fs.copyFileSync(envExample, envFile);
    console.log('✓ 已从 .env.example 创建 .env');
  } else {
    console.warn('⚠ 未找到 .env.example,将使用默认配置');
  }
} else {
  console.log('✓ .env 已存在');
}

// 检查并复制 config.json
if (!fs.existsSync(configFile)) {
  if (fs.existsSync(configExample)) {
    fs.copyFileSync(configExample, configFile);
    console.log('✓ 已从 config.json.example 创建 config.json');
  } else {
    console.warn('⚠ 未找到 config.json.example,将使用默认配置');
  }
} else {
  console.log('✓ config.json 已存在');
}

// 确保必要的目录存在(防止 Docker 挂载时创建文件夹)
const dataDir = path.join(rootDir, 'data');
const imagesDir = path.join(rootDir, 'public', 'images');

if (!fs.existsSync(dataDir)) {
  fs.mkdirSync(dataDir, { recursive: true });
  console.log('✓ 已创建 data 目录');
}

if (!fs.existsSync(imagesDir)) {
  fs.mkdirSync(imagesDir, { recursive: true });
  console.log('✓ 已创建 public/images 目录');
}

// 构建镜像
console.log('\n📦 正在构建镜像...\n');
try {
  execSync('docker compose build', { 
    cwd: rootDir, 
    stdio: 'inherit' 
  });
  console.log('\n✅ 镜像构建成功!');
  console.log('\n运行以下命令启动服务:');
  console.log('  docker compose up -d');
} catch (error) {
  console.error('\n❌ 构建失败');
  process.exit(1);
}