Running MongoDB in a Docker Container on CentOS, AlmaLinux, and RockyLinux Print

  • 0

Whether you're setting up a development environment, trying out MongoDB, or even deploying it into a production setting, running MongoDB in a Docker container can be an efficient and clean way to get started. Docker abstracts away many of the complexities of setting up a MongoDB server and makes it easier to manage, scale, and replicate your MongoDB database.

Here is a detailed guide on how to run MongoDB in a Docker container on CentOS, AlmaLinux, and RockyLinux, even if Docker isn't already installed on your system.

Pre-requisites

Before we get started, ensure your system meets the following requirements:

- Operating System: CentOS, AlmaLinux, or RockyLinux
- User privileges: A user with `sudo` privileges
- Network: An active internet connection

 Step 1: Install Docker

If Docker isn't already installed on your system, you'll need to install it. Docker is a platform that allows developers to develop, deploy, and run applications easily using containerization.

Here's how you can install Docker:

1. Update your system's package database:


sudo yum update -y

2. Install the necessary packages for Docker installation:


sudo yum install -y yum-utils

3. Add the Docker CE (Community Edition) repository to your system:


sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

4. Install Docker CE:


sudo yum install -y docker-ce docker-ce-cli containerd.io

5. Start the Docker service and enable it to start on boot:


sudo systemctl start docker
sudo systemctl enable docker

6. Verify the installation:


sudo docker --version

This command will print out the Docker version if the installation was successful.

Step 2: Pull the MongoDB Docker Image

With Docker installed, you can now pull the MongoDB Docker image from Docker Hub:


sudo docker pull mongo

This command will download the latest MongoDB Docker image to your system.

Step 3: Run MongoDB in a Docker Container

With the MongoDB image now available on your system, you can start a Docker container running MongoDB:


sudo docker run --name mongodb -d -p 27017:27017 mongo

This command does the following:

- `--name mongodb`: Names the Docker container "mongodb"
- `-d`: Runs the container in detached mode, which means the container runs in the background
- `-p 27017:27017`: Maps port 27017 inside the Docker container to port 27017 on the host machine, allowing you to connect to MongoDB
- `mongo`: Specifies the Docker image to use (in this case, the MongoDB image you just pulled)

After running this command, MongoDB will be accessible on your machine on port 27017, which is the default MongoDB port.

Step 4: Verify the MongoDB Server

You can use the `docker ps` command to check if the MongoDB server is running:


sudo docker ps

This command will show you a list of all running Docker containers. If MongoDB is running correctly, you should see the "mongodb" container in this list.

Additionally, you can check the logs of the MongoDB server:


sudo docker logs mongodb

If MongoDB is running correctly, you should see standard MongoDB startup logs.

Conclusion

This guide has shown you how to install Docker and run MongoDB in a Docker container on CentOS, AlmaLinux, or RockyLinux. With MongoDB running in a Docker container, you can now start building your applications with MongoDB as your database.

 

Alternative Articles

A Step-By-Step Guide to Installing MongoDB

To uninstall the current MongoDB container and pull a MongoDB 4.4 Docker image, you need to follow these steps:

1. First, you need to stop the existing MongoDB Docker container by running the following command:

sudo docker stop mongodb

2. Then, you have to remove the stopped container:

sudo docker rm mongodb

3. Now, you can pull the MongoDB 4.4 image from Docker Hub:

sudo docker pull mongo:4.4

4. Finally, run the MongoDB 4.4 Docker container with the following command:

sudo docker run --name mongodb -d -p 27017:27017 mongo:4.4

Now, MongoDB 4.4 will be running on your machine on port 27017, the default MongoDB port.

Please note that running MongoDB in a Docker container has its own set of complexities. It might not be suitable for production environments without appropriate data persistence and backup strategies. Always ensure your data is safe and secure when using Docker for database services.

 


Was this answer helpful?

« Back