0

Is there a way to find out from inside a Docker container whether that container is running natively on a Linux host, or in Docker Machine?

The background of the question is that I want to enable JMX in my Tomcat-based container, but for that, I need to know the external IP address of the host:

  • When running in Docker-Machine, this is something like 192.168.99.100.
  • Running natively on Linux, this is the IP address of the host.
  • Running in Docker for Mac, this is also the IP address of the host.

I have a Docker Tomcat image that I want to roll out with JMX enabled, but I don't know whether people are going to use it natively or running in Docker-Machine, hence I don't know which IP address to provide for the java.rmi.server.hostname parameter when starting Tomcat.

Is there a way to either

  • find that IP address from inside of the container (or find out where it's running), or
  • find it out from the outside (e.g. in docker-compose) and then pass it into the container upon start? Can the DOCKER_HOST environment variable help in this case? When running Docker Machine, it's set to tcp://192.168.99.100:2376- is this variable also used when not running Docker Machine?

I would like to avoid the situation where people have to provide the IP address manually when starting the container (or Docker Compose).

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 1
    regarding to your second option, you can use [docker api](https://docs.docker.com/engine/reference/api/docker_remote_api/) and get information regarding to your container! – Mohsen ZareZardeyni Dec 07 '16 at 09:23
  • @Mohsen89z what would you query in docker to find out what type of host you are running on? – Matt Dec 07 '16 at 09:34
  • 1
    I think that's going to be hard to do without injecting an environment variable at run time from the host running it. You could possibly set up a script for people to run your containers that collects the info for them. or maybe try [jolokia](https://jolokia.org) which suits being run in docker. – Matt Dec 07 '16 at 09:36

1 Answers1

0

Here's how I have solved this by using the DOCKER_HOST variable. My assumption is that if this variable is set, it will contain the IP address of the host, in the form of tcp://192.168.99.100:2367.

In my docker-compose.yaml file, I have mapped the host's DOCKER_HOST variable into the container:

version: "2"

services:
  foo:
    environment:
      DOCKER_HOST: $DOCKER_HOST

Then in the container, I evaluate this variable as part of the script that is run when the container is started, and then use it to set Tomcat's JMX properties:

# Allow to specify the JMX port from the outside. Use 10992 as a default if not provided
DOCKER_JMX_PORT=${DOCKER_JMX_PORT:-10992}

# Evaluate the $DOCKER_HOST variable
if [ -n "DOCKER_HOST" ]; then
  # DOCKER_HOST variable is set
  DOCKER_JMX_HOST=$(echo "$DOCKER_HOST" | sed -e "s|tcp://||g" -e "s/:.*//g")
else
  # DOCKER_HOST variable is not set
  DOCKER_JMX_HOST=$(hostname)
fi

# Set the JMX properties for Tomcat
export CATALINA_OPTS="-Dcom.sun.management.jmxremote=true \
  -Dcom.sun.management.jmxremote.port=$DOCKER_JMX_PORT \
  -Dcom.sun.management.jmxremote.rmi.port=$DOCKER_JMX_PORT \
  -Dcom.sun.management.jmxremote.authenticate=false \
  -Dcom.sun.management.jmxremote.ssl=false \
  -Djava.rmi.server.hostname=$DOCKER_JMX_HOST"

When the DOCKER_HOST environment variable is not set, I assume that the container is running natively, e.g. on Linux, and I use the hostname for the JMX properties.

nwinkler
  • 52,665
  • 21
  • 154
  • 168