How to Set Up MongoDB with Docker and Access it from Your Spring Boot Application

Ro ro
3 min readMar 14, 2024

--

In this tutorial, I will guide you step-by-step on how to set up a MongoDB instance from your IntelliJ Terminal.

(1) Install Docker

You will need Docker installed on your machine. To download, visit https://docs.docker.com/engine/install/ and download the package appropriate for your operating system.

(2) Install the Docker plugin

Go to Settings > Plugins > Marketplace and select Docker.

After installation, you will be able to see a console like this. You will need to connect this to the local Docker. Click on the “+” symbol and provide the path to your Docker. For detailed instructions, please refer to the official documentation provided here.

(3) Pull MongoDB Docker Image

Once docker is connected, we pull the MongoDB Docker image from the official Docker Hub repository. You can do this by running the following command in your terminal:

docker pull mongo:4.4.29

This will download the latest MongoDB 4.4.29 image to your local machine. I have not used the latest image due to compatibility issues

(4) Create and Start the MongoDB Container:

Once the image is downloaded, create and start a MongoDB container using Docker.

docker run -d -p 27017:27017 --name mongoDb -v mongo-data:/data/db mongo:4.4.29

This command creates a MongoDB container named “mongoDb”, exposes port 27017 (default MongoDB port) on the host machine, and mounts a volume for persistent data storage.

You can see the same container that started both on your IntelliJ service console and your Docker Desktop application.

MongoDB container running on the docker desktop application
MongoDB container running on the Docker Desktop
MongoDB container running on the IntelliJ console

(5) Access the MongoDB Shell:

To interact with the MongoDB instance running inside the container, you can access the MongoDB shell using the following command:

docker exec -it mongoDb mongo

This command opens the MongoDB shell for the “mongoDb” container, allowing you to execute MongoDB commands and queries directly from your terminal.

(6) Configure Connection Details

In your Spring Boot application, you need to specify the connection details for MongoDB. Add the following configuration to your application.properties or application.yml file:

spring.data.mongodb.uri=mongodb://localhost:27017
#specify the database name. if it does not exist, MongoDB will create it
spring.data.mongodb.database=donorDb

This configuration tells Spring Boot to connect to MongoDB running on localhost at port 27017 and to use the “donorDb” database. Adjust the URI and database name as needed for your application.

(7) Visualise and Analyse Data

You can connect to MongoDB via MongoDB Compass, where you can execute queries and visualize the data stored in the database.

Screenshot of the MongoDB compass with two document entries

Conclusion

Realizing the simplicity that Docker brings was a game-changer. Setting up MongoDB traditionally is tedious, requiring manual installation and configuration. However, with Docker, it is as easy as running a few commands in the terminal!

--

--