-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.node-litestream
More file actions
119 lines (94 loc) · 3.72 KB
/
Dockerfile.node-litestream
File metadata and controls
119 lines (94 loc) · 3.72 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# ビルドステージ
FROM node:22-alpine AS builder
WORKDIR /app
# package*.jsonをコピー(package-lock.jsonも含む)
COPY package*.json ./
# 全ての依存関係をインストール(devDependenciesを含む)
RUN npm ci || npm install
# アプリケーションのソースをコピー
COPY . .
# フレームワーク検出とビルド
RUN if [ -f package.json ]; then \
if grep -q '"next"' package.json; then \
echo "Building Next.js app..." && npm run build; \
elif grep -q '"@react-router/dev"' package.json || grep -q '"react-router"' package.json; then \
echo "Building React Router app..." && npm run build; \
else \
echo "Building HonoX app..." && npm run build; \
fi \
fi
# 存在しないディレクトリを作成(COPYエラーを防ぐため)
RUN mkdir -p dist .next build public
# 実行ステージ
FROM node:22-alpine
# Install litestream and dependencies
RUN apk add --no-cache curl bash && \
curl -L https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.tar.gz | tar -C /usr/local/bin -xzf -
WORKDIR /app
# package*.jsonをコピー
COPY package*.json ./
# 本番用の依存関係のみインストール
RUN npm ci --only=production || npm install --production
# フレームワーク別の成果物をコピー
# 全てのフレームワークのビルド成果物をコピー(ビルダーステージですべて作成済み)
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/build ./build
COPY --from=builder /app/public ./public
# Litestream設定ファイルとSupervisord設定を生成するスクリプト
COPY <<'EOF' /app/setup-litestream.sh
#!/bin/bash
set -e
# Check if all required environment variables are set
if [ -n "$SAKURA_OBJECT_STORAGE_BUCKET" ] && \
[ -n "$SAKURA_OBJECT_STORAGE_ACCESS_KEY" ] && \
[ -n "$SAKURA_OBJECT_STORAGE_SECRET_KEY" ] && \
[ -n "$SQLITE_DB_PATH" ]; then
echo "Configuring Litestream backup..."
# Create directory for SQLite database if it does not exist
mkdir -p "$(dirname "$SQLITE_DB_PATH")"
# Create Litestream config
cat > /etc/litestream.yml <<LITESTREAM_EOF
dbs:
- path: $SQLITE_DB_PATH
replicas:
- type: s3
endpoint: https://s3.isk01.sakurastorage.jp
bucket: $SAKURA_OBJECT_STORAGE_BUCKET
path: ${APP_NAME}/${SQLITE_DB_PATH##*/}
access-key-id: $SAKURA_OBJECT_STORAGE_ACCESS_KEY
secret-access-key: $SAKURA_OBJECT_STORAGE_SECRET_KEY
sync-interval: ${LITESTREAM_REPLICATE_INTERVAL:-10s}
LITESTREAM_EOF
# Restore database from backup if it does not exist
if [ ! -f "$SQLITE_DB_PATH" ]; then
echo "Database not found, attempting to restore from backup..."
if /usr/local/bin/litestream restore -config /etc/litestream.yml -if-replica-exists "$SQLITE_DB_PATH"; then
echo "Database restored successfully from backup"
else
echo "No backup found or restore failed, starting with fresh database"
fi
fi
echo "Starting Litestream with app integration..."
sync
# Start Litestream with exec to run the app
cd /app
exec /usr/local/bin/litestream replicate -config /etc/litestream.yml -exec "npm start"
else
echo "Litestream backup not configured, starting app directly..."
sync
# Start app directly without litestream
exec npm start
fi
EOF
RUN chmod +x /app/setup-litestream.sh
# ビルド時引数でポートを受け取る
ARG PORT=3000
# 環境変数
ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=$PORT
# ポートを公開(デフォルト3000、ビルド時に変更可能)
EXPOSE $PORT
# エントリーポイントとして設定スクリプトを使用
ENTRYPOINT ["/app/setup-litestream.sh"]