Set up MongoDB + Prisma with Docker

Haneen Mahdin
2 min readApr 6, 2023
MongoDB + Prisma + 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.

Prisma website screenshot
Prisma.io

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.

  1. Prisma required replica set for transactions
  2. 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.

The dockerfile from prisma’s repository for mongodb replica set
Dockerfile

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.

Docker compose file building from mongodb replica set image and publishing on PORT 27017 on container and 27017 on host.
Docker compose

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.

.env file showing the MongoDB connection string
.env file

You’re all set and now you can go ahead and run the docker-compose.yml file by running.

Running docker-compose in terminal with detached mode
Running docker-compose

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.

:)

--

--