This command will open mongo in docker and connect to (or create if it does not exist) the test database:

Bonus: Mongo Express in Docker

Mongo Express is a web based UI for mongo. You can of course install it locally, but happily it exists in a docker image too.

To install it:

docker-pull mongo-express

To run it, we docker run our mongo-express container and link it to our mongo instance, in this case named as da-mongo:

docker run --link da-mongo:mongo -p 8081:8081 mongo-express

Once that is done, in your navigator you can connect to Mongo Express on localhost:8081:

From within Mongo Express you view and edit the content of the databases and run queries as required without having to resort to the command line.

Setting up the Java Application

We will in this article develop a local Java Application to communicate to the Dockerised Mongodb. We could also choose to build our Java Application inside a Docker Container too, but won’t in this article. For details on how to develop inside a Docker container, look here.

First off, create a new Maven application. With Maven we can easily download the necessary mongodb driver. In these steps I am using NetBeans

Name our application MongoClientTest

With a Maven application, to download the mongo driver we need to access the pom.xml file in the Project Files folder of our project:

Inside, we will insert the following XML snippet to define our dependency.