Docker Install MongoDB

Docker Install MongoDB

MongoDB is a free open source cross-platform document-oriented NoSQL database program.

1. Check the available MongoDB versions

Visit the MongoDB mirror repository at https://hub.docker.com/_/mongo?tab=tags&page=1.

You can view other versions of MongoDB by Sort by, the default is the latest version mongo:latest.

Docker Install MongoDB

You can also find other versions you want in the drop-down list –

Docker Install MongoDB

In addition, we can use the docker search mongo command to see the 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. fetch the latest version of the MongoDB image

Here we pull the official image of the latest version:

$ docker pull mongo:latest

Docker Install MongoDB

3. Check the local image

Use the following command to see if mongo is installed.

$ docker images

Docker Install MongoDB

In the above image you can see that we have installed the latest version (latest) of the mongo image.

4. Run the container

Once the installation is complete, we can run the mongo container using the following command.

$ docker run -itd --name mongo -p 27017:27017 mongo --auth

Parameter Description:

  • -p 27017:27017: Maps port 27017 of the container service to port 27017 of the host. External access to mongo’s services can be made directly through the host ip:27017.
  • –auth: requires a password to access the container service.

Docker Install MongoDB

5. Successful installation

Finally, we can view the running information of the container by using the docker ps command:

Docker Install MongoDB

Next, use the following command to add a user and set a password, and try to connect.

$ docker exec -it mongo mongo admin
# Create a user named admin with a password of 123456.
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}, "readWriteAnyDatabase"]});
# Try to connect using the user information created above.
> db.auth('admin', '123456')

Docker Install MongoDB

Like(0)