Docker Community Forums

Share and learn in the Docker community.

Finding swarm container name from within container?

(Tonyowens) #1

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": {
        "": "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…

(Nathan Le Claire) #2

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

(Tonyowens) #3

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 “” 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.

(Nathan Le Claire) #4

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.

(Tonyowens) #5

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.

(Nathan Le Claire) #6

Why do you need this?

(Tonyowens) #7

I need this for AppDynamics monitoring.

(Nathan Le Claire) #8

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.

(Tonyowens) #9

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.