Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2

Mudasir
8 min readJan 12, 2024

--

Introduction:

As a DevOps Engineer working with CICD pipelines and container orchestration tools like Kubernetes, you understand the importance of streamlining the deployment process. Docker Compose has become a valuable tool for deploying multi-container applications, making it easier to manage dependencies and ensuring consistency across different environments. In this blog, we’ll explore how to create a Docker Compose file for deploying MongoDB and MongoDB Express and discuss the significance and benefits of using these technologies.

MongoDB and MongoDB Express: A Dynamic Duo

MongoDB is a NoSQL database that has gained immense popularity for its flexibility, scalability, and ability to handle unstructured data. MongoDB Express, also known as Mongo Express, is a lightweight web-based administrative interface for managing MongoDB databases. Together, they provide a powerful solution for handling data in modern applications.

Importance of MongoDB:

  1. Schema-less Design: MongoDB’s schema-less design allows for flexible data storage, making it easier to adapt to evolving application requirements.
  2. Scalability: MongoDB scales horizontally, making it suitable for applications with varying workloads and data sizes.
  3. JSON-Like Documents: Data is stored in BSON (Binary JSON) format, which is easy to read and write, fostering seamless integration with various programming languages.

Benefits of MongoDB Express:

  1. User-Friendly Interface: MongoDB Express provides a web-based GUI for managing databases, collections, and documents, making it accessible even for those without extensive database expertise.
  2. Query Execution: It allows users to execute complex queries and view results, facilitating efficient database management and troubleshooting.

Docker Compose: Simplifying Deployment

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. Here’s why it’s beneficial for deploying MongoDB and MongoDB Express:

  1. Isolation: Containers encapsulate applications and their dependencies, ensuring isolation and avoiding conflicts between different components.
  2. Ease of Deployment: With a single YAML file, you can define all services, networks, and volumes required for your application, simplifying deployment and reducing the risk of errors.
  3. Portability: Docker Compose configurations are portable across different environments, eliminating the “it works on my machine” problem.
  4. Resource Efficiency: Containers share the host OS kernel but run in isolated user spaces, making better use of system resources compared to virtual machines.

Step 1: Set Up an AWS AMI (Amazon Linux 2) EC2 Instance

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 dashboard and click “Launch Instance.”

3. Name your Instance and Choose the “Amazon Linux 2 AMI” from the available AMIs.

4. Configure the instance specifications, including instance type and key pair.

5. Configure Network Settings

In this step, create a new security group for your AWS EC2 instance to manage network access. Initially, allow port 22 for SSH to facilitate secure remote connections. As our deployment progresses, we’ll need to open additional ports to enable access to MongoDB Express.

6.Launch the instance and connect to it via SSH.

Step 2: Install Docker and Docker Compose on Amazon Linux 2 AMI

Here we will use a simple Bash script that will install Docker and Docker Compose on an Amazon Linux 2 AMI.

Create a new file, for example, install_docker.sh, and copy-paste the following content:

#!/bin/bash

# Update the package repository
sudo yum update -y

# Install Docker using Amazon Linux Extras
sudo amazon-linux-extras install docker -y

# Start the Docker service
sudo service docker start

# Add the user to the docker group
sudo usermod -a -G docker ec2-user

# Enable Docker to start on boot
sudo chkconfig docker on

# Install Docker Compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# Display Docker and Docker Compose versions for verification
docker --version
docker-compose --version

The provided Bash script automates the installation process of Docker and Docker Compose on an Amazon Linux 2 AMI. Let’s break down each step:

  1. Update Package Repository:
  • sudo yum update -y: Updates the package repository to ensure the system has the latest information about available packages.

2. Install Docker using Amazon Linux Extras:

  • sudo amazon-linux-extras install docker -y: Uses Amazon Linux Extras to install Docker on the instance.

3. Start the Docker Service:

  • sudo service docker start: Initiates the Docker service to enable containerization.

4. Add User to Docker Group:

  • sudo usermod -a -G docker ec2-user: Adds the current user (ec2-user) to the Docker group, allowing them to execute Docker commands without using sudo.

5. Enable Docker to Start on Boot:

  • sudo chkconfig docker on: Configures Docker to start automatically when the system boots up.

6.Install Docker Compose from GitHub:

  • sudo curl -L ...: Downloads the latest version of Docker Compose from the official GitHub release page and saves it to /usr/local/bin/docker-compose.
  • sudo chmod +x /usr/local/bin/docker-compose: Makes the downloaded Docker Compose binary executable.

7. Display Docker and Docker Compose Versions:

  • docker --version and docker-compose --version: Verifies the successful installation of Docker and Docker Compose by displaying their respective versions.

Save the file and then make it executable:

chmod +x install_docker.sh

Now, you can execute the script using:

./install_docker.sh

Let’s verify that Docker and Docker Compose are set up on our Amazon Linux 2 AMI:

Step 3: Create Docker Compose File

Before setting up the Docker Compose file, let’s create a directory and navigate into it:

# Create a directory
mkdir mongodb-setup

# Move into the directory
cd mongodb-setup

Now, create a file named docker-compose.yml using a text editor of your choice, and paste the following content:

version: '3.8'
services:
mongodb:
image: mongo:latest
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- mongo-data:/data/db

mongo-express:
image: mongo-express:latest
restart: always
ports:
- "8081:8081"
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb

# Define named volumes
volumes:
mongo-data:
driver: local

Let’s go through the Docker Compose file in detail:

  • Services:
  • mongodb:
  • Image: Utilizes the official MongoDB Docker image tagged as latest.
  • Ports: Maps host machine’s port 27017 to the MongoDB container’s port 27017 for external access.
  • Environment Variables: Configures the root username as ‘admin’ and the password as ‘password’.
  • Volumes: Declares a named volume, mongo-data, ensuring persistent storage for MongoDB data.
  • mongo-express:
  • Image: Utilizes the official MongoDB Express Docker image with the latest tag.
  • Ports: Maps host’s port 8081 to the MongoDB Express container’s port 8081 for external access.
  • Environment Variables: Specifies the admin username, password, and the MongoDB server’s hostname.
  • Restart Policy: Configures the service to restart always for resilience.
  • Volumes:
  • mongo-data: Named volume with the local driver for persistent storage of MongoDB data.

This Docker Compose file orchestrates the deployment of MongoDB and MongoDB Express containers, providing clarity and modularity in configuration settings.

Step 4: Deploy with Docker Compose

Before deploying the containers, it’s advisable to ensure that you have the latest versions of the Docker images. Run the following command to pull the images specified in the docker-compose.yml file:

docker-compose pull

Start Containers in Detached Mode:

Once the images are pulled, initiate the deployment by running the following command:

docker-compose up -d

The docker-compose up -d command launches the containers defined in the docker-compose.yml file in detached mode. The -d flag ensures that the containers run in the background, allowing you to continue using the terminal for other tasks. This step effectively starts the MongoDB and MongoDB Express services, making them accessible as configured in the Docker Compose file.

Ensure you execute these commands in the directory containing the docker-compose.yml file to successfully deploy your MongoDB and MongoDB Express containers.

Step 5: Verify Docker Containers

Once you’ve deployed your containers using Docker Compose, it’s crucial to verify their status and ensure they are running as expected. Use the following command:

docker-compose ps

The docker-compose ps command provides a summary of the current state of containers defined in the docker-compose.yml file. It displays information such as the container names, their status (running, exited, etc.), and the ports they are mapped to. This verification step ensures that your MongoDB and MongoDB Express containers are successfully running.

Step 6: Accessing MongoDB Express from Browser

Before attempting to access MongoDB Express from the browser, it’s essential to open port 8081 in the security group of your AWS EC2 instance. Follow these steps:

  1. Open Port 8081:
  2. Navigate to your AWS Management Console.
  3. Go to the EC2 dashboard and locate your running instance.
  4. Under the “Security” tab, find the associated security group.
  5. Edit the inbound rules to allow traffic on port 8081.
  6. Save the changes.

Access MongoDB Express:

Open your web browser and enter the following URL:

http://your-instance-ip:8081

Replace <your-instance-ip> with the public IP or DNS of your AWS EC2 instance.

Conclusion:

In this blog, we embarked on a journey to set up a robust and scalable MongoDB and MongoDB Express environment using Docker Compose on an AWS EC2 instance. We began by provisioning an Amazon Linux 2 EC2 instance, installing Docker and Docker Compose, and creating a Docker Compose file defining the services for MongoDB and MongoDB Express. The provided Docker Compose file incorporated security measures, including setting up usernames, passwords, and configuring persistent volumes for data storage.

Following the deployment, we emphasized the importance of verifying the status of our containers using docker-compose ps. To ensure seamless accessibility, we opened the necessary ports in the EC2 instance's security group. Finally, we demonstrated how to access MongoDB Express from a web browser, interacting with the MongoDB databases securely.

This blog aimed to guide DevOps Engineers and developers through the process of creating a containerized MongoDB environment, leveraging Docker Compose for simplicity and reproducibility. By following these steps, users can effortlessly deploy, manage, and access MongoDB and MongoDB Express, fostering a streamlined and efficient development and deployment workflow.

Through a combination of AWS, Docker, and MongoDB technologies, this blog equipped readers with the knowledge to harness the power of containerization for database management, fostering a resilient and scalable infrastructure.

If you found this blog post helpful and insightful, I invite you to show your appreciation by giving it a clap! Your support fuels my motivation to continue sharing valuable content. Don’t forget to hit that follow button as well, so you can stay connected and receive updates on upcoming posts. Let’s embark on this journey together and explore even more exciting insights into the world of DevOps. Your engagement is greatly appreciated! 👏🔗

--

--

Mudasir

AWS Community Builder | DevOps Engineer | 3x AWS | 1x Azure | CCNP | Palo Alto | NSE 3 | Ansible | Terraform | Docker | Jenkins | Git