First create a user deifned bridge network if not already available and connect all your containers to the same network. Once connected to the same user-defined network, containers can communicate with each other using container IP addresses or container names. Refer the docker docs on networking for more details.
docker network list
docker network create msr
docker pull mongodb/mongodb-community-serverHere is the link to docker hub with a detailed installation instructions here
Few points to note
mongodb://localhost:27017. You can try it with Compass, MongoDB’s GUI to visualize and analyze your data.-v argument.MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD. These environment variables will create a user with root permissions with the specified user name and password.mongodb+srv://username:password@clusterURL as an environment variable to our applicationdocker run --detach --name mongodb --network msr --publish 27017:27017 --volume C:\home\Projects\msr\data\mongodbinc:/data/db --env MONGODB_INITDB_ROOT_USERNAME=admin --env MONGODB_INITDB_ROOT_PASSWORD=password mongodb/mongodb-community-server:7.0-ubi8
[!WARNING] The path for the volume is OS specific. The above was run from a Windows command Prompt
To manage your MongoDB server or to access, import, and export your data, you can use a second MongoDB container from which you will run the necessary CLI tools. To open up a Mongo Shell session to your MongoDB Atlas server, use mongosh and specify the cluster URL.
docker run -it --name mongosh mongodb/mongodb-community-server:7.0-ubi8 mongosh "mongodb://username:password@clusterURL/database"
[!CAUTION] I have not tried this on my local yet.
docker pull mongoHere is the link to the docker hub
MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD are used to create a new user and set that user’s password. This user is created in the admin authentication database and given the role of root, which is a “superuser” role.-v /my/own/datadir:/data/db part of the command mounts the /my/own/datadir directory from the underlying host system as /data/db inside the container, where MongoDB by default will write its data files.docker run --detach --name mongodb --network msr --publish 27017:27017 --volume C:\home\Projects\msr\data\\mongodb:/data/db --env MONGO_INITDB_ROOT_USERNAME=admin --env MONGO_INITDB_ROOT_PASSWORD=password mongo:7.0.5
[!WARNING] The path for the volume is OS specific. The above was run from a Windows command Prompt
The following example starts another MongoDB container instance and runs the mongosh (use mongo with 4.x versions) command line client against the original MongoDB container from the example above, allowing you to execute MongoDB statements against your database instance:
docker run -it --network msr --rm mongo mongosh --host mongodb test
[!CAUTION] I have not tried this on my local yet.
The docker exec command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your mongo container:
docker exec -it mongodb bash
The MongoDB Server log is available through Docker’s container log:
docker logs mongodb