Running MongoDB on Docker

Now that we have Docker running, we can start a new MongoDB process through Docker. This process is really easy:

  1. Go to the Terminal and type the following:
$ docker run --name my-mongo -p 27017:27017 -d mongo:latest
Every time you restart Docker, the content will be lost. To avoid that, you can create a volume on the host machine.
  1. Create the following volume on the host machine:
$ docker run --name my-mongo -p 27017:27017 -v /data/mongo:/data/db -d mongo:latest

This command will create a new MongoDB through Docker. The container's name will be my-mongo, the port will be mapped to 27017, and we will get the latest docker image at https://hub.docker.com/_/mongo/.

  1. Once this command finishes, type the following command: 
$ docker ps

You should be able to see the container running:

Listing the new container created using the ps command
  1. Now, let's go to this container and see whether MongoDB is running. To do so, run the exec command, as follows, to go to the container we created:
$ docker exec -it my-mongo sh
  1. Then, run the following command to connect to the MongoDB:
$ mongo

The output should look as follows:

MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-12-27T01:56:17.455+0000 I STORAGE [initandlisten]
2018-12-27T01:56:17.456+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-12-27T01:56:17.456+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-12-27T01:56:18.144+0000 I CONTROL [initandlisten]
2018-12-27T01:56:18.145+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-27T01:56:18.145+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-27T01:56:18.145+0000 I CONTROL [initandlisten]

If you see the previous output, everything is fine and we are good to move on to the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.160.216