Prisma Migrate: Deploy Migration with Docker
Perform database migration with Prisma Migrate using Docker
- Authors
Marc Stammerjohann
- Published at
One question about the guide Dockerizing a NestJS app with Prisma and PostgreSQL was asked frequently: "How to run database migrations in production?"
Let's dive into this topic and find out how to use prisma migrate deploy
with Docker for your production database.
Prisma recommends to perform migrations for production database in an automated step and advises against performing it locally π ββοΈ
Prisma Migrate with Docker
Let's take a look at how to integrate the command prisma migrate deploy
into the following Dockerfile
.
First of all, one command is very important: COPY prisma ./prisma/
. This makes sure to not only copy the schema.prisma
into the Docker image, but also includes the migrations
directory. This is necessary as prisma migrate deploy
only executes the migration history files.
Add a RUN command β
What about adding RUN npx prisma migrate deploy
to the Dockerfile
?
Two issues that come to mind are making it not a good solution.
- Performs migration during build step, not before Docker container is started
- CI/CD needs access to your production database, requires
DATABASE_URL
environment
RUN
commands are executed during the build steps of your the Docker image. Time may pass between the database migration and restarting the Docker container (Nest app), leaving your database and Nest app in different states. Additionally, when you create your Docker image in a CI/CD pipeline, the server needs access to the production database to perform the migration.
Add to CMD β
A better approach is to perform the migration just before starting your Nest app.
Nest app is started with the last command CMD [ "npm", "run", "start:prod" ]
. CMD is not executed during the build steps of your Docker image, rather than during executing the Docker container (docker run ...
or docker-compose up
).
This is a multi-stage build Dockerfile
, you need to copy the prisma
directory including the migration history into the second stage.
Add a new script to your package.json
to execute prisma migrate deploy && npm run start:prod
.
Use the new script as CMD
command
When testing locally make sure that your DATABASE_URL
in your .env
file is not using localhost
as the host π
ββοΈ. Replace localhost
with the database container name.
Now start your Docker containers with docker-compose up
to see the logs and verify that your migrations are performed. In case all migrations have already been applied to your database, Prisma Migrate will complete with "No pending migrations to apply.".
Hurray π the database migrations are executing successfully with Docker! Test it out on your test, staging and then production environment.
Sponsor us
Did you find this post useful? We at notiz.dev write about our experiences developing Apps, Websites and APIs and develop Open Source tools. Your support would mean a lot to us π. Receive a reward by sponsoring us on Patreon or start with a one-time donation on GitHub Sponsors.