Set up MongoDB + Prisma with Docker
Prisma is an ORM for database — a server-side library that helps developers read and write data to the database in an intuitive, efficient and safe way.
Setting up MongoDB Atlas with Prisma is very simple but however setting up MongoDB locally on docker with prisma is a bit of a tricky process. Today, I will you through how we can solve this issue.
We face three two main problems when setting up Prisma with MongoDB on Docker.
- Prisma required replica set for transactions
- Connection string requires authentication source
To fix this issue, we are going to create our custom docker image. Go ahead and create a Dockerfile inside a directory named mongodb_rs
inside your project folder. You can name this whatever you want but this will be later used in the docker-compose.yml
file.
Copy the contents of the Dockefile
from prisma’s repository and paste it into the docker file we created.
Create a new docker-compose.yml
file in project folder.
Now we’ve fixed the first issue.
Let’s move on to the second one!
The connection string needs to know the authentication source of our connection. In order to fix this, we have to add ?authSource=admin
in the url.
You’re all set and now you can go ahead and run the docker-compose.yml
file by running.
You can see the logs by running:
docker-compose logs -f
If you see a message ‘REPLICA SET ONLINE’ that means your container is up and running.
That’s it! Thanks for reading.
:)