Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Litestreamを用いてDBを永続化 #38

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN groupadd -r appuser && useradd -r -g appuser -s /bin/false appuser
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
curl=8.5.0-2ubuntu10.4 \
unzip=6.0-28ubuntu4.1 \
openssl=3.0.13-0ubuntu3.4 \
ca-certificates=20240203 \
&& rm -rf /var/lib/apt/lists/*
curl \
unzip \
openssl \
ca-certificates \
sqlite3 \
sqlite3-tools \
&& rm -rf /var/lib/apt/lists/* \
&& curl -L https://github.com/benbjohnson/litestream/releases/download/v0.3.13/litestream-v0.3.13-linux-amd64.tar.gz -o /tmp/litestream.tar.gz \
&& tar -C /usr/local/bin -xzf /tmp/litestream.tar.gz \
&& rm /tmp/litestream.tar.gz
USER appuser
WORKDIR /home/appuser
ENV MISE_ROOT="/home/appuser/.local/share/mise"
Expand Down Expand Up @@ -44,28 +49,42 @@ FROM deps AS builder
WORKDIR /app
USER appuser
COPY --chown=appuser:appuser . .
ENV DATABASE_URL=file:/app/prisma/data/deploy.db
RUN mkdir -p prisma/data && \
touch prisma/data/deploy.db && \
ENV DATABASE_URL=file:/app/db/deploy.db
RUN mkdir -p db && \
touch db/deploy.db && \
bun prisma migrate deploy && \
bun run build
USER root
RUN mkdir -p /tmp/prod/app && \
cp -r build /tmp/prod/app/ && \
cp -r prisma /tmp/prod/app/ && \
cp -r db /tmp/prod/app/ && \
cp package.json /tmp/prod/app/ && \
chown -R 65532:65532 /tmp/prod && \
chmod -R 755 /tmp/prod/app && \
chmod 777 /tmp/prod/app/prisma/data && \
touch /tmp/prod/app/prisma/data/deploy.db && \
chmod 666 /tmp/prod/app/prisma/data/deploy.db
chmod 666 /tmp/prod/app/db/deploy.db

FROM gcr.io/distroless/nodejs22-debian12:nonroot AS runner
FROM node:20-slim AS runner
sou1118 marked this conversation as resolved.
Show resolved Hide resolved
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends \
sqlite3 \
ca-certificates \
gettext-base \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir -p /app/db && \
chown -R node:node /app
COPY --from=base /usr/local/bin/litestream /usr/local/bin/
COPY litestream.yml.template /etc/litestream.yml.template
COPY run.sh /run.sh
RUN chmod +x /run.sh && \
chown -R node:node /etc/litestream.yml.template
COPY --from=prod-deps /tmp/prod-deps/node_modules ./node_modules
COPY --from=builder /tmp/prod/app ./
ENV NODE_ENV=production \
DATABASE_URL=file:/app/prisma/data/deploy.db
USER 65532:65532
DATABASE_URL=file:/app/db/deploy.db
RUN chown -R node:node /app && \
chmod 755 /app/db && \
chmod 666 /app/db/deploy.db
USER node
EXPOSE 3000
CMD ["node_modules/@remix-run/serve/dist/cli.js", "build/server/index.js"]
ENTRYPOINT ["/run.sh"]
4 changes: 2 additions & 2 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ services:
ports:
- "3000:3000"
volumes:
- sqlite_data:/app/prisma/data
- sqlite_data:/app/db
environment:
- NODE_ENV=production
- DATABASE_URL=file:/app/prisma/data/deploy.db
- DATABASE_URL=file:/app/db/deploy.db
user: "65532:65532"
restart: unless-stopped

Expand Down
6 changes: 6 additions & 0 deletions litestream.yml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dbs:
- path: /app/db/deploy.db
replicas:
- type: gcs
bucket: ${LITESTREAM_BUCKET}
path: deploy.db
29 changes: 29 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh
set -e

# Generate litestream.yml in the app directory where node user has write permission
envsubst < /etc/litestream.yml.template > /app/litestream.yml

# Remove existing db if exists
if [ -f /app/db/deploy.db ]; then
mv /app/db/deploy.db /app/db/deploy.db.bak
fi

# Restore database from GCS if exists
litestream restore -if-replica-exists -config /app/litestream.yml /app/db/deploy.db

if [ -f /app/db/deploy.db ]; then
echo "Successfully restored database from Cloud Storage"
rm -f /app/db/deploy.db.bak
else
echo "No database backup found in Cloud Storage, using original"
mv /app/db/deploy.db.bak /app/db/deploy.db
fi

# Before starting the app, ensure WAL mode is disabled for Prisma compatibility
sqlite3 /app/db/deploy.db "PRAGMA journal_mode=DELETE;"

# Start Litestream replication with the app as the subprocess
exec litestream replicate \
-config /app/litestream.yml \
-exec "node node_modules/@remix-run/serve/dist/cli.js build/server/index.js"