Docker Installation of MongoDB
MongoDB is a free, open-source, cross-platform, document-oriented NoSQL database program.
1. Check Available MongoDB Versions
Visit the MongoDB image repository address: https://hub.docker.com/_/mongo?tab=tags&page=1.
You can view other versions of MongoDB by sorting with "Sort by," and the default is the latest version, mongo:latest.
You can also find other versions you want in the dropdown list:
Additionally, we can use the docker search mongo
command to check available versions:
$ docker search mongo
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mongo MongoDB document databases ... 1989 [OK]
mongo-express Web-based MongoDB admin int... 22 [OK]
mvertes/alpine-mongo light MongoDB container 19 [OK]
mongooseim/mongooseim-docker MongooseIM server the lates... 9 [OK]
torusware/speedus-mongo Always updated official Mon... 9 [OK]
jacksoncage/mongo Instant MongoDB sharded cluster 6 [OK]
mongoclient/mongoclient Official docker image for M... 4 [OK]
jadsonlourenco/mongo-rocks Percona Mongodb with Rocksd... 4 [OK]
asteris/apache-php-mongo Apache2.4 + PHP + Mongo + m... 2 [OK]
19hz/mongo-container Mongodb replicaset for coreos 1 [OK]
nitra/mongo Mongo3 centos7 1 [OK]
ackee/mongo MongoDB with fixed Bluemix p... 1 [OK]
kobotoolbox/mongo https://github.com/kobotoolb... 1 [OK]
valtlfelipe/mongo Docker Image based on the la... 1 [OK]
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 installed:
$ docker images
From 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 -itd --name mongo -p 27017:27017 mongo --auth
Parameter explanations:
- -p 27017:27017: Maps the container service's 27017 port to the host's 27017 port. External access can directly reach the mongo service via the host's IP:27017.
- --auth: Requires a password to access the container service.
5. Successful Installation
Finally, we can view the container's running information using the docker ps command:
Next, use the following commands to add a user and set a password, and attempt to connect.
$ docker exec -it mongo mongo admin
# Create a user named admin with the password 123456.
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
# Attempt to connect using the created user information.
> db.auth('admin', '123456')
For MongoDB 6.0 and above, use the following command:
docker exec -it mongo mongosh admin