How to Get a container’s full id from inside of itself in Kubernetes. I want to add container id in my application log which is running as Kubernetes container
-
Possible duplicate of [How can I get Docker Linux container information from within the container itself?](https://stackoverflow.com/questions/20995351/how-can-i-get-docker-linux-container-information-from-within-the-container-itsel) – larsks Nov 11 '19 at 11:36
-
Generally, with K8S, if you're talking about containers you've missed the point of K8S. Unless you're debugging the deployment itself you would normally log at a service level, not a container level. – Software Engineer Nov 11 '19 at 15:15
-
he writes specifically that he wants to see container specific logging, that could be on a service level too. So I think it's a very good point. The current value of the podname is the same as the hostname, so running the hostname command in a Linux based pod gives you that, assuming with container the pod is meant. – Vincent Gerris Nov 27 '20 at 11:00
2 Answers
The HOSTNAME
environment variable is readily available in any container running on Kubernetes and gives the unique name of the pod in which the container is running. Use the means provided by the logging framework to acccesss the environment variable and make it part of the logging pattern, or to programatically add its value to log entries.
That should do for application logging purposes assuming there is only one application container in the pod (which is regarded as a best practice anyway).

- 4,036
- 1
- 10
- 16
-
This doesn't really answer the question though, does it? What does the pod's hostname have to do with the container id? – Software Engineer Nov 11 '19 at 15:17
-
2@EngineerDollery -- maybe it wasn't clear in my answer - the HOSTNAME environment variable available in a container running in Kubernetes pod is the unique name of the pod and has nothing to do with the k8s node/VM/server host. Therefore, and under the assumption in my answer, it can be used for logging purposes in lieu of some container id to identify where the application _instance_ was/is running. It is actually useful when logs from all application containers are shipped to a single location for analysis and correlation (Stackdriver, Splunk, Elastic...) along with system-level logs. – apisim Nov 11 '19 at 20:38
-
@apisim Actually I want to identify the logs (log4j) at the pod level(the only container is running inside the pod). I am setting hostName in SystemProperty But the problem is Application is running before the application server(weblogic server) started so it is not reflecting in logs – Kundan Kumar Jan 09 '20 at 15:02
There are two ways to expose Pod and Container fields to a running Container:
- Environment variables
- Volume Files
Together, these two ways of exposing Pod and Container fields are called the Downward API.
So, simply using environment variables you can inject any metadata of a pod into the running container.
Post Comment Update - As per the kubernetes documentation each name has a UID that is appended to the name of the resource, for example, a pod or container which will provide with for a way to get a unique ID to be used for logging.
metadata.name = myimage + unique id
note* -the only caveat here is the fact that UID changes on each upgrade so It would be better to assign a unique ID from your side to identify the container or pod in combination with K8 UID.
Here's an example of YAML.
apiVersion: v1
kind: Pod
metadata:
name: dapi-envars-fieldref
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "sh", "-c"]
args:
- while true; do
echo -en '\n';
printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
printenv MY_POD_IP MY_POD_SERVICE_ACCOUNT;
sleep 10;
done;
env:
- name: MY_POD_ID // <--- here you inject env into container
valueFrom:
fieldRef:
fieldPath: metadata.name // <--- set value of the env var to pod name
- name: MY_POD_SERVICE_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
restartPolicy: Never

- 2,689
- 1
- 21
- 40
-
The name has the ID within it, it depends on what ID you require. AS per the question, the aim is to have a unique identifier to add to logs per container. And as per the Kubernetes naming convention the pod-name`metadata.name' is composed of `imagename_uniqid` – damitj07 Nov 12 '19 at 05:25
-