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.
Before you begin, ensure you have the following installed on your system:
- Docker
- Docker Compose (Included with Docker Desktop for Mac and Windows)
Follow these steps to get your local MongoDB environment up and running.
git clone https://github.com/ubc/tlef-mongodb-docker
cd tlef-mongodb-docker
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.
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
.
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
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.
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
If you want to completely wipe your local database and start fresh, you can remove the persistent data volume.
-
Stop and remove the containers:
docker-compose down
-
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
. -
Restart the services:
docker-compose up -d
This will create a new, empty database.