How do I pass the real hostname to my dockerized application instead of the container name?
By default, when you use the variable $HOSTNAME, you will get the randomly generated container
name at runtime.
This makes viewing historical data difficult for APM users as the data is tied to the host, process, and
agent names.
If the agent is not able to retrieve a hostname at runtime, it will default to the string, "ContainerHost".
Application Performance Management 10.7
Application Performance Management SaaS
There are two solutions available, depending upon how you have setup your Docker environment.
While the examples below are specific to running on Linux/UNIX/MacOS/WSL, you can do the same
thing using Windows batch scripts or PowerShell.
Using Docker Compose
You can add an environment variable for your service:
version: "2.4"
services:
tomcat:
image: tomcat:9.0
environment:
REAL_HOST: MYCOMPUTERNAME
N.B.: The above example is an excerpt from a working docker-compose.yml.
You can now run the service using standard syntax:
docker-compose up -d
Using a custom environment file with Docker Compose
N.B.: When using this method, set the environment variable's value to the exported variable in your
custom environment file.
To dynamically get the hostname and pass it to your container, do the following:
1. Create a text file with the following content
export REAL_HOST=$(hostname)
2. Call the custom environment file before calling Docker Compose
source custom.env; docker-compose up -d
Without Docker Compose
Using Bash
docker run --env REAL_HOST=`hostname` tomcat:9.0
Try using parentheses if back ticks do not work in your shell.
docker run --env REAL_HOST=(hostname) tomcat:9.0
Using the environment variable with the Agent
Use the variable as a Java system property.
-Dintroscope.agent.hostName=$REAL_HOST
You may need to use curly braces, depending on your runtime environment.
-Dintroscope.agent.hostName=${REAL_HOST}
Hard-code the value in IntroscopeAgent.profile.
This is an undocumented property so you will need to add this to your profile.
introscope.agent.hostName=MYCOMPUTERNAME
Thank Haruhiko Davis for the writeup for this KB.