Skip to content

Deploying a MongoDB Database

MongoDB is a popular open-source NoSQL database designed for high performance, scalability, and flexibility. It stores data in JSON-like documents, making it ideal for modern applications that require dynamic schemas and rapid development.

This guide explains how to deploy a MongoDB database to Klutch.sh using a Dockerfile, including installation, sample configuration, and persistent volume setup.

Prerequisites

  • Docker installed (Download)
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Sample Dockerfile

Create a Dockerfile in your project root:

FROM mongo:7.0
# Expose default MongoDB port
EXPOSE 27017
# Optional: Add custom initialization scripts
# COPY ./init.js /docker-entrypoint-initdb.d/

Sample Usage

You can connect to your MongoDB database from any app using the following connection string:

mongodb://<APP_HOST>:27017

Replace <APP_HOST> with the internal Klutch.sh service hostname or external IP as needed.


Deploying with Persistent Volumes

To ensure your MongoDB data persists across deployments and restarts, attach a persistent volume in Klutch.sh:

  1. Push your code (with Dockerfile) to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project.
  4. Create a new app:
    • Select your MongoDB GitHub repository and branch
    • Choose the Dockerfile option when prompted
    • Set service details and environment variables as needed
    • Attach a persistent volume:
      • In the “Volumes” section, add a new volume (e.g., mongodbdata)
      • Mount path: /data/db
    • Configure region, compute, and other settings as needed
  5. Click “Create” to deploy. Klutch.sh will build your Docker image, attach the volume, and start your MongoDB database.

Resources


Deploying MongoDB to Klutch.sh with a Dockerfile and persistent volumes ensures your data is safe and your database is production-ready.