Skip to content

ubc/tlef-mongodb-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Local MongoDB Environment with Docker

This project provides a simple, Docker-based setup for running a local MongoDB instance with authentication enabled. It also includes Mongo Express, a web-based administration UI.

This setup is designed to provide a consistent and easy-to-manage database environment for developers, ensuring that applications can be developed locally and then deployed to staging/production environments with minimal changes to their configuration.

Prerequisites

Before you begin, ensure you have the following installed on your system:

Getting Started

Follow these steps to get your local MongoDB environment up and running.

1. Clone the Repository

git clone https://github.com/ubc/tlef-mongodb-docker
cd tlef-mongodb-docker

2. Configure Environment Variables

Create a local environment file by copying the example file.

cp .env.example .env

The .env file holds the credentials for your local database. You can customize these values if you wish, but the defaults are suitable for local development.

  • MONGO_INITDB_ROOT_USERNAME: The root username for the MongoDB instance.
  • MONGO_INITDB_ROOT_PASSWORD: The root password for the MongoDB instance.
  • MONGO_EXPRESS_LOGIN: The username to log in to the Mongo Express UI.
  • MONGO_EXPRESS_PASSWORD: The password to log in to the Mongo Express UI.

Important: The .env file is included in .gitignore and should never be committed to version control.

3. Start the Services

Run the following command to build the images and start the MongoDB and Mongo Express containers in detached mode:

docker-compose up -d

You can check the status of the running containers with docker-compose ps.

Connecting to the Database

Your applications can connect to the MongoDB instance using the following connection string format. The credentials should match what you have set in your .env file.

mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@localhost:27017/

For a Node.js application, you would typically use an environment variable for the connection string and the dotenv package to load it from a .env file within your application's project.

Example connection string using default credentials:

mongodb://mongoadmin:secret@localhost:27017/

You can also specify a default database in the connection string:

mongodb://mongoadmin:secret@localhost:27017/my-app-database?authSource=admin

Accessing the Database UI

Mongo Express is available in your web browser at:

URL: http://localhost:8081

Log in with the MONGO_EXPRESS_LOGIN and MONGO_EXPRESS_PASSWORD credentials from your .env file.

Managing the Environment

Stopping the Services

To stop the running containers without removing them, run:

docker-compose stop

To stop and remove the containers, networks, and volumes created by up, run:

docker-compose down

Resetting the Database

If you want to completely wipe your local database and start fresh, you can remove the persistent data volume.

  1. Stop and remove the containers:

    docker-compose down
  2. Remove the data volume (the volume name is <directory-name>_mongodb_data):

    docker volume rm tlef-mongodb-docker_mongodb_data

    You can confirm the volume name by running docker volume ls.

  3. Restart the services:

    docker-compose up -d

This will create a new, empty database.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published