This folder contains instructions to set up a MongoDB database environment.
-
Navigate to this folder
Open your terminal and go to the folder containing thedocker-compose.ymlfile. -
Run the command:
docker compose up -dExplanation: This command will download the latest MongoDB image, start the container, and configure the database environment.
-
Host:
localhost:27017 -
Database:
test(default) -
Authentication:
- If authentication is enabled, use the credentials defined in your
docker-compose.ymlfile. - For learning purposes, you can start with no authentication or a simple username/password.
- If authentication is enabled, use the credentials defined in your
You can connect using any MongoDB client, such as Mongo Shell, Mongo Compass, or IDE plugins.
Tip
For IDE integration:
- IntelliJ: use Database Navigator plugin.
- Visual Studio: use MongoDB for Visual Studio or NoSQL Manager.
services:
mongo:
image: mongo:latest
container_name: mongo
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpassword
volumes:
- mongo_data:/data/db
volumes:
mongo_data:Explanation:
image: mongo:latest: Uses the latest MongoDB image.container_name: mongo: Names the container.ports: Maps container port27017to the host, allowing local connections.environment: Sets environment variables for the root user.volumes: Persists database data using a Docker volume.
Tip
You can add custom scripts to initialize your database:
- Place
.jsor.shfiles inside/docker-entrypoint-initdb.d/. - They will run automatically when the container is first started.
- Example: creating initial collections or inserting test data.