Skip to main content

Install Node.js agent in a container

Using the Agent Wizard is the simplest way to deploy a new agent by selecting Language-specific > Node > your operating system > Install in a container. Installing the Node.js agent in a container to AWS Fargate is also supported.

Select method to deploy

  1. Select Linux or macOS, or Windows.

  2. Select Dockerfile or Kubernetes.

    Kubernetes is available for Linux or macOS only.

Build Dockerfile image with agent

Under Build image with Agent:

  1. Add the following to your Dockerfile:

    RUN npm install @contrast/agent
  2. Modify your application command to include the agent:

    CMD ["node", "--import", "@contrast/agent", "[application entrypoint]"]

    The --import flag loads the agent before your application code. For example, if your Dockerfile starts the application with CMD ["node", "app.js"], change it to CMD ["node", "--import", "@contrast/agent", "app.js"]. If you start the application with an npm script, put the flag in the script.

  3. Build the image:

    docker build -t my-app-with-contrast .

Install the agent with optional dependencies enabled, on the same operating system and architecture that the container runs on. The agent includes platform specific binaries. If they do not match, the agent logs Failed to load native binding and your application starts without it. Do not use --omit=optional.

If you install in one build stage and copy the result to another, use the same operating system and architecture in both.

Set the container working directory (WORKDIR) to the folder that holds your application's package.json. If the file is somewhere else, set agent.node.app_root. The file needs a name field, or you must set the application name with CONTRAST__APPLICATION__NAME.

On Windows, the Dockerfile commands are the same. The agent supports Windows on x64 only, so use an x64 Windows image.

If your image has no shell and no npm, such as a distroless image, you cannot change the start command in a script. Set the NODE_OPTIONS environment variable instead:

ENV NODE_OPTIONS="--import @contrast/agent"

NODE_OPTIONS reaches every Node.js process in the container. By default the agent skips npm, npx, yarn, pnpm and common build tools.

To shorten startup time, you can rewrite your application while you build the image. See Reduce container startup time for details.

For Kubernetes (Linux only)

The Contrast Agent Operator sets NODE_OPTIONS on the workloads it injects, so you do not change your container command for Kubernetes installs.

  1. Under Select method to deploy with containers, select Kubernetes.

  2. Under Select a method to configure agent operator, select Helm charts or Manifest.

  3. For Helm:

    1. Under Download and set up the values file, copy the code displayed in the wizard to create a values file.

      Skip this step if you already have the operator installed.

    2. Under Install and deploy Helm chart, run the Helm commands in the wizard.

      Skip this step if you already have the operator installed.

    3. Under Label namespaces, run the command displayed to label your namespaces to add AgentInjectors.

    4. Under Label deployments, follow the wizard steps and run the commands to label your deployments.

  4. For Manifest:

    1. Under Install operator, run the commands displayed in the wizard to install the Contrast agent operator.

      Skip this step if you already have the operator installed.

    2. Under Configure operator, follow the wizard steps to deploy the secret and to create the ClusterAgentConnection.

      Skip this step if you already have the operator configured.

    3. Under Configure Injector, copy or download the YAML file, run the kubectl apply command displayed in the wizard, and then wait for the command to complete.

    4. Under Label namespaces, run the command displayed to label your namespaces to add AgentInjectors.

    5. Under Label deployments, follow the wizard steps and run the commands to label your deployments.

    6. Configure agent (optional). Copy the ClusterAgentConfiguration YAML to set agent settings for the workloads the operator injects.

Configure the agent

A connection token is the recommended method. Keep credentials out of your Dockerfile and your image.

For a Dockerfile install, set CONTRAST__API__TOKEN in the environment where you run your application. You can also use other environment variables or the Node.js YAML template.

On Windows, the wizard shows the token command with set in place of export.

For a Kubernetes install, the operator uses the token in the values file for Helm, or the secret and ClusterAgentConnection that you created in the Configure operator step for Manifest. To set other agent settings, use the yaml setting under clusterDefaults in the values file for Helm, or the optional Configure agent step for Manifest.

Verify the agent deployment

Interact with your application as you normally would. For example, interact with your application's web interface or send API commands.

  1. In the Agent Wizard, select View agent activity to verify that your agent appears in the agents list.

  2. Verify that you see the name of your application in the applications list.

    • In Northstar, select Applications from the left navigation.

    • In Contrast, select Applications from the header.

  3. Verify that you see the name of your local server in the servers list.

    • In Northstar, select Servers from the left navigation.

    • In Contrast, select Servers from the header.