Automatically create SQLite database and run Prisma migrations in Docker for Next.js app
Answered
Barbary Lion posted this in #help-forum
Barbary LionOP
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?
Answered by American black bear
For migrations someone mentioned to me to append migration command to build script inside package.json scripts:
For sqlite file I've never used it. I suppose something similar could work. Hopefully someone else helps you with that.
// package.json
{
"scripts": {
"build": "migrate-command && next build"
}
}
For sqlite file I've never used it. I suppose something similar could work. Hopefully someone else helps you with that.
3 Replies
American black bear
For migrations someone mentioned to me to append migration command to build script inside package.json scripts:
For sqlite file I've never used it. I suppose something similar could work. Hopefully someone else helps you with that.
// package.json
{
"scripts": {
"build": "migrate-command && next build"
}
}
For sqlite file I've never used it. I suppose something similar could work. Hopefully someone else helps you with that.
Answer
Barbary LionOP
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:
And here's the content of the
Two quick tips based on my experience:
- Carefully check the location of your
- Always add
Thanks again for your valuable help! 🙏😊
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"]
And here's the content of the
entrypoint.sh
file:#!/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! 🙏😊
American black bear
Glad I could help :D