MongoDB is a free, open-source, cross-platform, document-oriented NoSQL database program.
1. Check Available MongoDB Versions
Visit the MongoDB image repository: https://hub.docker.com/_/mongo?tab=tags&page=1.
You can view other MongoDB versions via "Sort by". The default is the latest version mongo:latest.
You can also find other desired versions in the dropdown list:
Additionally, we can use the docker search mongo command to view available versions:
$ docker search mongo
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mongo MongoDB document databases ... 1989
mongo-express Web-based MongoDB admin int... 22
mvertes/alpine-mongo light MongoDB container 19
mongooseim/mongooseim-docker MongooseIM server the lates... 9
torusware/speedus-mongo Always updated official Mon... 9
jacksoncage/mongo Instant MongoDB sharded cluster 6
mongoclient/mongoclient Official docker image for M... 4
jadsonlourenco/mongo-rocks Percona Mongodb with Rocksd... 4
asteris/apache-php-mongo Apache2.4 + PHP + Mongo + m... 2
19hz/mongo-container Mongodb replicaset for coreos 1
nitra/mongo Mongo3 centos7 1
ackee/mongo MongoDB with fixed Bluemix p... 1
kobotoolbox/mongo https://github.com/kobotoolb... 1
valtlfelipe/mongo Docker Image based on the la... 1
2. Pull the Latest MongoDB Image
Here we pull the official latest version of the image:
$ docker pull mongo:latest
3. Check Local Images
Use the following command to check if mongo is already installed:
$ docker images
In the image above, we can see that we have installed the latest version (latest) of the mongo image.
4. Run the Container
After installation, we can use the following command to run the mongo container:
docker run -d -p 27017:27017 --name my-mongo-container mongo
Parameter explanation:
-d: Run the container in the background.-p 27017:27017: Map port 27017 of the host to port 27017 of the container.--name my-mongo-container: Assign a name to the container, heremy-mongo-container. You can change it as needed.
5. Installation Successful
Finally, we can check the container's running information with the docker ps command:
# docker ps
CONTAINER ID IMAGE ... PORTS NAMES
d53e5d57668b mongo ... :::27017->27017/tcp my-mongo-container
You should see the MongoDB container named my-mongo-container running.
Next, we can use a MongoDB client (e.g., the mongo shell) to connect to the running MongoDB container.
You can use the following command to connect to MongoDB:
$ mongosh --host 127.0.0.1 --port 27017
Current Mongosh Log ID: 656d34911ff5455b0c3afdc0
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.0
Using MongoDB: 7.0.4
Using Mongosh: 2.1.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
...
This will connect to port 27017 on the local host. You can adjust it based on the port you mapped earlier.
To enter the bash shell of the MongoDB container, use the following command:
docker exec -it my-mongo-container bash
Remember to stop and remove the container when it's no longer needed. You can use the following commands:
docker stop my-mongo-container
docker rm my-mongo-container
YouTip