Adding New Relic APM and SERVERS monitoring to your dockerized ghost app

In previous post, we went through setting up a basic ghost application and it's dependencies separated in docker containers, and orchestrating it all with docker-compose.
In this small article, we will go through modifying the ghost application from the previous post, to include New Relic APM library, to monitor the Express served ghost application.
To finish up, we'll add the New Relic SERVERS library as a separate container, to monitor the actual server that are hosting the containers.
And although this article is based on code from a previous post, the instructions to setup the New Relic libraries should me somewhat applicable to other setups.

New Relic offers several different libraries to monitor all aspects of your application and architecture. They all integrate really smoothly, so dockerizing these libraries are pretty trivial. Best of all, they offer a free LITE subscription! - I fucking love free shit!

As base source, I'm using the results from previous post.
The final code after the implementations elaborated in this article, can be found in a branch in the github repo.. The master branch has probably diversified by now.
I'm gonna assume you've read the previous post, as I'm gonna show partial chunks of that source.


I want to monitor the performance and behaviour of my ghost application. The installation should be included in the docker container housing the actual ghost application/site.

I also want to monitor the actual server, that are hosting all the docker containers. This tool should be added in a separate container.


New Relic APM bash install script

When you've fetched your API key, we need to write a bash script which installs the new relic npm module, copies the newrelic.js file, and makes the nessecary changes inject the module into ghost. Here is how looks like:

echo "Installing new relic!"

npm install newrelic
cp node_modules/newrelic/newrelic.js newrelic.js

sed -i -r "s/My Application/mewm blog/" newrelic.js
sed -i -r "s/info/trace/" newrelic.js 
sed -i -r "s/license key here/${1}/" newrelic.js 
sed -i "1ivar newrelic = require('newrelic');" index.js 

This installs the npm module, and and copies the config file. We use sed to replace the mandatory values in newrelic.js.
In the first sed, we switch the name of what's gonna pop up in the New Relic console.
The API key is taken from the first command line argument.
The info/trace value determines the aggresiveness of the monitoring. trace is the most meticulous, while info and up has less performance overhead.
The last line, injects the require() statement in to the first line in the ghost index.js.

We also need to change the Dockerfile for the ghost application, to add the install script during build. This is a line consisiting of ADD / - Ive added that line right under the ADD command, that adds the file.

Ghost run script alterations

The is our default command for the docker container, which is housing ghost. We need to modify it, so it runs the This is just the top of the file. The rest is elaborated here


if [ "${NEW_RELIC}" != "0" ] && [ "${NODE_ENV}" = "production" ]; then
	sh / ${NEW_RELIC}

Here, we basically make sure, that if we have an API key provided, and we're in node production mode, we run our new relic install script, with the API key, as first argument.
If you do not provide an API key in docker-compose.yml, this step will be skipped.

After rebuilding and booting your new container, after a few minutes, you should be able to see some metrics in the New Relic console. You'll also be able to see performance stats for your database.

New Relic SERVERS docker container

There is a pre-built image out there, which takes care of setting up the New Relic server daemon, to provide server stats. In fact, we can keep the instructions only to the docker-compose.yml file. Here is a paste of the modified docker-compose.yml:

  build: ./data
    - /var/lib/mysql
    - /var/www/ghost/content
#    - /Users/mewm/www/ghost-theme:/var/www/ghost/content/themes/casper This can be added for theme development. Comment out the theme stuff in before rebuilding
  build: ./mariadb
    - "3305:3305"
    - data
    - DEFAULT_USER=ghost # A user with this name will be created
    - DEFAULT_PASS=foobarbaz
    - PORT=3305
  build: ./ghost
    - "2368:2368"
    - db:database
    - data
    - DB_HOST=database
    - DB_CLIENT=mysql
    - DB_USER=ghost
    - DB_PASSWORD=foobarbaz
    - DB_PORT=3305
    - DB_DATABASE=ghost
    - NODE_ENV=production
    - URL=
    - THEME_SOURCE= # Git repo to fetch theme from
    - NEW_RELIC_LICENSE_KEY=<api key here>
  image: uzyexe/newrelic
    - NEW_RELIC_LICENSE_KEY=<api key here>

As you can see, it takes very few instructions to setup up the server monitoring. After booting the container, the hostname should pop up in the New Relic console.

Final words

Monitoring your applications and servers, are crucial to businesses. Detecting bottlenecks and evaluate performance is much easier if you have proper monitoring set up. New Relic is just one of the many tools out there. Also, stats and infographics just turns me on!

I use New Relic because it's what I have most experience with, but there are a ton of alternatives out there.
Also, back in the days when I signed up, they promised me a t-shirt - never got it though :(

Show Comments