From 54b215152f0f7f27cefede656328c1ec4ec127b3 Mon Sep 17 00:00:00 2001 From: Nice Try Date: Fri, 26 Jun 2026 06:07:29 +0000 Subject: [PATCH] fix: initialize sqlite schema on docker startup --- Dockerfile | 16 +++++++++++----- docker-entrypoint.sh | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 09a2e39..7a231cc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,7 +50,7 @@ ENV PORT=6767 ENV HOSTNAME=0.0.0.0 RUN apt-get update \ - && apt-get install -y --no-install-recommends openssl ca-certificates \ + && apt-get install -y --no-install-recommends openssl ca-certificates gosu \ && rm -rf /var/lib/apt/lists/* \ && groupadd --system --gid 1001 nodejs \ && useradd --system --uid 1001 --gid nodejs nextjs @@ -61,12 +61,18 @@ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --chown=nextjs:nodejs /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma -# Runtime writable folders. Users can bind-mount these from anywhere. -RUN mkdir -p /app/My_Courses /app/prisma \ - && chown -R nextjs:nodejs /app/My_Courses /app/prisma +# Keep Prisma CLI available at runtime so fresh bind-mounted SQLite databases +# can be initialized automatically before the server starts. +COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules +COPY --from=deps --chown=nextjs:nodejs /app/package.json ./package.json +COPY --from=deps --chown=nextjs:nodejs /app/package-lock.json ./package-lock.json -USER nextjs +COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh +RUN chmod +x /usr/local/bin/docker-entrypoint.sh \ + && mkdir -p /app/My_Courses /app/prisma \ + && chown -R nextjs:nodejs /app/My_Courses /app/prisma EXPOSE 6767 +ENTRYPOINT ["docker-entrypoint.sh"] CMD ["node", "server.js"] diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..c8229ab --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -e + +# Ensure bind-mounted runtime folders exist. +mkdir -p /app/My_Courses /app/prisma + +# If the container starts as root, make common bind mounts writable by the app user. +# This helps when users create ./prisma/dev.db or ./My_Courses on the host first. +if [ "$(id -u)" = "0" ]; then + chown -R nextjs:nodejs /app/My_Courses /app/prisma 2>/dev/null || true +fi + +# A fresh Docker deployment often bind-mounts an empty SQLite file at /app/prisma/dev.db. +# Initialize/update the Prisma schema before the Next.js server starts. +if [ -n "${DATABASE_URL:-}" ]; then + echo "Initializing SQLite schema with Prisma..." + gosu nextjs:nodejs ./node_modules/.bin/prisma db push --schema=/app/prisma/schema.prisma --skip-generate +fi + +exec gosu nextjs:nodejs "$@"