How to install and test mongo in Docker?
To install and test MongoDB in Docker, you can follow these steps:
- Pull the official MongoDB image: Run the following command in the terminal to pull the official MongoDB image.
docker pull mongo
- To run a MongoDB container: use the following command to create and start a MongoDB container:
docker run -d --name mongodb -p 27017:27017 mongo
This will create a container named mongodb and map the container’s port 27017 to the host’s port 27017.
- Connect to the MongoDB container: Use the following command to connect to the MongoDB shell of the MongoDB container:
docker exec -it mongodb mongo
This will open the command line shell for MongoDB, where you can execute various commands.
- Testing: In the MongoDB shell, you can run some basic testing commands such as creating a database, inserting data, etc. Here are some example commands:
Create a database named test.
use test
Insert a record in the test database.
db.users.insert({name: "Alice", age: 30})
Retrieve all the data from the test database.
db.users.find()
By following the above steps, you can install MongoDB in Docker and perform simple testing. You can further explore the functionalities and usages of MongoDB according to your actual needs.