How to setup MongoDB using docker in Amazon Linux 2023 (Fedora)

You are currently viewing How to setup MongoDB using docker in Amazon Linux 2023 (Fedora)

MongoDB is a popular NoSQL database that is used to store data in a flexible and scalable way. Docker is a containerization platform that allows you to package and deploy applications in a consistent way. By using Docker, you can easily set up MongoDB on Amazon Linux 2023 (Fedora).

Prerequisites

Before you begin, you will need the following:

  • An Amazon Linux 2023 (Fedora) instance
  • Docker installed

Installing Docker

If you don’t already have Docker installed, you can install it using the following command:

sudo dnf update
sudo dnf install docker

Once Docker is installed, start the Docker service using the following command:

sudo systemctl start docker

You can also enable Docker to start automatically on system boot:

sudo systemctl enable docker

Next, pull the MongoDB Docker image from the official Docker registry. Run the following command:

sudo docker pull mongo

This will download the latest MongoDB image to your machine.

To store MongoDB data and configuration files persistently, we need to create a directory on the host machine. Run the following command to create the directory:

mkdir ~/mongodb_data

This will create a directory named “mongodb_data” in your home directory.

With the MongoDB image downloaded and the data directory in place, we can now start a MongoDB container. Execute the following command:

sudo docker run -d -p 27017:27017 -v ~/mongodb_data:/data/db --name mongodb mongo

This command runs the MongoDB container in the background with port mapping and data volume configurations. The container will be named “MongoDB”.

To ensure that the MongoDB container is up and running, run the following command:

sudo docker ps

You should see the MongoDB container listed along with its details, indicating that it’s running successfully.

Open an interactive container instance of mongo and connect to the deployment with mongosh.

docker exec -it mongodb mongosh

To confirm your MongoDB instance is running, run the Hello command:

db.runCommand(
   {
      hello: 1
   }
)

Conclusion

Congratulations! You have successfully set up MongoDB using Docker on Amazon Linux 2023 (Fedora). Docker provides a convenient way to deploy and manage MongoDB instances, allowing you to focus on your application development without worrying about infrastructure setup. You can now connect to MongoDB using the hostname or IP address of your machine on port 27017 and start building your applications with this powerful NoSQL database.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply