MongoDB

Running MongoDB in a docker container

Open a terminal window and run:

docker run -d --name mongodb \
    --restart unless-stopped \
    -v ~/data-db/mongodb:/data/db \
    -v ~/data-db/mongodumps:/mongodumps \
    -p 27017:27017 mongo:3.4

Running databases in containers is not suggested for production environment

On Windows

# create a volume
docker volume create --name=mongodata

# run mongodb
docker run -d --restart unless-stopped --name mongodb -v mongodata:/data/db -p 27017:27017 mongo:4
Read more

Install MongoDB Compass

To manage your MongoDB instance we use Compass:

Create MongoDB container for build tests

Open a terminal window and run:

Once it is running the shell will return the container id that looks like:

Now we should enter in the container and run the mongoShell to operate on the db:

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.

We can now exit from the shell and the container, press ctrl+c then ctrl+d:

We are now ready to build and push the image to docker registry.

Push Image to Private Docker Registry

Last updated