how to access the individual container id for use in logging on kubernetes

6/26/2017

Is it possible for a container running in kubernetes to access its container id? If I have a service (my term, not kubernetes) and have scaled it 5 times horizontally, I want to log to stdout including the individual container id. Is this possible, or indeed is this even something I should be doing? I may not have grasped the concepts behind kubernetes services yet.

-- Zuriar
kubernetes

2 Answers

6/27/2017

Have you try the downward API volume? It's one easy way to put Pod and container metadata inside your container. Although it doesn't mention the container id, other metadata might be useful.

https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/

Another option is to include kubectl in your container as explained in this answer

-- rod
Source: StackOverflow

6/27/2017

For OpenShift, and therefore I presume also Kubernetes, you can possibly look up the HOSTNAME environment variable and use the value of it. This will be the name of the pod. I know it isn't the container ID, but could be close enough.

-- Graham Dumpleton
Source: StackOverflow