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:
Then you can use this script in your Dockerfile's CMD:
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.
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:
Then you can use this script in your Dockerfile's CMD:
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.