Use a Dockerfile

Note: This option applies to containers running in Docker and Kubernetes.

This option uses a Dockerfile to include the Node.js Agent binaries in the Docker image at build time. It assumes a single image is built that contains the application and Node.js Agent binaries.

To include the agent in the application image during the image build:

  1. Perform the Node.js Agent Installation Steps
  2. Copy the Application Folder to the Image
  3. Set the Node.js Agent Environment Variables
  4. Set the UNIQUE_HOST_ID Environment Variable
  5. (On-Premises Controller only) Copy the Controller Certs to the Image

Perform the Node.js Agent Installation Steps

Perform the installation steps described in Install the Node.js Agent.

  1. Run the NPM command to include the appdynamics package from your application folder:

    $ cd nodejsapp
    $ npm install appdynamics@next
  2. Add the require statement to the application source code and include reuseNode and reuseNodePrefix properties. Set the reuseNodePrefix property to refer to an environment variable that will be set in a later step:

    require("appdynamics").profile({
    reuseNode: true,
    reuseNodePrefix: process.env.APPDYNAMICS_AGENT_REUSE_NODE_NAME_PREFIX
    });

    The reuse node name and prefix properties are required to support unique naming for multiple container instances for the same application image. See Node.js Settings Reference.

  3. If the Node.js Agent should report transaction analytics data, add the analytics properties to the require statement and set the value to an environment variable that will be set in a later step. See Node.js Settings Reference.

    require("appdynamics").profile({
    reuseNode: true,
    reuseNodePrefix: process.env.APPDYNAMICS_AGENT_REUSE_NODE_NAME_PREFIX,
    analytics: {
    host: process.env.APPDYNAMICS_ANALYTICS_HOST_NAME,
    port: process.env.APPDYNAMICS_ANALYTICS_PORT,
    ssl: process.env.APPDYNAMICS_ANALYTICS_SSL_ENABLED
    }
    });

Copy the Application Folder to the Image

Edit the Docker file to copy the application folder to the image:

COPY nodejsapp/ /nodejsapp/

Set the Node.js Agent Environment Variables

If you are running the application in a non-Kubernetes environment (using docker run for example), set the agent environment variables in the Dockerfile. For example:

ENV APPDYNAMICS_AGENT_APPLICATION_NAME=<value>
ENV APPDYNAMICS_AGENT_TIER_NAME=<value>
ENV APPDYNAMICS_AGENT_ACCOUNT_NAME=<value>
ENV APPDYNAMICS_AGENT_ACCOUNT_ACCESS_KEY=<value>
ENV APPDYNAMICS_CONTROLLER_HOST_NAME=<value>
ENV APPDYNAMICS_CONTROLLER_PORT=<value>
ENV APPDYNAMICS_CONTROLLER_SSL_ENABLED=<value>
ENV APPDYNAMICS_AGENT_REUSE_NODE_NAME_PREFIX=<value>
ENV APPDYNAMICS_AGENT_NODE_NAME=<value> # not used in node name but required by Node.js agent
ENV APPDYNAMICS_LOGGER_OUTPUT_TYPE=console
# variables required to send transaction analytics data
ENV APPDYNAMICS_ANALYTICS_HOST_NAME=<value>
ENV APPDYNAMICS_ANALYTICS_PORT=<value>
ENV APPDYNAMICS_ANALYTICS_SSL_ENABLED=<value>

For Kubernetes applications, omit these environment variables from the Dockerfile and set them using ConfigMaps, Secrets, and the deployment spec as described in Use Init Containers. The analytics host, port and ssl settings depend on how the Analytics Agent is deployed. See Deploy Analytics in Kubernetes for options.

Set the UNIQUE_HOST_ID Environment Variable

Set the UNIQUE_HOST_ID environment variable to enable APM correlation with the Cluster Agent. Since the value depends on a runtime value, set this environment variable in the image startup script using the values documented in Manually Configure App Agents to Correlate with the Cluster Agent.

For a Kubernetes environment with a Docker runtime, add the following startup script startup.sh to the Docker image in the following example:

#!/bin/bash
# OpenShift 3.10 or 3.11:
UNIQUE_HOST_ID=$(sed -rn '1s#.*/##; 1s/(.{12}).*/\\1/p' /proc/self/cgroup)
exec node /nodejsapp/myapp.js

(On-Premises Controller only) Copy the Controller Certs to the Image

For Node.js Agents communicating with an on-premises Controller, edit the Dockerfile to copy the cert file containing the on-premises certs to the image.

For example:

COPY ./onprem-cacerts /opt/appdynamics/cacerts

Update the application source code to add the certificateFile property in the require statement. Set the certificateFile path to the location of the cert file.

require("appdynamics").profile({
reuseNode: true,
reuseNodePrefix: process.env.APPDYNAMICS_AGENT_REUSE_NODE_NAME_PREFIX,
certificateFile: /opt/appdynamics/cacerts
});