P
Prismaβ€’5w ago
Luis

Automatically create SQLite database and run Prisma migrations in Docker for Next.js app

I have a Dockerfile that builds a Next.js application using Prisma with a SQLite database. I'd like to know if there's a recommended or automated way to create the SQLite database file and run the necessary Prisma migrations directly inside the Docker container, before the Next.js app starts. What's the best practice to achieve this setup?
Solution:
Hey πŸ‘‹ The most common approach is to run Prisma migrations as part of your application startup process. You can modify your Docker container's startup command to run migrations before starting the Next.js application. You can create a custom startup script or modify your package.json to run migrations before starting the application:...
Jump to solution
3 Replies
Prisma AI Help
Prisma AI Helpβ€’5w ago
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into #ask-ai for a quick spin!
Solution
Nurul
Nurulβ€’4w ago
Hey πŸ‘‹ The most common approach is to run Prisma migrations as part of your application startup process. You can modify your Docker container's startup command to run migrations before starting the Next.js application. You can create a custom startup script or modify your package.json to run migrations before starting the application:
{
"scripts": {
"start:prod": "prisma migrate deploy && next start"
}
}
{
"scripts": {
"start:prod": "prisma migrate deploy && next start"
}
}
Then you can use this script in your Dockerfile's CMD:
CMD ["npm", "run", "start:prod"]
CMD ["npm", "run", "start:prod"]
For SQLite specifically, you don't need to explicitly create the database file. When Prisma connects to a SQLite database, it automatically creates the file if it doesn't exist.
Luis
LuisOPβ€’3w ago
Hey! πŸ‘‹ Sorry for the delayed response, and thank you so much for your detailed replyβ€”it really helped me understand how to set things up correctly! In the end, I opted for a similar solution, using a specific entrypoint file in the Dockerfile. Here's how I implemented it: In the Dockerfile:
COPY --chown=nextjs:nodejs entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

CMD ["node", "server.js"]

ENTRYPOINT ["./entrypoint.sh"]
COPY --chown=nextjs:nodejs entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

CMD ["node", "server.js"]

ENTRYPOINT ["./entrypoint.sh"]
And here's the content of the entrypoint.sh file:
#!/bin/sh
set -e

# Prisma migrations
npx prisma migrate deploy & PID=$!
wait $PID

exec "$@"
#!/bin/sh
set -e

# Prisma migrations
npx prisma migrate deploy & PID=$!
wait $PID

exec "$@"
Two quick tips based on my experience: - Carefully check the location of your entrypoint.sh file. - Always add #!/bin/sh at the beginning of your entrypoint script because bash may not work correctly inside Alpine Docker images (so you need to use sh). Thanks again for your valuable help! πŸ™πŸ˜Š

Did you find this page helpful?