How to run migrations in database inside Dockerfile using Node.js? #232
Replies: 1 comment 3 replies
-
OK, so there's a lot here and it's unclear what the exact question/problem is. I see you've updated your SO question saying you've fixed it. In general, you don't create a db, or run db migrations in a Dockerfile at image-build-time, you run them as a job in swarm/kubernetes (if that's what you're using) outside of the apps. One of the main reasons it's not a good idea to run migrations with the app (nextjs, etc.) container is that you'll usually run many of those for load balancing and redundancy in production, and you only want one migration job running (not one per container replica). This has lead to many different designs of how to do migrations during an apps update (replacing all the containers with the new version of the app), and it sometimes depends on the features of the ORM/app and sometimes limited by the features of the runtime (docker, compose, swarm, kubernetes, etc.) |
Beta Was this translation helpful? Give feedback.
-
I've been trying to get Database migrations working in Drizzle + SQLite but have no luck so far.
I've had questions that aren't solved. Like for example,
drizzle-orm
is not found becausenode_modules
doesn't have a copy of it.I tried using anonymous volumes as suggested here but despite doing that, I still get the same error when i try
docker logs nextjs-sqlite
:docker-compose.yml
Dockerfile
run.sh
I can't run migrations in dockerfile as it will build it in the image so
.sqlite
file won't be created.I have to run it in the container just before I run my next.js server so
.sqlite
file gets created.Locally, I do
npm run db:generate
to createmigrations/
folder &npm run db:migrate
to createusers.dev.sqlite
which reads files frommigrations/
folder.My basic goal is to get
*.sqlite
file in the production while using bind mount. But unsuccessful for a week.Other ORMs do it easily like I found this blog that does it easily with Prisma + Nest -> https://notiz.dev/blog/prisma-migrate-deploy-with-docker
But I'm using Drizzle instead of Prisma which doesn't have a direct command like
prisma migrate deploy
so I have to run an npm script.How would I do it?
I have a sample proof-of-concept -> https://github.com/deadcoder0904/easypanel-nextjs-sqlite (Next.js + Drizzle + SQLite + Docker)
My entire goal is to make SQLite file successfully created in production docker container. Unfortunately, there are no answers for Drizzle + Docker rather than complex hacks which I only found one person implement.
Is there a solution to this?
Beta Was this translation helpful? Give feedback.
All reactions