The Docker stack is a popular deployment tool that helps developers and engineers manage their software with ease, but it can be difficult to configure. This blog post will walk you through deploying and managing your own docker container using the mongoDB software as an example.
The “docker-compose mongodb” is a tool that helps to manage MongoDB containers. This tool can be used to deploy and manage Docker containers with the MongoDB database.
MongoDB is a well-known open-source document database with excellent speed and versatility. But, for your projects, have you tried containerizing a MongoDB database? If not, you’re in for a surprise!
In this lesson, you’ll learn how to use Docker to deploy, protect, and manage MongoDB. So keep reading to learn how to install Docker MongoDB containers on your own!
Prerequisites
This seminar includes practical examples. Make sure you have the following items to follow along:
- An Ubuntu computer – Ubuntu 20.04 LTS is used in this lesson.
Related: [Step-by-Step] How to Install Ubuntu 20.04
- A user who has sudo access
- Docker 20.10.9 is used in this tutorial.
Installation and Use of Docker on Ubuntu (In the Real World)
The Docker MongoDB Image is being downloaded.
You must first download a MongoDB image from the Docker Hub in order to run the MongoDB container locally before you can deploy and manage a Docker MongoDB container.
Download the MongoDB Community Edition(CE) image (pull mongo) from Docker Hub using the docker command below. At the time of writing, this picture was in version 4.2.
The program gets the most recent version of the picture and marks it as latest, as seen below.
Obtaining a mongo image
Run the docker images command to get a list of all the images on your server.
The MongoDB picture labeled as newest is shown below.
All Docker images are listed here.
Docker MongoDB Container Deployment
You may now deploy a MongoDB container using the MongoDB image you obtained. You’ll use the mongo program to start your MongoDB container and build a data directory. The mapping between the Docker container’s data region and the host computer is then defined.
1. Create a data directory called mongodata to hold the MongoDB database and logs using the mkdir command.
2. After that, use the docker run command to create a container named mymongo. In the case that output should be presented while starting the container, it is also launched with an interactive pseudo-TTY (-it).
Finally, the internal directory /data/db is tied (-v) to the newly constructed directory mongodata. The -d option launches the container in detached mode, which means it operates in the background and delivers the console output.
When you execute the container, the system generates the /data/db directory automatically to keep data about the modifications you make. This directory operates in a read-only mode and allows for permanent data storage on the host system.
—name mongodata:/data/db sudo docker run -it mongo mymongo -d
You’ll see something like this after the command finishes correctly.
MongoDB Container Deployment
3. Finally, use the docker ps command to determine the container’s ID and verify its status. The docker ps tool shows all information about the container that is presently operating.
The docker ps command is similar to the standard Linux ps command.
The Ports part of the report given below shows all of the ports allocated to the container for listening to incoming connections.
Port 27017 is assigned to the host in this case. The result below shows that the MongoDB instance on this container may be accessed from the host via localhost:27017.
Checking the Status of Docker MongoDB Containers
You could want to look at the mymongo container’s log file to see what occurred to your mongo database/instance when anything went wrong. If that’s the case, use the docker logs command as follows: docker logs docker-container sudo docker Docker-container should be replaced with the name of your Docker container.
Docker Container Log File Reading
Docker MongoDB Container Attachment to Bash Shell
How do you maintain a Docker MongoDB container that you’ve recently deployed? To do so, first use the docker exec command to connect the Docker container to the Bash shell.
Because your container is presently operating in detached mode, it’s critical to connect it to the Bash shell (running in the background). If the container is running in the background, it will not accept any input or show any output.
To connect your container (mymongo) to the Bash shell, use the docker exec command.
docker exec -it mymongo bash sudo
When the command is finished, your prompt will look something like this. The container ID (77782fa95314) is a unique alphanumeric number.
Container IDs are critical for avoiding name conflicts and identifying containers across hosts, therefore don’t modify them.
Docker MongoDB Container Attachment to Bash Shell
To log into the MongoDB shell in the container, execute the mongo command without any parameters (mymongo). You perform your mongo queries/commands in the MongoDB shell.
Because you are now a root user within the container, you may execute any command without the sudo prefix once inside. Your host system will be unaffected by any changes you make in the container.
You can determine you’ve entered the MongoDB shell by glancing at the prompt below.
Accessing the MongoDB shell
Run the help command in the MongoDB shell to view all possible commands, as shown below.
The Commands Available in MongoDB Shell
Creating an Administrative MongoDB User
You’ll now create an administrator MongoDB user once you’ve installed a MongoDB server within a Docker container. You can connect to the MongoDB server and manage databases as an administrative user.
1. Return to the MongoDB shell and execute the use command to switch to the admin database. The administrator user now has the necessary rights to handle the databases.
Changing to the Administration Database
To establish an administrator user, copy and paste the following code into the MongoDB prompt.
The db.createUser() function is used to create an administrator user in the code below. The username (user) and password (pwd) shown below are hardcoded and may be changed to suit your needs.
# Make a database for administrative users. createUser(# Sets the administrative user’s username: “ata”, # Sets the administrative user’s password: “password123”, # Sets the administrative user’s roles: [ role: “userAdminAnyDatabase”, db: “admin”)
If the code is correct, you should see something like this in your MongoDB prompt.
Adding a New Administrative User
3. To leave the MongoDB shell, use the quit() query.
4. Finally, connect to the MongoDB server using the mongo command to test the administrator account you established (ata). When asked, provide the administrator user’s password.
—authenticationDatabase admin mongo -u ata -p
If you connect to the MongoDB server successfully, it prints out the MongoDB server version, as seen below.
Getting in Touch with the MongoDB Server
You may wish to run some more tests on the administrator user. If that’s the case, use the instructions below to display all of the database’s users.
The administrative user you established appears in the list below.
Users in the Database List
Getting Started with MongoDB
It would be impossible to manage a Docker MongoDB container without first building a database. You’ll use the MongoDB shell to establish a new MongoDB database into which you’ll store data.
1. To list all databases on your server, execute the show dbs command from inside an interactive session within the Docker container.
An admin database, a config database, and a local database are shown below. In every MongoDB server, the MongoDB shell generates these databases by default.
All MongoDB databases are listed here.
2. Next, run the use command below to create a new database. Replace <database_name> with the database name of your choice. But for this example, the database is named linux.
If the specified database exists, the use query changes the current database to it. If not, the use query builds a new database and switches to it automatically.
Creating a MongoDB Database from Scratch
3. Rerun the show dbs command (step one) to verify whether the database you established is still there.
Is the freshly constructed database (linux) still missing from the list? MongoDB only creates the database the first time you store data in it. The information might come in the form of a collection or a paper.
All MongoDB databases are listed here.
4. Paste the following code into the MongoDB shell and hit Enter.
The following code generates a new collection called linux version, which you may rename to anything you choose. The data in the collection is organized in key-value pairs.
# Inserting data with the insertOne method db.linux version.insertOne(# Key:Value pairs to insert to the database “debian”: “11”, “ubuntu”: “20.04”, “rocky linux”: “8.4”, “alma linux”: “8” cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval cheval
The linux version collection is formed and has an ObjectID, as seen in the output below.
displaying the collection’s ID
5. Use the show collections command to examine a list of collections and check whether linux version is one of them.
You can see that the linux version collection was successfully formed in the screenshot below.
Displaying the Collections List
6. Finally, execute the following command to verify that the data you entered in the linux version collection is valid. The pretty() function displays data in a human-friendly way.
db.linux version.find().pretty()
Use the Update() function to make changes to the database data.
The result shown below is in a much more understandable manner.
Data from the MongoDB Database
The Docker MongoDB Container is being restarted.
You should now have a Docker MongoDB container up and running. But what if you no longer need the container if it is broken? A MongoDB Docker container may be stopped, restarted, and even removed with a few Docker commands.
To list all running containers, use the docker ps command.
As shown below, write down the name and container ID of the container you want to stop, restart, or delete.
All running Docker containers are shown.
2. To stop a running MongoDB container, use one of the docker stop instructions listed below.
docker stop mymongo sudo docker stop container-ID sudo
3. Check whether the container has shut down by running the docker ps command again (step one).
The container is no longer included in the list of operating containers, indicating that it has been properly terminated.
Containers are listed alphabetically.
4. If you want to restart a container, use one of the docker start commands listed below.
docker start mymongo sudo docker start container-ID sudo
5. Finally, check whether the container is operating using the docker ps command.
Perhaps you no longer need a container. If this is the case, you must first stop the container and use the rm command to delete it, such as sudo docker container rm mongodb. Replace mongodb with the container name or container ID, as in the previous examples.
Conclusion
You learnt how to deploy and manage a Docker MongoDB container in this tutorial by establishing an administrator account and constructing a database and storing data in it. You’ve recognized that you can clean up your Docker images by stopping, restarting, and removing containers you don’t need.
Docker Image Prune Can Help You Manage Your Docker Images
Using Docker to deploy and manage MongoDB is the first step toward taking use of containerization technologies and lowering overhead. Why not take this initial step to learn more about MongoDB and how a Docker MongoDB container might assist you with your projects?
The “docker mongodb tutorial” is a comprehensive guide that will walk you through the process of deploying and managing a MongoDB container. It also includes instructions for setting up the MongoDB data directory, configuring authentication, and other steps.
Related Tags
- docker run mongodb
- deploy mongodb on docker
- how to check mongodb version in docker
- docker-compose mongodb environment variables
- mongodb docker-compose example