How to run drizzle migrations before Next build when running on Docker?
I wanna run my drizzle migrations (generate and push) before Next.js starts building my app so I can have access to the database and the tables at build time.
I'm experimenting with SelfHosting, I have my Postgres database and my Next app running in separate containers from a docker-compose, I managed to make my migrations be ready for when I visit my website on production, and it works nicely, I did this by having 3 services in my compose.yaml: 1- my database (db), 2- my database migrations (migrations, depends on db, and run the drizzle scripts), and 3- my Next.js app (web, that depens on db and migrations).
db => runs first
migrations => runs second (supposedly)
web => runs third
I do not understand why my Next.js app won't have access to my database tables if the migration service already executed at this point (supposedly, unless I'm wrong, please correct me).
The issue is that my Next.js pages that access the database need to be forced dynamic so it the build doesn't break when attemping to generate static pages, since migrations haven't been aplied (database is up and runnig tho, but it's empty because tables haven't been created)
Anyway, this is my first time working with Docker trying to deploy a full-stack app to my VPS, after trying so many different thing for the last week, I've come to the conclusion that this doesn't even make sense at all, why do I want to have my migrations ready at build time to genrate static pages if the static page output will be empty because the database will be empty anyway. And in the case I require to generate static pages that depend on data, this data would be pulled from an external database or external APIs or services and those exist out of my containers anyway, so my pages will still be able to pre-render.
3 Replies
These are the important files (Dockerfile, compose.yaml, .env, etc...), my app works correctly and database is up, with the corresponding migrations when I visit my deployed app. But pages that access my database runnig on the contaner needed to be turned dynamic. I would appreciate any advice, solution or roast, I'll learn from whatever you give me.
Have you tried making a static page that pulls from other random open api just to get data?
Perhaps it's not your db that is the problem but the static generation?
Yes that works, because that API is external and always up and running out of my containers.
No, the database is not the problem, the time where migrations actually are applied seem to be. But maybe it’s not a problem at all and it’s a skill issue, which tbh might be since I’m fairly new to docker