How to Deploy MongoDB on AWS using Docker: The Definitive Guide for First-Timers

Deploying a MongoDB database on Amazon Web Services (AWS) using Docker containers provides multiple benefits – it‘s cost-effective, easy to manage, and scales seamlessly. As a full-stack developer, I frequently use this stack for my projects. In this guide, I‘ll walk you through the process step-by-step, highlighting best practices and common pitfalls to avoid. By the end, you‘ll be able to deploy your own production-ready MongoDB instance on AWS EC2 using Docker with confidence. Let‘s dive in!

Why MongoDB + Docker + AWS?

MongoDB is the most popular NoSQL database according to DB-Engines, and for good reason. Its flexible document model, extensive querying capabilities, and horizontal scalability make it a great fit for modern applications. Containerizing MongoDB using Docker provides further benefits like portability, efficiency, and simplified management.

AWS is the dominant cloud provider, with 32% market share per Canalys. EC2 is their flagship compute service that allows you to provision virtual servers in the cloud. Together, MongoDB, Docker, and AWS are a powerful combination for deploying and scaling database workloads.

Containerizing MongoDB with Docker

The official MongoDB Docker image makes it extremely easy to spin up a MongoDB server in a container. It‘s preconfigured and optimized for running MongoDB, so we don‘t need to install or configure anything manually on the host.

Some key benefits of running MongoDB in a Docker container:

  1. Isolation – The container runs in its own isolated environment with its own resources. This provides strong security and prevents conflicts with other processes on the host.

  2. Portability – MongoDB containers can easily be moved between environments (dev, staging, prod) or even other host machines and cloud providers. This flexibility is a major advantage.

  3. Efficiency – Containers are lightweight and have minimal overhead compared to virtual machines. We can run many MongoDB containers on a single host machine.

  4. Easy orchestration – Docker provides tools like Docker Compose and Docker Swarm for defining and running multi-container applications. This simplifies deployment of complex architectures.

Deploying MongoDB on AWS EC2

Now let‘s walk through the process of actually deploying our containerized MongoDB on AWS EC2. I‘ll assume you already have an AWS account and are familiar with the basics of EC2.

1. Provision an EC2 instance

First, launch a new EC2 instance to host your MongoDB container. For this guide, I recommend using the latest Amazon Linux 2 AMI with at least a t3.medium instance type. This provides 2 vCPU and 4 GiB memory which is sufficient for development and light production workloads. For heavier workloads, choose a compute-optimized (C5) or memory-optimized (R5) instance type instead.

Make sure to configure a security group that allows inbound traffic on port 27017 which is what MongoDB uses by default. For production, you‘ll want to refine this to allow access only from trusted sources.

2. Install Docker on EC2

Once your EC2 instance is running, SSH into it and install Docker using the following commands:

sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

These commands update the package manager, install Docker from the extras repository, start the Docker service, and add the default ec2-user to the docker group so you can run Docker commands without sudo.

Log out and log back in for the group membership to take effect. Verify your Docker installation by running:

docker info

3. Run the MongoDB container

Now we‘re ready to launch our MongoDB container! Run the following command:

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

This does the following:

  • -d runs the container in detached mode i.e. in the background
  • --name mongodb sets the name of our container to "mongodb" for easy reference
  • -p 27017:27017 maps port 27017 on the host to port 27017 in the container which is what MongoDB listens on
  • mongo:4.4 is the image and tag we want to use, in this case the official MongoDB 4.4 image

The container will start up and MongoDB will be running. You can verify this by running:

docker ps

You should see your mongodb container listed with status "Up".

4. Configure MongoDB authentication

By default, our MongoDB container allows unauthenticated access from anywhere. This is insecure, so let‘s enable authentication.

First, connect to the MongoDB server in the container:

docker exec -it mongodb mongo admin

This runs the mongo CLI client inside our running container and connects to the admin database.

Create an admin user with root privileges:

db.createUser(
  {
    user: "admin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "root", db: "admin" } ]
  }
)

Exit the MongoDB shell and the container. We now need to restart our container with authentication enabled and the credentials passed in:

docker stop mongodb
docker rm mongodb
docker run -d --name mongodb \
    -p 27017:27017 \
    -e MONGO_INITDB_ROOT_USERNAME=admin \
    -e MONGO_INITDB_ROOT_PASSWORD=secretpassword \
    mongo:4.4 --auth

Now all connections to the MongoDB container must be authenticated using the admin user we created, making our database much more secure.

5. Configure the EC2 security group

As a best practice, you should refine the EC2 security group to allow inbound traffic on port 27017 only from specific trusted sources, e.g. the other EC2 instances where your application is running. This follows the principle of least privilege and provides defense in depth.

Managing the MongoDB container

Here are some tips for managing your running MongoDB container:

  • To view logs:

    docker logs mongodb
  • To restart:

    docker restart mongodb
  • To connect an external client e.g. MongoDB Compass, use the EC2 instance‘s public DNS with port 27017 and the credentials you configured earlier

  • Regularly update to the latest MongoDB Docker image for patches and improvements. You can do a rolling update to avoid downtime.

  • Configure backups and test your restore procedure regularly! You can use a tool like mongodb-consistent-backup to take live cluster-consistent backups.

  • Monitor key metrics like CPU, memory usage, disk space, and query performance using a monitoring tool like Datadog or the MongoDB Atlas service.

Troubleshooting common issues

Even with the correct setup, issues can still occur. Here are some common problems and solutions:

  • Connection refused errors – Double check that the EC2 instance‘s security group allows inbound traffic on port 27017. Verify the MongoDB container is running with docker ps.

  • Authentication failed errors – Ensure you‘re passing the correct credentials. If you‘ve lost the admin credentials, you‘ll need to start a new container without --auth to reset them.

  • Out of memory errors – The MongoDB container has run out of available RAM. Upgrade to a larger EC2 instance type or add swap space on the host as a temporary stopgap.

  • Disk space issues – MongoDB has filled up the available disk space. You‘ll need to scale up the EC2 instance‘s EBS volume or move old data to archival storage.

Scaling MongoDB on AWS

As your application grows, you may need to scale your MongoDB deployment on AWS. There are a few strategies to consider:

  1. Vertical scaling – Upgrade to a larger EC2 instance type with more CPU, RAM, and I/O throughput. This is the simplest option to handle increased traffic.

  2. Sharding – Shard your MongoDB cluster across multiple EC2 instances to distribute load. Each shard handles a subset of the total data. This requires configuration changes and application support.

  3. Read replicas – Add read-only replicas of your MongoDB container on additional EC2 instances and point read traffic to them. This offloads queries from the primary node and reduces latency.

  4. Use a managed service – For large-scale deployments, consider using a fully managed MongoDB service like MongoDB Atlas on AWS. This takes care of scaling, backups, and administration for you.

Conclusion

Deploying a MongoDB container on AWS EC2 using Docker is a great way to run a production-grade database. By following this guide, you can do it securely with authentication, backups, monitoring, and a scaling strategy in place. Of course, every application is different so adapt the advice here to your own unique situation.

Containerizing your MongoDB workload gives you the flexibility to deploy it anywhere and leverage all the benefits of containerization. Docker and AWS are a natural fit for running containers at scale in the cloud.

Whether you‘re working on a small side project or a large microservices architecture, this stack will serve you well. Now go build something great with your newly deployed MongoDB database! Let me know in the comments how it went and share your own tips for deploying NoSQL databases in containers.

Similar Posts