Docker Mongodb Setup
- Caution: Changing the cgroup driver of a Node that has joined a cluster is strongly not recommended. If the kubelet has created Pods using the semantics of one cgroup driver, changing the container runtime to another cgroup driver can cause errors when trying to re-create the Pod sandbox for such existing Pods.
- Once Docker is running, you can confirm that everything is working by opening a new terminal window and typing the command: docker -version # Docker version 18.09.2, build 6247962 Install the extension. The Remote - Containers extension lets you run Visual Studio Code inside a Docker container. Install the Remote - Containers.
- Docker daemon: This is also called Docker Engine, it is a background process which runs on the host system responsible for building and running of containers. Docker Client: This is a command line tool used by the user to interact with the Docker daemon. Docker Image: An image is an immutable file that’s essentially a snapshot of a container.
Before running the docker commands, i assume that you have docker installed on your machine, if not, you can install and complete the setup first. MongoDB as Docker Container. To run mongoDB in docker, you need the mongodb image in your local docker daemon. You can get the image using the command.
- Docker Tutorial
- Docker Useful Resources
- Selected Reading
To start the installation of Docker, we are going to use an Ubuntu instance. You can use Oracle Virtual Box to setup a virtual Linux instance, in case you don’t have it already.
The following screenshot shows a simple Ubuntu server which has been installed on Oracle Virtual Box. There is an OS user named demo which has been defined on the system having entire root access to the sever.
To install Docker, we need to follow the steps given below.
Step 1 − Before installing Docker, you first have to ensure that you have the right Linux kernel version running. Docker is only designed to run on Linux kernel version 3.8 and higher. We can do this by running the following command.
uname

This method returns the system information about the Linux system.
Syntax
Options
a − This is used to ensure that the system information is returned.
Return Value
This method returns the following information on the Linux system −
- kernel name
- node name
- kernel release
- kernel version
- machine
- processor
- hardware platform
- operating system
Example
Output
When we run above command, we will get the following result −
From the output, we can see that the Linux kernel version is 4.2.0-27 which is higher than version 3.8, so we are good to go.
Step 2 − You need to update the OS with the latest packages, which can be done via the following command −
This method installs packages from the Internet on to the Linux system.
Syntax

sudo apt-get update
Options
sudo − The sudo command is used to ensure that the command runs with root access.
update − The update option is used ensure that all packages are updated on the Linux system.
Return Value
None
Example
Output
When we run the above command, we will get the following result −
This command will connect to the internet and download the latest system packages for Ubuntu.
Step 3 − The next step is to install the necessary certificates that will be required to work with the Docker site later on to download the necessary Docker packages. It can be done with the following command.
Step 4 − The next step is to add the new GPG key. This key is required to ensure that all data is encrypted when downloading the necessary packages for Docker.
The following command will download the key with the ID 58118E89F3A912897C070ADBF76221572C52609D from the keyserver hkp://ha.pool.sks-keyservers.net:80 and adds it to the adv keychain. Please note that this particular key is required to download the necessary Docker packages.
Docker Mongodb Configuration
Step 5 − Next, depending on the version of Ubuntu you have, you will need to add the relevant site to the docker.list for the apt package manager, so that it will be able to detect the Docker packages from the Docker site and download them accordingly.
Precise 12.04 (LTS) ─ deb https://apt.dockerproject.org/repoubuntu-precise main
Trusty 14.04 (LTS) ─ deb https://apt.dockerproject.org/repo/ ubuntu-trusty main
Wily 15.10 ─ deb https://apt.dockerproject.org/repo ubuntu-wily main
Xenial 16.04 (LTS) - https://apt.dockerproject.org/repo ubuntu-xenial main
Since our OS is Ubuntu 14.04, we will use the Repository name as “deb https://apt.dockerproject.org/repoubuntu-trusty main”.
And then, we will need to add this repository to the docker.list as mentioned above.
Step 6 − Next, we issue the apt-get update command to update the packages on the Ubuntu system.
Step 7 − If you want to verify that the package manager is pointing to the right repository, you can do it by issuing the apt-cache command.
In the output, you will get the link to https://apt.dockerproject.org/repo/
Step 8 − Issue the apt-get update command to ensure all the packages on the local system are up to date.
Step 9 − For Ubuntu Trusty, Wily, and Xenial, we have to install the linux-image-extra-* kernel packages, which allows one to use the aufs storage driver. This driver is used by the newer versions of Docker.
It can be done by using the following command.
Step 10 − The final step is to install Docker and we can do this with the following command −
Here, apt-get uses the install option to download the Docker-engine image from the Docker website and get Docker installed.
The Docker-engine is the official package from the Docker Corporation for Ubuntu-based systems.
In the next section, we will see how to check for the version of Docker that was installed.
Docker Version
To see the version of Docker running, you can issue the following command −
Syntax
Options
version − It is used to ensure the Docker command returns the Docker version installed.
Return Value
The output will provide the various details of the Docker version installed on the system.
Example
Docker Mongodb Install
Output
When we run the above program, we will get the following result −
Docker Info
To see more information on the Docker running on the system, you can issue the following command −
Syntax
Options
info − It is used to ensure that the Docker command returns the detailed information on the Docker service installed.
Return Value
The output will provide the various details of the Docker installed on the system such as −
- Number of containers
- Number of images
- The storage driver used by Docker
- The root directory used by Docker
- The execution driver used by Docker
Example
Output
When we run the above command, we will get the following result −
Docker for Windows
Docker has out-of-the-box support for Windows, but you need to have the following configuration in order to install Docker for Windows.
System Requirements
Windows OS | Windows 10 64 bit |
Memory | 2 GB RAM (recommended) |
You can download Docker for Windows from − https://docs.docker.com/docker-for-windows/
Docker ToolBox
Docker ToolBox has been designed for older versions of Windows, such as Windows 8.1 and Windows 7. You need to have the following configuration in order to install Docker for Windows.
System Requirements
Windows OS | Windows 7 , 8, 8.1 |
Memory | 2 GB RAM (recommended) |
Virtualization | This should be enabled. |
You can download Docker ToolBox from − https://www.docker.com/products/docker-toolbox
In this article, we will see how to run mongodb as a docker container in development.
As a developer, we all know how frustrating it is to setup a database and start it in development environment everyday.
Well, i have also been in the situation unless docker came into play.
As we all know, Docker solves the problem of 'it is working on my machine' problem.
Let's see how to setup mongodb once and run it without any problem in development environment.
If you are completely new to the concepts of docker, i recommend you to read this article series to get better idea.
If you are already familiar with docker, but want to learn how to setup docker and nodejs. read this article to learn about it.
Docker Setup
Before running the docker commands, i assume that you have docker installed on your machine, if not, you can install and complete the setup first.
MongoDB as Docker Container
To run mongoDB in docker, you need the mongodb image in your local docker daemon. you can get the image using the command,
the above command will pull the mongodb image from docker registry.
Once, you pull the mongoDB image from docker image registry, you can run the image with a single command.
that's it..
really..... you can now start using mongoDB in your application.

So simple..right?.
Now, we will try to understand what is going on under the hood and what does it really do
Here, i have separated the command into four parts to understand it in a better way.
Part1
Here, we are running the docker container with a detached mode. there are two mode to run docker container. one is detached and another one is interactive mode.
detached mode will run in background whereas interactive mode runs in the foreground(terminal will be active).
Problem with interactive mode is if you close the terminal, container will stop running.
Part 2
Here, -p represents the port of the container. you can map the port of the container and your machine port to communicate with the container.
As MongoDB always run in port 27017, we map the ports here.
Part 3

After that, we added flag --name which represents the docker container name. you can specify the name you want to have it for your mongoDB container.
Part 4
Lastly, we mount the volume of docker container and local machine volume. This is one of the important commands in the whole part.
-v /data/db:/data/db mount the volume of docker container db directory and local machine db directory.
so that, you won't lose your database data even after restarting the container.
Summary
To sum up, this simple step on running the mongoDB local development on docker can save you lot of time on development. i recommend you to use this on your development process.
Want to stand out from the Crowd?
No spam, ever. Unsubscribe anytime.
To Read More
Modern React Redux Toolkit - Login ...
User Authentication is one of the common workflow in web applications. In this t...
Building Nodejs Microservice - A Cl...
This Article explains everything about how to build Nodejs Microservices in clou...
I Accidentally wiped the entire dat...
One of the tragic accident in my job turned out to be good learning for me in re...
