Red Hat Mobile Application Platform

Getting up and running with local development for Red Hat Mobile Application requires that you run MongoDB and Redis locally. Doing so isn’t particularly difficult if you follow online guides, but it would be much more straightforward if you could just get these pieces of software up and running in a single command and not need to worry about versioning, creating data directories, setting permissions, and compiling some things such as Redis from source. It would be even better if you could easily switch versions. This is where containers shine.

In the next few paragraphs, we'll demonstrate how you can run any almost any version of MongoDB and Redis with a single command on a machine that has the Docker service installed.

NOTE: This is not an extensive Docker CLI tutorial; just enough to learn basic commands that will allow you to get MongoDB and Redis up and running easily.

Docker

If you aren’t familiar with Docker then it’s worth heading over to their What is Docker? page, but to put it simply, the Docker project provides a container runtime that allows us to run software in a containerized environment. This means you can easily play around with a software package without affecting your host OS such as Ubuntu, Windows, or macOS. It also means you get preconfigured environments for running software which is exactly what we’d like for Redis and MongoDB - no complex setup required.

Installing the Docker CLI is simple for Windows and macOS once you have a recent version of VirtualBox also installed on your machine. Ubuntu appears to be a little more involved, but the documentation is clear and concise so there should be no issues. Here are the guides for each platform:

Once you’ve finished following the guide for your system you can verify the installation by typing the following in a terminal:

$ docker --version
Docker version 17.03.0-ce, build 60ccb22

If you see some version output then you’re good to go. It’s worth noting that I was initially running a much older version (1.11.0, build 4dc5990) and had encountered issues surrounding port mappings - so make sure you use a recent version!

Running MongoDB in a Container

The following command tells the Docker CLI to start a container that’s running Mongo db version 2.4.14. Passing the version is one of the most powerful features of the Docker CLI. Doing so allows us to test multiple versions of MongoDB in minutes without going through the arduous process of making configuration changes on our host machine! 2.4.14 is close to the version running on Red Hat Mobile Application Platform.

docker run --name mongodb -p 27017:27017 -p 28017:28017 -d mongo:2.4.14

You can use a tool such as Robomongo or mongo-express to connect and play around with the database. When developing node.js code you can use the mongodb module to connect.

Being able to do this is extremely useful since if a MongoDB update is planned for your Red Hat Mobile deployment you can update to the same version for local development and test instantaneously.

Running Redis in a Container

Now that we appreciate the power of containers we can fire up Redis easily too. Just run the following command:

docker run --name redis -p 6379:6379 -d redis

If you want to verify it’s working you could use a tool such as redis-commander to access it, or just try from your application code using the node.js driver, or $fh.cache in the fh-mbaas-api.

Executing Commands within a Container

If you’d like to run commands inside a container (think like SSH’ing in) then you’ll need either the container ID or name. You can easily get the container name like using the docker ps command.

Output from "docker ps"

Once you have the ID, you can pass it to the exec command to get access to a bash prompt in the specified container:

docker exec -it $CONTAINER_ID bash

This is a neat way to debug your application’s interactions with the container, such as using SET, GET, and KEYS commands in our Redis container like so:

$CONTAINER_ID=$(docker ps | grep -i redis | awk '{print $1}')
$ docker exec -it $CONTAINER_ID bash

root@8b0867a109fe:/data# redis-cli
127.0.0.1:6379> keys *
1) "cached-oauth-token"
127.0.0.1:6379> exit
root@8b0867a109fe:/data# exit

Docker Containerized Environment Performance vs. Native

When running a containerized application on macOS or Windows for development purposes you need to run it within VirtualBox since these systems don’t have kernel level container support like Linux does. Ultimately we should not worry about this since our goal with this post is to use containers on our development machines where high performance isn’t a concern, but for the curious the differences I noted between a native MongoDB instance vs. one running in the docker-formatted container are:

  • Containerized MongoDB 1,000 Sequential Insert Time: ~775ms
  • Containerized MongoDB 1,000 Concurrent Insert Time: ~429ms
  • Native MongoDB macOS 1,000 Sequential Insert Time: ~422ms
  • Native MongoDB macOS 1,000 Concurrent Insert Time: ~311ms

Or if you prefer graphs here it is:

Docker Performance Comparison

Both are using node.js 4.4.3, MongoDB v2.4.14, no indexes applied, and just a default empty collection. The MacBook Pro performing the test is a 2.5GHz Intel Core i7 with 16GB 1600MHz RAM and a 500GB SSD (mid-2015 model).

The code for each test is linked below:

I’m perfectly happy with the numbers posted by the MongoDB instance running in a docker-formatted container since, typically as developers, we’re not doing much benchmarking on our own machines so the difference will be negligible unless you’re dealing with huge numbers of queries and extremely large payloads.


There are many GUI tools to connect to MongoDB databases and browse, download this cheat sheet to get to the command line to get the command line you need.

Last updated: October 31, 2023