How To Run MongoDB In A Docker Container

How To Run MongoDB In A Docker Container

MongoDB is a document-based NoSQL database that is good for high volume data storage. Rather than using tables and rows like traditional relational databases, MongoDB makes use of Collections and Documents. The documents consist of key-value pairs which are the basic unit of data in MongoDB. In this tutorial, we’ll use Docker to launch a container running MongoDB. Once it is up and running in a container, we can connect to it in a shell and test out some of the common Mongo commands.

Start MongoDB In Docker

With Docker installed and running, spinning up a Mongo container is done using the command here.

vegibit@ThinkPad MINGW64 ~
$ docker run --name mongo -d mongo
Unable to find image 'mongo:latest' locally
latest: Pulling from library/mongo
171857c49d0f: Pulling fs layer
419640447d26: Pulling fs layer
61e52f862619: Pulling fs layer
892787ca4521: Pulling fs layer
06e2d54757a5: Pulling fs layer
e2f7d90822f3: Pulling fs layer
f518d3776320: Pulling fs layer
feb8e9d469d8: Pulling fs layer
69705b632494: Pulling fs layer
c7daea26376d: Pulling fs layer
13d1f9e1fc77: Pulling fs layer
f87e65fe7ffd: Pulling fs layer
feb8e9d469d8: Waiting
e2f7d90822f3: Waiting
c7daea26376d: Waiting
13d1f9e1fc77: Waiting
69705b632494: Waiting
f518d3776320: Waiting
892787ca4521: Waiting
f87e65fe7ffd: Waiting
06e2d54757a5: Waiting
419640447d26: Verifying Checksum
419640447d26: Download complete
61e52f862619: Verifying Checksum
61e52f862619: Download complete
892787ca4521: Verifying Checksum
892787ca4521: Download complete
e2f7d90822f3: Verifying Checksum
e2f7d90822f3: Download complete
06e2d54757a5: Verifying Checksum
06e2d54757a5: Download complete
171857c49d0f: Download complete
f518d3776320: Verifying Checksum
f518d3776320: Download complete
171857c49d0f: Pull complete
419640447d26: Pull complete
61e52f862619: Pull complete
892787ca4521: Pull complete
06e2d54757a5: Pull complete
e2f7d90822f3: Pull complete
f518d3776320: Pull complete
69705b632494: Verifying Checksum
69705b632494: Download complete
feb8e9d469d8: Verifying Checksum
feb8e9d469d8: Download complete
feb8e9d469d8: Pull complete
69705b632494: Pull complete
13d1f9e1fc77: Verifying Checksum
13d1f9e1fc77: Download complete
f87e65fe7ffd: Verifying Checksum
f87e65fe7ffd: Download complete
c7daea26376d: Verifying Checksum
c7daea26376d: Download complete
c7daea26376d: Pull complete
13d1f9e1fc77: Pull complete
f87e65fe7ffd: Pull complete
Digest: sha256:a4448eb5f6e6097353d0ab97eb50aeb0238bb4e60c37e401920d3c2c4fc73eb9
Status: Downloaded newer image for mongo:latest

What Happened When We Ran docker run –name mongo -d mongo

One we ran the command above, we saw quite a bit of output in the terminal. So what exactly is happening here? These steps give a brief summary.

  1. Docker looks for the mongo image locally
  2. Docker does not find the mongo image locally so it next checks
  3. The image is found and downloads each layer
  4. A new container is created based on the mongo image
  5. The container runs and can be seen via cli or Docker Desktop

We can see our new Mongo container running with the docker container ls command.

vegibit@ThinkPad MINGW64 ~
$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
08190185820c        mongo               "docker-entrypoint.s…"   7 minutes ago       Up 7 minutes        27017/tcp           mongo

With docker container top mongo we can see the process running. A container is just a process that is limited to what resources it can access. They are not VMs.

vegibit@ThinkPad MINGW64 ~
$ docker container top mongo
PID                 USER                TIME                COMMAND
986                 999                 0:05                mongod --bind_ip_all

Using Docker Desktop is also a great way to be able to view and manage any containers on the system.
mongo container running in docker

Docker Connect To Container cli

The docker exec command runs a new command in a running container. We can use this to run the ps aux command inside the container to see the running processes.

vegibit@ThinkPad MINGW64 ~
$ docker exec mongo ps aux
mongodb      1  1.1  2.1 1584916 136412 ?      Ssl  15:20   1:00 mongod --bind_ip_all
root        76  0.0  0.0  34404  2804 ?        Rs   16:50   0:00 ps aux

Stopping The Mongo Container

vegibit@ThinkPad MINGW64 ~
$ docker stop mongo

vegibit@ThinkPad MINGW64 ~
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

vegibit@ThinkPad MINGW64 ~
$ docker top mongo
Error response from daemon: Container 08190185820c9b1d513375f9ff011e4042fae95487fd3bcbc3e9c18b5048dc99 is not running

vegibit@ThinkPad MINGW64 ~
$ docker exec mongo ps aux
Error response from daemon: Container 08190185820c9b1d513375f9ff011e4042fae95487fd3bcbc3e9c18b5048dc99 is not running

Restart The Mongo Container

vegibit@ThinkPad MINGW64 ~
$ docker start mongo

vegibit@ThinkPad MINGW64 ~
$ docker container list
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
08190185820c        mongo               "docker-entrypoint.s…"   2 hours ago         Up 19 seconds       27017/tcp           mongo

vegibit@ThinkPad MINGW64 ~
$ docker mongo top
docker: 'mongo' is not a docker command.
See 'docker --help'

vegibit@ThinkPad MINGW64 ~
$ docker top mongo
PID                 USER                TIME                COMMAND
1253                999                 0:01                mongod --bind_ip_all

Connect To The Shell Of Mongo Container

Ok great. We have a MongoDB container running, but how can we interact with it? To do this we can use the docker exec command. There are a few parts to the command so let’s review what each part does.

  1. docker exec runs a new command in a running container
  2. -it -i = # stdin, -t = # interactive
  3. mongo the name of the container to run the command in
  4. /bin/sh the command to run, in this case launch the shell
docker exec -it mongo /bin/sh

The hashtag you see is the new prompt inside the container. From there we can launch the mongo interactive shell:

# mongo
MongoDB shell version v4.4.1
connecting to: mongodb://
Implicit session: session { "id" : UUID("d882e577-b5a5-438d-8a35-4a2e0002bf50") }
MongoDB server version: 4.4.1
The server generated these startup warnings when booting:
        2020-09-30T17:50:54.021+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See
        2020-09-30T17:50:54.720+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2020-09-30T17:50:54.720+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

We can try some various Mongo commands in the container to see it working.

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use myNewDataBase
switched to db myNewDataBase
> db
> db.dropDatabase()
{ "ok" : 1 }
> db.createCollection("Container");
{ "ok" : 1 }
> show collections
> db.Container.drop()
> show collections
> db.createCollection("Computer")
{ "ok" : 1 }
> db.Computer.insert( { _id: 1, item: "Macbook", qty: 1 } )
WriteResult({ "nInserted" : 1 })
> show collections
> db.Computer.count()
> db.Computer.find()
{ "_id" : 1, "item" : "Macbook", "qty" : 1 }

Cleaning Up

Well that was fun. Let’s clean up our docker environment now by stopping the container, removing it, and listing all containers to make sure its gone.

$  docker container stop mongo
$  docker container rm mongo
$  docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Learn More


The takeaway is that once we have Docker set up and running, it is fairly easy to launch any type of container you like. In this case, we used MongoDB. It could just as easily have been MySQL, Oracle, Java, Postgres, Redis, Ubuntu, or whatever we like. Another thing to remember is that a container is just a process, it is not a virtual machine. Containers are much more lightweight than a full virtual machine and are easy to spin up, test out, and destroy very quickly.