Finding swarm container name from within container?

I need to find the container name to use within CATALINA_OPTS with the instance id that is visible when executing docker inspect on a swarm enabled container ie.

 "Labels": {
        "com.docker.swarm.task.name": "MyService.2"
 }

Any ideas on how to collect this within the container? Maybe mount a file and update with a “docker ps” command? Sounds hacky…

I don’t understand. You are interested in service-to-service communication?

No, I need to inject variables into CATALINA_OPTS at runtime to instrument AppD. I need to come up with a way to identify the service using an instance. If i could discover “com.docker.swarm.task.name” from inside the container that would be perfect as in my example, the label is the 2nd instance of MyService. I am currently using “service name”“hostname from /etc/hostname” which isn’t working out because container id’s come and go but instances are persistent unless scaling takes place.

Usually you wouldn’t want to rely on individual task names – what do you need to inject into CATALINA_OPTS exactly? In services on the same docker network you can resolve to another service using the name of the service, and it will load balance requests to this among task instances automatically.

So now in catalina_opts i’m setting some property value like DV_MYHOST1_MYSERVICE_CONTAINERID. What I would rather have is DV_MYHOST1_MYSERVICE_1 incremented for however many instances. Even if I let off the host specific info like this DV_MYSERVICE_1 would be desirable since the node names have to be unique.

Why do you need this?

I need this for AppDynamics monitoring.

Gotcha. Yeah unfortunately container introspection capabilities are a bit limited at the moment especially with the new Swarm 1.12 stuff. However you could use hostname (set to container ID) and then work your way backwards if needed. There’s long-standing interest in container introspection, but I’m not sure if anything’s actively being worked on.

Thanks Nathan. I am using /etc/hostname (container id) at the moment. I’ll have to work out something outside of docker to manage this.