In this tutorial we learn how we install MongoDB using Docker on AlmaLinux 10 with persistent storage, secure configuration, and easy connection setup.
In today’s fast-paced hosting and development landscape, containerizing databases has become a go-to strategy for performance, scalability, and portability. In this guide, we’ll walk through how we install MongoDB inside Docker on AlmaLinux 10, the latest and stable RHEL-based Linux distribution. Whether we’re building a scalable app backend or managing data across containers, this approach fits cleanly into DevOps workflows and secure infrastructure.
Prerequisites
Before we begin, let’s ensure we have the following in place:
- A AlmaLinux 10 dedicated server or KVM VPS.
- A basic programming knowledge.
How to Install MongoDB in Docker on AlmaLinux 10 – Step-by-Step Guide for Developers
Let’s get started with the exact steps we follow for this setup.
Step 1: Update AlmaLinux 10
Before installing anything, it’s essential we make sure our system is fully updated. We start with:
sudo dnf update -y
This ensures Docker and other packages install without dependency issues.
Step 2: Install Docker on AlmaLinux 10
AlmaLinux doesn’t ship with Docker by default, but we can install it safely from the official Docker repository.
Install the dnf-plugins-core package (which provides the commands to manage your DNF repositories) and set up the repository.
sudo dnf -y install dnf-plugins-core
Add the Docker repo:
sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
Install Docker engine:
sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
Start and enable Docker service:
sudo systemctl start docker
sudo systemctl enable docker
Verify Docker installation:
docker --version
Once we get the version output, Docker is ready on AlmaLinux 10.
Step 3: Pull MongoDB Docker Image
We’ll now pull the latest stable MongoDB image from Docker Hub.
docker pull mongo:latest
This gives us a portable MongoDB server image ready to run on any environment.
Step 4: Create a MongoDB Container with Persistent Storage
To ensure data persistence and avoid data loss on container restarts, we mount a volume.
docker run -d \
--name mongodb \
-p 27017:27017 \
-v mongodata:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=securepassword \
mongo:latest
Here’s what this does:
- --name mongodb gives our container a friendly name.
- -p 27017:27017 exposes MongoDB to the host system.
- -v mongodata:/data/db creates a persistent volume.
- The environment variables set up MongoDB’s root user securely.
To protect against brute force or accidental misuse, we always recommend using a strong password.
Step 5: Verify MongoDB is Running
We can confirm MongoDB is active by checking container logs:
docker logs mongodb
To connect interactively, run:
docker exec -it mongodb mongosh -u admin -p securepassword
This opens the MongoDB shell where we can start working with databases.
Step 6: Configure MongoDB Access and Security (Advanced)
To harden MongoDB running in Docker, we can:
A. Use a custom network:
docker network create mongo-network
Then run MongoDB inside that network:
docker run -d \
--name mongodb \
--network mongo-network \
-p 27017:27017 \
-v mongodata:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=securepassword \
mongo:latest
This isolates MongoDB from other containers or host processes unless explicitly allowed.
B. Setup a docker-compose.yml for reproducibility:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: securepassword
volumes:
- mongodata:/data/db
restart: always
volumes:
mongodata:
Then run it:
docker compose up -d
Using Compose allows us to manage services with version control and rapid deployment.
Step 7: Enable Firewall Ports (Optional)
If our AlmaLinux server is running firewalld, we can allow access to MongoDB:
sudo firewall-cmd --add-port=27017/tcp --permanent
sudo firewall-cmd --reload
For added security, we can restrict MongoDB access to trusted IPs only.
Step 8: Auto-Start MongoDB Container on Boot
If we prefer Docker to restart MongoDB automatically after a reboot, we ensure --restart=always
is part of the Docker command or Docker Compose config.
Step 9: Connection URL
Here is the connection URL using this you can connect to MongoDB.
mongodb://admin:securepassword@localhost:27017
Breakdown:
- admin: Username (set via MONGO_INITDB_ROOT_USERNAME)
- securepassword: Password (set via MONGO_INITDB_ROOT_PASSWORD)
- localhost: Host where MongoDB is running (or the server’s IP address if accessing remotely)
- 27017: Default MongoDB port exposed in the Docker container
Example for Mongoose (Node.js):
mongoose.connect('mongodb://admin:securepassword@localhost:27017/mydatabase', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Replace mydatabase
with the name of the database we want to use or create.
Step 10: Monitor and Maintain MongoDB Container
To monitor logs:
docker logs -f mongodb
To stop and start the container:
docker stop mongodb
docker start mongodb
To remove it completely (data will still persist unless volume is removed):
docker rm -f mongodb
Final Thoughts
We’ve now set up a secure, persistent, and production-friendly MongoDB instance inside Docker on AlmaLinux 10. This modern containerized setup ensures minimal OS overhead, fast deployments, and better control over environment isolation.
Our development and DevOps teams can use this guide to:
- Spin up test environments
- Build microservices with MongoDB backend
- Deploy scalable APIs with minimal configuration
By following these steps, we stay aligned with modern container best practices while ensuring our system is clean, efficient, and secure.