How to Install Docker MongoDB Container Using MongoDB Image. The marvels made possible through cloud technology have brought a quiet revolution in many different fields. Technologies like driverless cars, IoT, and advanced AI are only a few examples of what cloud technology has given us. With improvements coming in every year, there is no limit to what kinds of futuristic possibilities cloud technology can unlock.
The software development industry, in particular, has witnessed significant advancements thanks to the cloud. Apart from enabling radical methodologies like DevOps and the different cloud computing types can now also make virtual machines a thing of the past by supporting container technology. Enterprises have utilized this feature to develop applications in a much more supportive environment. You can also use containers to set up powerful database configurations, like for MongoDB, quickly.
This article goes briefly through container technology and discusses one of the most popular container options currently out there called Docker. It also gives a glimpse of what MongoDB is like in the cloud and talks about setting a MongoDB container up using MongoDB image.
Taking A Closer Look At Containers & Docker
The main goal that led to the birth of container technology was efficient compartmentalization of a computer’s resources and internal system. Its roots lie in an early version of the Unix system, in which an application could only access particular directories and sub-directories. Further on, developments led to the formation of control group technology that could also control specific limits of the CPU and memory.
Modern-day container technology has become enormously efficient, leading it to gain global popularity. Containers offer a way in which applications are separate from the environments in which they run. Container-based applications can run consistently and be quickly deployed, regardless of whether they run on a developer’s laptop or a data server. Compared with virtual machines, their predecessors, containers are lighter, virtualize more efficiently, and use much less memory.
Docker is the most popular container technology used today. Released in 2013 as Docker Engine, it utilized the existing container concepts mentioned above and focuses on enabling developers to handle application dependencies separately from the resources. Popular open-source serverless frameworks around the globe use Docker, as well as major cloud vendors. Docker containers are industry-standard and significantly secure due to isolation capabilities.
Thanks to containers, developers no longer have to worry about dependencies or environments when developing. While containers presently enjoy more popularity than virtual machines, many configurations suggest running both together can provide great flexibility in application deployment and management. There are many open-source initiatives that aim to develop further and strengthen container technology and its ecosystem.
MongoDB in The Cloud
MongoDB is a popular database technology used frequently for all kinds of development and data storage needs. It is ‘NoSQL’ in nature and stores data in separate instances called ‘documents.’ The data is stored in JSON-like format and can be entirely flexible thanks to the optional inclusion of a rigid schema. Its document model enables the data stored to be directly accessed within code as objects.
MongoDB integrates with cloud technology very quickly and intuitively. It offers cloud applications like the MongoDB Atlas, a cloud database that can connect to leading cloud services like AWS, Azure, and GCP. Setting up a MongoDB server on any of the cloud services, for example, an AWS MongoDB setup is pretty easy to follow. The Atlas Data Lake is also a scalable data storage option and integrates directly into the main Atlas application.
MongoDB has often been one of the most preferred database services in the world of cloud development and services. It provides powerful analytic capabilities and supports easy horizontal scaling if a deployment needs to expand. MongoDB also accounts for disaster scenarios and provides smooth recovery options. Its data format enables rich data storage that helps fuel powerful predictive analytics.
As data needs grow more expansive day by day, most cloud-backed technologies rely on MongoDB for their storage and analytic needs. Its different pricing structure supports all kinds of uses for individuals, small-time businesses, or giant enterprises. Major cloud vendors like IBM Cloud provide MongoDB database management as a service, thanks to its high availability and security.
Setting Up A MongoDB Container On Docker Using MongoDB Image
As MongoDB is a popular choice, most applications being developed within some container will require configuring and managing a MongoDB container. Thanks to their accessible nature, setting up a MongoDB container within Docker is relatively straightforward. The instructions are for setting up Docker and MongoDB within a Ubuntu environment.
Discussed below are the primary processes and the different dependencies and prerequisites.
Install Docker & Docker Compose
- Update existing packages using apt update.
sudo apt update && sudo apt upgrade -y
- Install the necessary prerequisite files that install the necessary CA certificates.
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- Add the GPG key from Docker’s official repository on GitHub.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Now, we will download the official Docker repository to the apt sources so that we can download packages from the repository using the apt command.
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Update the package list.
sudo apt update
- Install Docker’s free community edition.
sudo apt install docker-ce
- The following command can be used to verify Docker’s active and running status.
sudo systemctl status docker
With Docker installed, it is time to install Docker Compose that is going to help us build our MongoDB container. It can also integrate multiple containers into one application.
- We will download and install the current stable release of Docker Compose.
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- The download binary’s permissions need to be altered so that we can execute them.
sudo chmod +x /usr/local/bin/docker-compose
- The following command can be used to verify successful installation of Docker Compose.
Configuring A MongoDB Container
Now that we have Docker and Docker Compose up, we can start building out a MongoDB container. The official image for building one is available from Docker’s official marketplace called Docker Hub. As Docker is a part of our apt package installer’s source list, we can directly refer to mongo and install it.
- We will firstly make a directory to hold the MongoDB docker-compose file. The -pv helps make multiple directories and subdirectories.
mkdir -pv mongodb/database
The following command shows a search result for MongoDB’s image.
sudo docker search mongodb
- Inside the mongodb directory, we write the following docker-compose file and name it ‘docker-compose.yml’.
version: "3.8" services: mongodb: image : mongo container_name: mongodb environment: - PUID=1000 - PGID=1000 volumes: - /home/barry/mongodb/database:/data/db ports: - 28000:28000 restart: unless-stopped
The image is directly referenced as mongo, and the container is named mongodb, just like the directory. The local port is mapped to an internal port, and the database is mapped to the sub-directory of the same name. The container’s restart policy is set to restart unless stopped by the user.
- We run the file described above using the following Docker Compose command.
sudo docker-compose up -d
- The following command shows all containers running currently and should show our mongodb container as one of them.
sudo docker ps -a
MongoDB Container is now up and running
MongoDB is a robust database technology that is supported and widely used by developers and cloud services globally. By integrating it in a Docker container, the application in development can utilize its powerful database features and provide a smooth user experience. The lightweight and manageable nature of Docker containers make it easy to setup and start using a MongoDB container right away.