In this guide we are going to explore how to run Mongodb locally with docker and docker compose. This can be helpful if you want to run Mongodb locally without installing it in your server or if you want to run multiple versions of Mongodb seamlessly.
Also check:
- How to Set up Authentication in Mongodb
- How to install and configure MongoDB 6 on Ubuntu 22.04
- How to install Mongodb 5 in Rocky Linux/Centos 8
- How to run Redis 6 with Docker and Docker-Compose
- Running Postgresql 14 with Docker and Docker-Compose
- How to run MsSQL Server 2019 with Docker and Docker-Compose
- How to run Mysql 8 with Docker and Docker-Compose
- How to run Cassandra 4 with Docker and Docker-Compose
Prerequisites
To follow along, ensure you have the following:
- Docker installed locally and permissions to use it to launch containers
- Docker compose is installed locally
- Basic knowledge of the command line
Using the docker run command
We are going to use the docker run
command to achieve our goal. The version of Mongodb that we want is mongo:5.0
the latest Mongodb version.
|
|
In the above command:
- The
-d
instructs docker container to run as a detached process. It run container in background and print container ID -p
is for port mapping. We are instructing the container to expose the container port externally. Container port6379
is mapped to host port6379
. That means the service can be accessed throughlocalhost:6379
- The
mongo:5.0
defines the image that we are running. We are running mongo image version 5.0. - The
-e
argument is for the environment variables. The supplied environment variables will be used to set up a Mongo DB root user and password.
To check that our container is running as expected, use the docker ps
command:
|
|
In my case the container is running as my-mongodb
the name we gave it. We can login to the container using the docker exec
command while executing /bin/bash
interactively. Here we are also logging in to posgtres with the credentials we specified above and checking the version.
|
|
If you need to clean up the container when not in use, you can stop and remove the container using this command:
|
|
Using the docker-compose tool
We can achieve the same functionality with docker-compose
. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.
Docker Compose allows you to define the service (Mongodb in our case) with properties like the image
to use, command
to overide the default Dockerfile command and andports
to expose.
Here is how we would use docker-compose
to achieve the functionality above. Save this as docker-compose.yaml
:
|
|
Now bring up the containers:
|
|
The commands:
up
brings up the container-d
in a detached mode
Verify the container processes using the ps command:
|
|
To login to the container and login to mongodb, use this:
|
|
Conclusion
In this guide we managed to run Mongodb 5 as a container in our system, we explored using the docker run
command while passing the required arguments an alternative approach of simplifying the process with docker-compose