Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (2024)

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (2)

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 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 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.”
Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (3)

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

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (4)

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

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (5)

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.

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (6)

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

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (7)

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:

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (8)

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
Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (9)

Start Containers in Detached Mode:

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

docker-compose up -d
Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (10)

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
Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (11)

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.
Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (12)

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.

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (13)

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! 👏🔗

Deploying MongoDB and MongoDB Express with Docker Compose on AWS EC2 (2024)
Top Articles
Sword of Triton
Request proof of residence
Costco Gas Barstow
Playboy Jordan Burton
Infodog Entry
Byrn Funeral Home Mayfield Kentucky Obituaries
No Hard Feelings Showtimes Near Amc Classic Pekin 14
Fredatmcd.read.inkling.com
Oem Prismhr Employee Login
Csulb Financial Aid Office Hours
Quooker Nordic Zeepdispenser Zwart ZPNBLK | bol
Goodwill Fairport
Blue Diamond French Bulldogs
Creed 3 Showtimes Near Southeast Cinemas Alamance Crossing Stadium 16
Hypebeast Muckrack
Stretch limos were the ultimate status symbol. Now they're going for cheap on Craigslist.
Madlyn Cline Ass
Ice Dodo Unblocked 76
Ballistic Unblocked Google Sites
Howdy Porta
Walb Game Forecast
Dexter Gomovies
Ddcforum Codes
2005 Chevy Colorado 3.5 Head Bolt Torque Specs
Orlando Rub Rating
855 700 4473
International Medical Insurance for Employers from Aetna - Global Health Plans for Expatriates & Business Travel Employees | Aetna
First Daughter | Rotten Tomatoes
Cole's Dockside Pre Fixe Menu
Mayas Mexican Pell City
Newsday Crossword Puzzle Brains Only
Flamenco Kiwifarms
Olde Kegg Bar & Grill Portage Menu
Tayyy_Boo
Tops Friendly Market Ad
Drumlin Farm Birthday Party
Badger State Pullers Schedule
Us Open Tennis Sirius Radio
Narragansett Bay Cruising - A Complete Guide: Explore Newport, Providence & More
9294027542
Taylor Jailbirds New Orleans
Community near Panama City Beach, FL 32413 - craigslist
Washington Craigslist Housing
House Party 2023 Showtimes Near Cinemark Oakley Station And Xd
Chicago Craigslist Classifieds
Craigslist Cars For Sale By Owner Memphis Tn
Best used electric cars 2024: Top 10 second-hand EVs to buy
Flirty Hump Day Quotes
Tokyo Spa Memphis Reviews
Craiglsist Cars
Hca Scheduler Login
All JAGUAR XJ Models by Year (1979-2019) - Specs, Pictures & History
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6177

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.