Docker > Docker >> Docker Images and Containers Views : 2345
Rate This Article :

How to deploy and use Mongo Db image using Docker

Introduction

This article describes how to deploy “Mongo Db” inside Docker Container and interact with “Mongo” Db through bash.

Prerequisites

1.       Docker should be installed on your Machine.

2.       Should have basic knowledge in Docker and its Commands.

Step 1:

Use the below command to Pull “Mongo” image From “Docker Hub

docker pull mongo

You will be getting below output (kind of) after executing the above command,

Using default tag: latest

latest: Pulling from library/mongo

2746a4a261c9: Pull complete

4c1d20cdee96: Pull complete

0d3160e1d0de: Pull complete

c8e37668deea: Pull complete

fc3987a82b4c: Pull complete

c75f139e0836: Pull complete

4acc9c8680b4: Pull complete

fb02df30d947: Pull complete

ae725ef3d2ce: Pull complete

e30f54ed6b43: Pull complete

bca9e535ddb8: Pull complete

9c3edad81b2a: Pull complete

6dbcf78fe5ae: Pull complete

Digest: sha256:7a1406bfc05547b33a3b7b112eda6346f42ea93ee06b74d30c4c47dfeca0d5f2

Status: Downloaded newer image for mongo:latest

docker.io/library/mongo:latest

Now you could see “mongo” image by running the below command.

docker images


Step 2:

If you able to view your “Mongo Db” image, then, we can run the “Mongo Db” image inside the Docker container,

Use the below command to deploy the image inside Docker.

docker run --name mongoapi -d -p 27017:27017 mongo

To verify, whether “Mongo Db” image, properly deployed inside the Docker, use the below command.

Docker ps

Above command will give you the below result, if deployment went well.

CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                      NAMES

8953411f5ddc        mongo                             "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:27017->27017/tcp   mongoapi

Use the below command to view the “Mongo Db” container deployment details.

docker inspect <<container id>>


Step 3:

Below are the different ways to work with Mongo Db Container.

1.       Using “Bash”

2.       Through Applications (i.e. Using “.Net Application” and “Mongo Db” Driver. We will see it in another Article)

We will see how to work with “Mongo” Db through Bash,

1.        Using “Bash” command

docker exec -it mongoapi bash

Execute the above command to enter into container. You will be getting the below result

root@8953411f5ddc:/#

Enter “mongo” to connect to Mongo Shell.

mongo (Example: root@8953411f5ddc:/# mongo)

1.       Below command will show the list of DBs present in the Docker Container

  Show dbs

2.       To Create (or) use Database, execute the below command. This will create database if it is not exists inside the docker container.

  use mymongodb

3.       To create a table and insert new values, execute the below command.

Input:

db.people.save({ firstname: "Tom", lastname: "This is Last Name” })

      Output:

        WriteResult({ "nInserted" : 1 })

4.       To find value inside a table

    Input:

    db.people.find({ firstname: "Tom" })

           Output:

                { "_id" : ObjectId("5e04db1744f1917378629f71"), "firstname" : "Tom", "lastname" : " This is Last Name " }


That’s it. In this Article, we have learnt,

1.       To “Pull” Mongo Db Image from Docker Hub.

2.       To deploy “Mongo” Image inside the Docker Container.

3.       To create “Mongo” Db and Tables by inserting user details.

 

 

About Author
Raj Kumar
Total Posts 57
Developer in .Net!
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
Comments not posted yet!! Please post your comments on this article.
  Privacy   Terms Of Use   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.