Running databases in containers is not suggested for production environment
On Windows
# create a volumedockervolumecreate--name=mongodata# run mongodbdockerrun-d--restartunless-stopped--namemongodb-vmongodata:/data/db-p27017:27017mongo:4
Now we should enter in the container and run the mongoShell to operate on the db:
dockerexec-itmongodb-testbash# we can now launch the mongoshell typing mongoroot@0cd914b83101:/#mongoMongoDBshellversionv3.4.15connectingto:mongodb://127.0.0.1:27017MongoDBserverversion:3.4.15WelcometotheMongoDBshell.Forinteractivehelp,type"help".Formorecomprehensivedocumentation,seehttp://docs.mongodb.org/Questions?Trythesupportgrouphttp://groups.google.com/group/mongodb-userServerhasstartupwarnings:2018-09-27T11:54:42.732+0000ISTORAGE [initandlisten]2018-09-27T11:54:42.732+0000ISTORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine2018-09-27T11:54:42.732+0000ISTORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem2018-09-27T11:54:42.854+0000ICONTROL [initandlisten]2018-09-27T11:54:42.855+0000ICONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.2018-09-27T11:54:42.855+0000ICONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.2018-09-27T11:54:42.855+0000ICONTROL [initandlisten]>
We are now ready to populate the db with some sample data for out tests suite.
Populating the Database
With the command use DATABASE_NAME MongoDB will create a new db called DATABASE_NAME, then we can check if it worked typing show dbs, since it hasn't any document it doesn't show the created db, so we need to insert a document by typing db.users.insert({"username": "daton", "password": "$2a$13$RnsMJ9C6LgZTOOYANiTKbO4vgnVo3MulMMfrL/Ly7E04sqSGmg8dO"}), now typing show dbs return also the list of the newly created db.
> use daton_authswitchedtodbdaton_auth> show dbsadmin0.000GBlocal 0.000GB> db.users.insert({"username": "daton", "password": "$2a$13$RnsMJ9C6LgZTOOYANiTKbO4vgnVo3MulMMfrL/Ly7E04sqSGmg8dO"})WriteResult({"nInserted":1})> show dbsadmin0.000GBlocal 0.000GBdaton_auth0.000GB>
We can now exit from the shell and the container, press ctrl+c then ctrl+d:
> ^C
bye
root@0cd914b83101:/# exit
We are now ready to build and push the image to docker registry.
Push Image to Private Docker Registry
14:28:10 in ~ via ⬢ v8.11.2 took 27m 45s
➜ docker commit $(docker ps -lq) mongodb-test
sha256:10276f0e2af807d725905c52f020a2a86b29c8caf5cbae2061e50b94f630ab2f
14:28:28 in ~ via ⬢ v8.11.2
➜ docker tag mongodb-test $REGISTRYADDRESS/mongodb-test
14:29:02 in ~ via ⬢ v8.11.2
➜ docker push $REGISTRYADDRESS/mongodb-test
The push refers to repository [$REGISTRYADDRESS/mongodb-test]
a0c3435b9e9f: Pushed
3bdfc7b15c4a: Pushed
84d5f4a4e9e6: Pushed
8cbce8e02108: Pushed
278c33372e35: Pushed
a0d9c837c597: Pushed
68a272e55e7a: Pushed
a2287de7d928: Pushed
8e810b18217e: Pushed
b81c040c9ace: Pushed
8d8ae80a45b3: Pushed
ba291263b085: Pushed
latest: digest: sha256:a022b3a241f28416aa7ec90bd660a38c4cc02ecda7b518cca5fbf57c9472c4d2 size: 2821