Proy
Proy

Reputation: 346

How to access task and node information within docker container service

With docker service I can get the following running tasks and associated nodes. I am wondering how each running task can retrieve its node ID and task name? Is there any environment variable to access those? If not how can I set one?

$ docker service ps appservice

ID             NAME           IMAGE             NODE      DESIRED STATE  CURRENT STATE          ERROR  PORTS
0qihejybwf1x   appservice.1   appservice:3.0.5  manager1  Running        Running 8 seconds
bk658fpbex0d   appservice.2   appservice:3.0.5  worker2   Running        Running 9 seconds
5ls5s5fldaqg   appservice.3   appservice:3.0.5  worker1   Running        Running 9 seconds
8ryt076polmc   appservice.4   appservice:3.0.5  worker1   Running        Running 9 seconds
1x0v8yomsncd   appservice.5   appservice:3.0.5  manager1  Running        Running 8 seconds
71v7je3el7rr   appservice.6   appservice:3.0.5  worker2   Running        Running 9 seconds
4l3zm9b7tfr7   appservice.7   appservice:3.0.5  worker2   Running        Running 9 seconds
9tfpyixiy2i7   appservice.8   appservice:3.0.5  worker1   Running        Running 9 seconds
3w1wu13yupln   appservice.9   appservice:3.0.5  manager1  Running        Running 8 seconds
8eaxrb2fqpbn   appservice.10  appservice:3.0.5  manager1  Running        Running 8 seconds

Upvotes: 1

Views: 1097

Answers (1)

Proy
Proy

Reputation: 346

I was able to achieve this by setting environment variables in the docker-compose. To retrieve task id I added following line in the service configuration:

environment:
        - MYTASKID={{.Task.ID}}

Upvotes: 2

Related Questions